STL 中的back()方法(12)
2014-08-12 16:15
369 查看
原文地址:http://www.cplusplus.com/reference/vector/vector/back/
front和back我觉得是一对,一个头,一个尾,属性都差不多,详见:http://blog.csdn.net/qq844352155/article/details/38458047
public member function
<vector>
Access last element
Returns a reference to the last element in the vector.
返回最后一个元素的引用。
Unlike member vector::end, which returns an iterator just past this element, this function returns
a direct reference.
和end不一样的是,end是返回一个指向超尾元素的迭代器,而这个函数是直接返回一个引用。
Calling this function on an empty container causes undefined behavior.
对一个空的容器调用该方法会导致未知的行为。
例子:
#include <iostream>
#include <vector>
#include <iterator>
using namespace std;
int main()
{
vector<int> vi;
cout<<vi.back()<<endl;
}
在linux下g++编译运行的结果
none
A reference to the last element in the vector.
返回值是一个指向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 vector (see member
types).
返回值都是属于引用类型。
Output:
Constant.
No changes.
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.
返回的引用可以用来访问或者是修改元素,并且这些操作都是安全的。
If the container is not empty, the function never throws exceptions (no-throw guarantee).
如果容器非空,该方法不会抛出异常。
Otherwise, it causes undefined behavior.
如果容器为空,该方法会导致未定义的行为。
//翻译的不好的地方请多多指导,可以在下面留言或者点击左上方邮件地址给我发邮件,指出我的错误以及不足,以便我修改,更好的分享给大家,谢谢。
转载请注明出处:http://blog.csdn.net/qq844352155
2014-8-12
于GDUT
front和back我觉得是一对,一个头,一个尾,属性都差不多,详见:http://blog.csdn.net/qq844352155/article/details/38458047
public member function
<vector>
std::vector::back
reference back(); const_reference back() const;
Access last element
Returns a reference to the last element in the vector.
返回最后一个元素的引用。
Unlike member vector::end, which returns an iterator just past this element, this function returns
a direct reference.
和end不一样的是,end是返回一个指向超尾元素的迭代器,而这个函数是直接返回一个引用。
Calling this function on an empty container causes undefined behavior.
对一个空的容器调用该方法会导致未知的行为。
例子:
#include <iostream>
#include <vector>
#include <iterator>
using namespace std;
int main()
{
vector<int> vi;
cout<<vi.back()<<endl;
}
在linux下g++编译运行的结果
Parameters
none
Return value
A reference to the last element in the vector.返回值是一个指向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 vector (see member
types).
返回值都是属于引用类型。
Example
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 | // vector::back #include <iostream> #include <vector> int main () { std::vector<int> myvector; myvector.push_back(10); while (myvector.back() != 0) { myvector.push_back ( myvector.back() -1 ); } std::cout << "myvector contains:"; for (unsigned i=0; i<myvector.size() ; i++) std::cout << ' ' << myvector[i]; std::cout << '\n'; return 0; } | Edit & Run |
myvector contains: 10 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 is not empty, the function never throws exceptions (no-throw guarantee).如果容器非空,该方法不会抛出异常。
Otherwise, it causes undefined behavior.
如果容器为空,该方法会导致未定义的行为。
//翻译的不好的地方请多多指导,可以在下面留言或者点击左上方邮件地址给我发邮件,指出我的错误以及不足,以便我修改,更好的分享给大家,谢谢。
转载请注明出处:http://blog.csdn.net/qq844352155
2014-8-12
于GDUT
相关文章推荐
- C++/STL_中的push_back方法与复制数据的问题
- STL array的back方法(3)
- STL array的fill方法(12)
- STL序列式容器中删除元素的方法和陷阱 一
- STL序列式容器中删除元素的方法和陷阱三
- (原創) 將map輸出到cout,是否有更方便的方法? (C/C++) (STL)
- 设计模式(12)-模板方法模式(Template Method)
- STL序列式容器中删除元素的方法和陷阱二
- DOS命令中的空格字段處理方法12/30
- Sybase Power Designer 12 注册方法/注册码
- BackroundWorker.DoWork方法中访问Word对象导致异常
- STL中erase方法对链表类容器的影响
- STL中string没有类似 MFC 中 CString 类的Format方法,怎么办?
- (转载)PowerDesigner 12 通用破解方法
- 用 Installshield 12 Premier 制作 MSDE2000 SP4 安装包方法和步骤
- 马宁伟-20年工作经验谈-12-管理方法
- STL序列式容器中删除元素的方法和陷阱 二
- STL序列式容器中删除元素的方法和陷阱一
- STL序列式容器中删除元素的方法和陷阱 二
- PowerDesigner 12 通用破解方法