STL vector中的operator[]方法(31)
来源:互联网 发布:莹恋乒乓球总会淘宝店 编辑:程序博客网 时间:2024/05/18 02:34
std::vector::operator[]
reference operator[] (size_type n);const_reference operator[] (size_type n) const;
返回一个指向位置n的元素的引用。
一个相似的方法是at,和该方法具有一样的行为,不同的是at是检测边界的,并且当请求的位置超出范围后会抛出out_of_range异常。
Portable programs should never call this function with an argument n that is out of range, since this causes undefined behavior.
不应该在n超出范围的时候调用该方法,这会导致未知的错误。
例子:
#include <iostream>#include <vector>using namespace std;int main(){vector<int> vi({1,2,3,4,5});cout<<"vi[0]="<<vi[0]<<endl;cout<<"vi[10]="<<vi[10]<<endl;}结果截图:
这里的vi[10]虽然未定义,但是还是会有默认的值。但这是不对的。
Parameters
- n
- Position of an element in the container.
Notice that the first element has a position of 0 (not 1).
Member type size_type is an unsigned integral type.
n是元素在容器中的位置。
注意第一个元素的位置是0而不是1.
n是一个无符号整数。
Return value
The element at the specified position in the vector.返回值为在指定位置的元素的引用。
If the vector object is const-qualified, the function returns a const_reference. Otherwise, it returns a reference.
如果vector对象是const属性的,返回的引用也是const的,否则,返回一个普通的引用。
Member types reference and const_reference are the reference types to the elements of the container (see vector member types).
返回的引用类型与元素类型一致。
Example
1234567891011121314151617181920212223242526272829
// vector::operator[]#include <iostream>#include <vector>int main (){ std::vector<int> myvector (10); // 10 zero-initialized elements std::vector<int>::size_type sz = myvector.size(); // assign some values: for (unsigned i=0; i<sz; i++) myvector[i]=i; // reverse vector using operator[]: for (unsigned i=0; i<sz/2; i++) { int temp; temp = myvector[sz-1-i]; myvector[sz-1-i]=myvector[i]; myvector[i]=temp; } std::cout << "myvector contains:"; for (unsigned i=0; i<sz; i++) std::cout << ' ' << myvector[i]; std::cout << '\n'; return 0;}
Output:
myvector contains: 9 8 7 6 5 4 3 2 1 0
Complexity
Constant.Iterator validity
No changes.Data races
The container is accessed (neither the const nor the non-const versions modify the container).
容器将被访问。
The reference returned can be used to access or modify elements. Concurrently accessing or modifying different elements is safe.被引用的元素通常都将被访问或者修改,同时访问以及修改其他元素都是安全的。
Exception safety
If the container size is greater than n, the function never throws exceptions (no-throw guarantee).
如果容器的大小大于n,不会抛出异常。
Otherwise, the behavior is undefined.
否则,将导致未知的错误。
//翻译的不好的地方请多多指导,可以在下面留言或者点击左上方邮件地址给我发邮件,指出我的错误以及不足,以便我修改,更好的分享给大家,谢谢。
转载请注明出处:http://blog.csdn.net/qq844352155
2014-8-19
于GDUT
- STL vector中的operator[]方法(31)
- STL vector中的operator=方法(30)
- STL vector中的insert方法
- 实战c++中的vector系列--vector的遍历(stl算法、vector迭代器(不要在循环中判断不等于end())、operator[])
- STL vector中的begin方法(3)
- STL vector中的front方法(4)
- STL vector中的rbegin方法(5)
- STL vector中的cbegin()方法(6)
- STL vector中的crbegin方法(7)
- STL vector中的end方法(8)
- STL vector中的cend方法(9)
- STL vector中的rend方法(10)
- STL vector中的crend()方法(11)
- STL vector中的reserve()方法(15)
- STL vector中的push_back方法(17)
- STL vector中的clear方法(18)
- STL vector中的at方法(20)
- STL vector中的data方法(21)
- 使用ADF-BC开发业务服务
- hdu 4970
- 巧数之471
- edward_mj退役经验帖(浙大2届Final队员陈伟杰的退役贴)
- STDIN_FILENO的作用及与stdin 的区别
- STL vector中的operator[]方法(31)
- 初学maven(5)-使用assembly plugin实现自定义打包
- SQL Server用触发器强制执行业务规则
- Mac上面的AppStore更新Xcode 5.1.1 速度慢的问题
- Air 调用本地化java程序
- 使用ADF Faces 开发用户界面
- Jobdu 题目1144:Freckles
- C语言发送电子邮件
- hdoj.1303 Doubles 20140819