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向量的特定元素
Using vector::at() function
And, using 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向量