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 函數