gpt4 book ai didi

c++ - 如何仅使用Iterator显示Vector的前10个元素?

转载 作者:行者123 更新时间:2023-12-02 10:18:23 25 4
gpt4 key购买 nike

我正在尝试使用displayVectorVer2()使其仅显示前10个元素,但是我不知道如何使用迭代器。我确实尝试了一些愚蠢的事情,只是为了看看会发生什么:我在for循环中将迭代器与displayLimit进行了比较。因为我的教授只让我使用15个元素,所以我通过减去vobj.end()-5来玩耍,但是我完全知道这不是一个好主意。

#include <iostream>
#include <vector>
#include <ctime>

template <class T>
void fillVector(std::vector<T>& vobj, int n);

template <class T>
void displayVectorVer2(std::vector<T>& vobj, typename std::vector<T>::iterator ptr);

template <class T>
void fillVector(std::vector<T>& vobj, int n)
{
srand((unsigned int)time(NULL));
for (int i=0; i<n; ++i)
{
vobj.push_back(rand()%99999+1);
}
}

template <class T>
void displayVectorVer2(std::vector<T>& vobj, typename std::vector<T>::iterator ptr)
{
std::cout << "Vector object contains " << vobj.size() << " values which are" << std::endl;

const unsigned displayLimit = 10;
if (vobj.size()>displayLimit)
{
for (ptr=vobj.begin(); ptr<vobj.end(); ++ptr)
{
std::cout << " " << *ptr;
}
std::cout << " ..." << std::endl;
}
else
{
for (ptr=vobj.begin(); ptr<vobj.end(); ++ptr)
{
std::cout << " " << *ptr;
}
std::cout << std::endl;
}
}

int main()
{
std::vector<int> vobj;
std::cout << "Before calling fillVector(...): vobj contains "
<< vobj.size() << " values." << std::endl;

std::cout << "\nEnter # of random values you'd like to store in vobj: ";
int n;
std::cin >> n;

std::cout << "\n*** Calling fillVector(...) ***" << std::endl;
fillVector(vobj, n);

std::cout << "\n*** Calling displayVectorVer2(...) ***" << std::endl;
std::vector<int>::iterator ptr;
displayVectorVer2(vobj,ptr);
}

最佳答案

也许我想的太简单了,但是那个解决了您的问题:

I'm trying to use displayVectorVer2() to have it only display the first 10 elements



不知道您是否进行充分的运动,那将是我的答案:

...
const unsigned displayLimit = 10;
if (vobj.size()>displayLimit)
{
for (ptr=vobj.begin(); ptr<vobj.begin()+displayLimit; ++ptr)
{
std::cout << " " << *ptr;
}
std::cout << " ..." << std::endl;
}
else
...

编辑:

That worked, but why does it work? I remember adding to vobj.begin() and getting extra empty elements appended to the original vector.



不知道您到底做了什么,但是也许可以帮助您理解代码:

...
const unsigned displayLimit = 10;
if (vobj.size()>displayLimit)
{
//Init ptr outside the for loop
ptr = vobj.begin();

//What the for loop is seeing with a more familiar syntax:
//for( ; i < 0 +displayLimit; ++i)
//what you are seeing
for (/*ptr init*/; ptr < vobj.begin() +displayLimit; ++ptr)
{
std::cout << " " << *ptr;
}
std::cout << " ..." << std::endl;
}
...

迭代器只为您提供int值,并且您可以将其与曾经“吃掉” int值的东西一起使用。在您的情况下,for循环。

如果告诉程序使用迭代器,则告诉程序:“只要给我Vector开头的数字并加10”即可。

在您的情况下,0“...并添加10”

您还可以编写类似的代码,并将n传递给函数
能够使用.end-input + 10来显示10行:

...
template <class T>
void displayVectorVer2(std::vector<T>& vobj, typename std::vector<T>::iterator ptr,int n)
{
std::cout << "Vector object contains " << vobj.size() << " values which are" << std::endl;

const unsigned displayLimit = 10;
if (vobj.size()>displayLimit)
{
ptr=vobj.begin();
for (; ptr<vobj.end() -n +displayLimit; ++ptr)
{
std::cout << " " << *ptr;
}
std::cout << " ..." << std::endl;
}
else
{
for (ptr=vobj.begin(); ptr<vobj.end(); ++ptr)
{
std::cout << " " << *ptr;
}
std::cout << std::endl;
}
}

int main()
{
std::vector<int> vobj;
std::cout << "Before calling fillVector(...): vobj contains "
<< vobj.size() << " values." << std::endl;

std::cout << "\nEnter # of random values you'd like to store in vobj: ";
int n;
std::cin >> n;

std::cout << "\n*** Calling fillVector(...) ***" << std::endl;
fillVector(vobj, n);

std::cout << "\n*** Calling displayVectorVer2(...) ***" << std::endl;
std::vector<int>::iterator ptr;
displayVectorVer2(vobj,ptr,n);
}
...

您也不应再在现代代码中使用 srand,因为它已使用了10年以上 since c++11 introduced <random>
冒昧可能会损害您的程序。如果用于生成敏感代码的种子。 srand也不提供应提供的“随机性” srand generates some numbers more often than others-这不是随机的。

关于c++ - 如何仅使用Iterator显示Vector的前10个元素?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61150861/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com