stl向量
从向量访问元素的不同方法 (Different ways to access elements from a vector)
1)使用[]运算子 (1) Using [] operator)
Syntax:
句法:
vector_name[index]
Parameter: index – is the position in vector.(0-based indexing)
参数: index –是向量中的位置。(从0开始的索引)
Return Value: The element at that given index.
返回值:给定索引处的元素。
Example:
例:
vector<int> a;
a.push_back(1);
a.push_back(2);
a.push_back(3);
int p=a[0]; //p=1
int q=a[1]; //q=2
a[n] for any n out of index bound it reflects undefined behavior (depends on compiler).
对于索引范围外的任何n , a [n]反映未定义的行为(取决于编译器)。
2)使用函数.at(index) (2) Using function .at(index))
Syntax:
句法:
vector_name.at(pos_n)
Parameter: index of desired element
参数:所需元素的索引
Return value: Returns the referenced element present at input index.
返回值:返回输入索引处存在的引用元素。
It returns a reference to the element and this function automatically checks whether n is within the range or not. In case of exception it raises out of index range error.
它返回对该元素的引用,并且此函数自动检查n是否在范围内。 如果出现异常,则会超出索引范围错误。
That's the difference between .at() and [] operator does not keep any check for out of the index bounds.
那就是.at()和[]运算符之间的区别,它不会使任何检查超出索引范围。
Example:
例:
//On same vector a
int p=a.at(0); //p=1
int q=a.at(1); //q=2
a.at(4) //compilation error
3)front()函数 (3) front() function)
Syntax:
句法:
vector_name.front()
Parameter: None
参数:无
Return value: Return the reference of first element of the vector.
返回值:返回向量的第一个元素的引用。
Example:
例:
//On same vector a
int p=a.front() //p=1
Note: Calling this function on an empty vector shows undefined behavior.
注意:在空向量上调用此函数将显示未定义的行为。
4)back()函数 (4) back() function)
Syntax:
句法:
vector_name.back()
Parameter: None
参数:无
Return: Reference of last element.
返回值:最后一个元素的引用。
Example:
例:
//On same vector a
int q=a.back() //q=3
Note: Calling this function on an empty vector shows undefined behavior.
注意:在空向量上调用此函数将显示未定义的行为。
C++ code to demonstrate example of the functions to access vector elements
C ++代码演示访问向量元素的功能示例
// C++ program to show
// how to access elements in vector in C++ STL>
#include <vector>
#include <iostream>
using namespace std;
int main( )
{
// declaring vector n
vector<int>n{1,2,3,4,5,6,7,8,9,0};
/* This is how the operator[i]
in c++ works i is the index
which is changing continuously
i.e printing the vector is best example*/
cout <<"Output of operator[]: \n";
for(int i=0; i<n.size(); i++)
cout<<n[i]<<" ";
/* This is how at() works similar
to the operator[] ,
Here it changes the whole vector
*/
cout << "\n\nOutput of at(): \n";
for(int i=0; i<n.size(); i++)
{
n.at(i)=i;
cout<<n.at(i)<<" ";
}
cout << "\n\nOutput of change vector because of at(): \n";
for(int i=0; i<n.size(); i++)
cout<<n[i]<<" ";
/*This is how the front()
works by using that here we are
accessing the front element of vector
*/
cout << "\n\nOutput of front(): \n";
cout<<n.front();
/*This is how the back()
works by using that here we are
accessing the back element of vector
*/
cout << "\n\nOutput of back(): \n";
cout<<n.back();
return 0;
}
Output
输出量
Output of operator[]:
1 2 3 4 5 6 7 8 9 0
Output of at():
0 1 2 3 4 5 6 7 8 9
Output of change vector because of at():
0 1 2 3 4 5 6 7 8 9
Output of front():
0
Output of back():
9
翻译自: https://www.includehelp.com/stl/different-ways-to-access-elements-from-a-vector-in-cpp-stl.aspx
stl向量