stl循环删除元素_使用C ++ STL中的每个循环访问矢量元素

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循环删除元素

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值