stl vector 函数
打印向量的所有元素 (Printing all elements of a vector)
To print all elements of a vector, we can use two functions 1) vector::begin() and vector::end() functions.
要打印矢量的所有元素,我们可以使用两个函数:1) vector :: begin()和vector :: end()函数。
vector::begin() function returns an iterator pointing to the first elements of the vector.
vector :: begin()函数返回一个指向向量的第一个元素的迭代器。
vector::end() function returns an iterator point to past-the-end element of the vector.
vector :: end()函数将迭代器点返回到向量的past-the-end元素。
We run a loop from the first element to the less than past-the-element and prints the vector elements.
我们从第一个元素到小于过去的元素运行一个循环,并打印矢量元素。
Note: To use vector, include <vector> header.
注意:要使用向量,请包含<vector>标头。
C ++ STL程序打印矢量的所有元素 (C++ STL program to print all elements of a vector)
//C++ STL program to print all elements of a vector
#include <iostream>
#include <vector>
using namespace std;
int main()
{
vector<int> v1;
v1.push_back(10);
v1.push_back(20);
v1.push_back(30);
v1.push_back(40);
v1.push_back(50);
//creating iterator
vector<int>::iterator it;
//printing all elements
cout << "vector v1 elements are: ";
for (it = v1.begin(); it != v1.end(); it++)
cout << *it << " ";
cout << endl;
return 0;
}
Output
输出量
vector v1 elements are: 10 20 30 40 50
翻译自: https://www.includehelp.com/stl/printing-all-elements-of-a-vector-using-vector-begin-and-vector-end-functions.aspx
stl vector 函数