stl向量_在C ++ STL中更改向量的特定元素

stl向量

Given a C++ STL vector and we have to change a particular element.

给定一个C ++ STL向量,我们必须更改一个特定元素。

更改向量的特定元素 (Changing a particular element of a vector )

We can change a particular element of a C++ STL vector using following ways

我们可以使用以下方式更改C ++ STL向量的特定元素

  1. Using vector::at() function

    使用vector :: at()函数

  2. And, using vector::operator[]

    并且,使用vector :: operator []

Note: To use vector – include <vector> header, and to use vector::at() function and vector::operator[] – include <algorithm> header or we can simply use <bits/stdc++.h> header file.

注意:要使用vector –包含<vector>头文件,并使用vector :: at()函数和vector :: operator [] –包含<algorithm>头文件,或者我们可以简单地使用<bits / stdc ++。h>头文件。

C ++ STL程序更改矢量元素 (C++ STL program to change an element of a vector)

//C++ STL program to change an element of a vector

#include <iostream>
#include <vector>
using namespace std;

int main()
{
    vector<int> v1{ 10, 20, 30, 40, 50 };

    //printing elements
    cout << "vector elements before the change..." << endl;
    for (int x : v1)
        cout << x << " ";
    cout << endl;

    //changing element at index 1 using at()
    v1.at(1) = 100;
    //changing element at index 2 using operator[]
    v1[2] = 200;

    //printing elements
    cout << "vector elements after the change..." << endl;
    for (int x : v1)
        cout << x << " ";
    cout << endl;

    return 0;
}

Output

输出量

vector elements before the change...
10 20 30 40 50
vector elements after the change...
10 100 200 40 50


翻译自: https://www.includehelp.com/stl/changing-a-particular-element-of-a-vector-in-cpp-stl.aspx

stl向量

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值