C++ vector::push_back 用法剖析
来源:互联网 发布:java编程思想 pdf 下载 编辑:程序博客网 时间:2024/05/29 10:09
C++编程语言中有一种叫做Vector的应用方法,它的作用在实际编程中是非常重要的。下面介绍一下C++ Vector的相关应用技巧及基本内容,希望能给大家带来一些帮助。
(1)vector< 类型 > 标识符 ;
(2)vector< 类型 > 标识符(最大容量) ;
(3)vector< 类型 > 标识符(最大容量,初始所有值);
(4) int i[4] = {12,3,4,5};
- vector< 类型 > vi(i , i+2); //得到i索引值为3以后的值 ;
(5)vector< vector<int> > //vi 定义2维的容器;记得一定要有空格,不然会报错
- vector< int > line
- // 在使用的时候一定要首先将vi个行进行初始化;
- for(int i = 0 ; i < 10 ; i ++)
- {
- vector.push_back(line);
- }
个人认为使用vector定义二维数组很好,因为长度可以不预先确定。很好!
(6)C++ Vector排序
- vector< int > vi ;
- vi.push_back(1);
- vi.push_back(3);
- vi.push_back(0);
- sort(vi.begin() , vi.end()); /// /从小到大
- reverse(vi.begin(),vi.end()) /// 从大到小
(7)顺序访问
- vector < int > vi ;
- for( int i = 0 ; i < 10 ; i ++)
- {
- vi.push_back(i);
- }
- for(int i = 0 ; i < 10 ; i ++) /// 第一种调用方法
- {
- cout <<vi[i] <<" " ;
- }
- for(vector<int>::iterator it = vi.begin() ;it !=vi.end() ; it++) ///第二种调用方法
- {
- cout << *it << " " ;
- }
(8)寻找
- vector < int > vi ;
- for( int i = 0 ; i < 10 ; i ++)
- {
- vector.push_back(i);
- }
- vector < int >::interator it = find(vi.begin() , vi.end,3) ;
- cout << *it << endl ; ///返回容器内找到值的位置。
(9)使用数组对C++ Vector进行初始化
- int i[10] ={1,2,3,4,5,6,7,78,8} ;
- ///第一种
- vector<int> vi(i+1,i+3); ///从第2个元素到第3个元素
- for(vector <int>::interator it = vi.begin() ; it != vi.end() ; it++)
- {
- cout << *it <<" " ;
- }
(10) 结构体类型
- struct temp
- {
- public :
- string str ;
- public :
- int id ;
- }tmp;
- int main()
- {
- vector <temp> t ;
- temp w1 ;
- w1.str = "Hello world" ;
- w1.id = 1 ;
- t.push_back(t1);
- cout << w1.str << "," <<w1.id <<endl ;
- return 0 ;
- }
C++ Vector的基本内容就为大家介绍到这里。
vector::push_back
public member function
void push_back ( const T& x );
Add element at the end
This effectively increases the vector size by one, which causes a reallocation of the internal allocated storage if the vector size was equal to the vector capacity before the call. Reallocations invalidate all previously obtained iterators, references and pointers.
Parameters
- x
- Value to be copied to the new element.
T is the first template parameter (the type of the elements stored in the vector).
Return value
noneIf a reallocation happens, it is performed using Allocator::allocate(), which may throw exceptions (for the defaultallocator,bad_alloc is thrown if the allocation request does not succeed).
Example
// vector::push_back#include <iostream>#include <vector>int main (){ std::vector<int> myvector; int myint; std::cout << "Please enter some integers (enter 0 to end):\n"; do { std::cin >> myint; myvector.push_back (myint); } while (myint); std::cout << "myvector stores " << int(myvector.size()) << " numbers.\n"; return 0;}
Complexity
Constant (amortized time, reallocation may happe 1 1
- C++ vector::push_back 用法剖析
- Vector push_back insert erase 用法
- vector push_back
- vector::push_back
- [C++]vector 的 push_back实现原理
- vector 的 push_back[转]
- vector::push_back函数解析
- 关于 vector push_back 实现
- vector的push_back
- C++ vector的push_back
- vector中的push_back()学习
- vector::push_back() demo
- vector::push_back 方法分析
- vector的push_back
- vector push_back和at
- vector push_back可能出错
- vector中的push_back
- C++ Vector用法深入剖析
- Python休眠、随机数
- 用PHP实现守护进程任务后台运行与多线程(php-resque使用说明)
- hdu1325 Is It A Tree?并查集
- FFmpeg SDK使用实例apiexample的分析
- ScrollView 之 实现视图的循环显示
- C++ vector::push_back 用法剖析
- 演练:ASP.NET控件实现 创建具有成员资格和用户登录功能的网站
- iOS网络编程(二) 自定义请求网络类----推荐用于需要请求过程片段数据
- VS2010 动态库开发演练:创建和使用动态链接库 (C++)(
- hdu 1863 畅通工程
- Java可变参数
- IMPORT command using the ADMIN_CMD procedure
- JavaWeb基础(复习1)
- http://wenku.baidu.com/link?url=cnaRe_jQ7OziHskrMgg5osYglrBRLZDTw-XD6HO4ZjbT60uiPisdolgz9KCfVuOb2qJM