std::vector的一些使用注意事项
2010-09-05 20:36
651 查看
使用at()函数而不是operator[]:理由是at()可以抛出invalid vector[T] subscript异常,而operator[]不会做范围检查。因此,at()函数更加安全。
使用vector的assign函数复制一个vector:
使用vector的assign函数复制一个vector:
include "stdafx.h" #include <vector> #include <iostream> int main( ) { using namespace std; vector<int> v1, v2, v3; vector<int>::iterator iter; v1.push_back(10); v1.push_back(20); v1.push_back(30); v1.push_back(40); v1.push_back(50); cout << "v1 = " ; for (iter = v1.begin(); iter != v1.end(); iter++) cout << *iter << " "; cout << endl; v2.assign(v1.begin(), v1.end()); cout << "v2 = "; for (iter = v2.begin(); iter != v2.end(); iter++) cout << *iter << " "; cout << endl; v3.assign(7, 4) ; cout << "v3 = "; for (iter = v3.begin(); iter != v3.end(); iter++) cout << *iter << " "; cout << endl; }
相关文章推荐
- std::vector的一些使用注意事项
- 使用string与vector时的一些注意事项
- std::vector 使用&&注意事项
- std_vector使用注意事项
- 使用vector容器存储动态申请内存变量的注意事项
- Ionic2使用百度地图和html5 geolocation的一些注意事项
- [转]使用成员函数指针的一些注意事项
- 项目中svn使用的一些注意事项(新手)
- GridView 使用的一些注意事项
- 聊下 git 使用前的一些注意事项
- Android dp方式的屏幕适配工具使用(一些疑惑和注意事项)
- 关于scrapy爬虫使用的一些小经验和注意事项
- 关于Runtime Permissions的学习以及使用的一些注意事项
- css属性使用中的一些注意事项(实战经验)
- Unity中使用SLua的一些注意事项
- ios storyboard使用的一些注意事项
- 博客园IT新闻客户端使用腾讯微博SDK静态库的一些注意事项
- 使用MatLab过程中的一些注意事项
- LigerUI使用的一些注意事项
- 关于使用存储过程的一些好处以及注意事项