欢迎访问 生活随笔!

生活随笔

当前位置: 首页 > 编程资源 > 编程问答 >内容正文

编程问答

stl vector 函数_在C ++ STL中使用vector :: begin()和vector :: end()函数打印矢量的所有元素...

发布时间:2023/12/1 编程问答 43 豆豆
生活随笔 收集整理的这篇文章主要介绍了 stl vector 函数_在C ++ STL中使用vector :: begin()和vector :: end()函数打印矢量的所有元素... 小编觉得挺不错的,现在分享给大家,帮大家做个参考.

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 iteratorvector<int>::iterator it;//printing all elementscout << "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 函数

总结

以上是生活随笔为你收集整理的stl vector 函数_在C ++ STL中使用vector :: begin()和vector :: end()函数打印矢量的所有元素...的全部内容,希望文章能够帮你解决所遇到的问题。

如果觉得生活随笔网站内容还不错,欢迎将生活随笔推荐给好友。