我想從迭代器對創建一個stl向量,我不確定向量可能有多少個元素。它可能只有一個元素。從一對迭代器創建stl向量C++
#include <iostream>
#include <vector>
int main()
{
using namespace std;
vector<int> vect;
for (int nCount=0; nCount < 6; nCount++)
vect.push_back(nCount);
vector<int> second(vect.begin(), vect.begin());
vector<int>::iterator it; // declare an read-only iterator
it = second.begin(); // assign it to the start of the vector
while (it != second.end()) // while it hasn't reach the end
{
cout << *it << " "; // print the value of the element it points to
it++; // and iterate to the next element
}
cout << endl;
}
我以爲vector'second'會有vect.begin()指向的一個元素。情況不是這樣嗎?
由於
您插入0元素... – PlasmaHH