stl循环删除元素
访问向量元素 (Accessing vector elements)
Here, we are going to learn by an example – how to access vector elements using for each loop?
在这里,我们将通过一个示例进行学习- 如何为每个循环使用矢量元素?
Note: There is no such for each loop in C++ STL, we can create it and access the elements by using the same type as the container type.
注意: C ++ STL中的每个循环都没有这样的代码 ,我们可以使用与容器类型相同的类型来创建它并访问元素。
Read more: for each (range based loop in C++ STL)
阅读更多: 每种内容(C ++ STL中基于范围的循环)
Syntax to access vector elements using for each kind of loop
用于每种循环的访问向量元素的语法
for(type variable_name : vector_name){
//print the element using variable_name
}
Here,
这里,
type – is the data type of the vector.
type –是向量的数据类型。
variable_name – is a temporary variable that which stores the elements one by one.
variable_name –是一个临时变量,它逐一存储元素。
vector_name – is the name of the vector.
vector_name –是向量的名称。
C ++ STL程序访问每个循环使用的向量元素 (C++ STL program to access vector elements using for each loop)
//C++ STL program to access vector elements
//using for each loop
#include <iostream>
#include <vector>
using namespace std;
int main()
{
//vector declaration
vector<int> v1;
//pushing the elements
v1.push_back(10);
v1.push_back(20);
v1.push_back(30);
v1.push_back(40);
v1.push_back(50);
//printing the vector elements
//using for each kind of loop
cout << "Vector v1 elements are: ";
for (int element : v1)
cout << element << " ";
cout << endl;
return 0;
}
Output
输出量
Vector v1 elements are: 10 20 30 40 50
翻译自: https://www.includehelp.com/stl/access-vector-elements-using-for-each-loop.aspx
stl循环删除元素