C++ vector 和push_back 详解
2015-12-03 09:59
393 查看
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 ++)
{ vector.push_back(i); }
for(int i = 0 ; i < 10 ; i ++) /// 第一种调用方法
{ cout <<vector[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个元素到第三个元素
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 = "Hellowor" ;
w1.id = 1 ;
t.push_back(t1);
cout << w1.str << "," <<w1.id <<endl ;
return 0 ;
}
void push_back ( const T& x );
Add element at the end
Adds a new element at the end of the vector,after its current last element. The content of this new element is initializedto a copy of x.
Thiseffectively increases the vector size by
one, which causes a reallocation of the internalallocated storage if the vector size was
equal to the vector capacitybefore the call. Reallocations invalidate allpreviously
obtained iterators, references and pointers.
Value to be copied to the new element.
T is the first template parameter (the type of theelements stored in the vector).
If areallocation happens, it is performed using Allocator::allocate(), which may throw exceptions
(for thedefault allocator, bad_alloc is
thrown if the allocation request does notsucceed).
#include<iostream>
#include<vector>
intmain ()
{
std::vector<int>myvector;
int myint;
std::cout << "Pleaseenter 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;
}
The example uses push_back to add a new element
to the vector each time a new integeris read.
(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 ++)
{ vector.push_back(i); }
for(int i = 0 ; i < 10 ; i ++) /// 第一种调用方法
{ cout <<vector[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个元素到第三个元素
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 = "Hellowor" ;
w1.id = 1 ;
t.push_back(t1);
cout << w1.str << "," <<w1.id <<endl ;
return 0 ;
}
vector::push_back
public member functionvoid push_back ( const T& x );
Add element at the end
Adds a new element at the end of the vector,after its current last element. The content of this new element is initializedto a copy of x.
Thiseffectively increases the vector size by
one, which causes a reallocation of the internalallocated storage if the vector size was
equal to the vector capacitybefore the call. Reallocations invalidate allpreviously
obtained iterators, references and pointers.
Parameters
xValue to be copied to the new element.
T is the first template parameter (the type of theelements stored in the vector).
Return value
noneIf areallocation happens, it is performed using Allocator::allocate(), which may throw exceptions
(for thedefault allocator, bad_alloc is
thrown if the allocation request does notsucceed).
Example
//vector::push_back#include<iostream>
#include<vector>
intmain ()
{
std::vector<int>myvector;
int myint;
std::cout << "Pleaseenter 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;
}
The example uses push_back to add a new element
to the vector each time a new integeris read.
Complexity
Constant (amortized time, reallocation mayhappen).See also
vector::pop_back | Delete last element (public member function) | |
vector::insert | Insert elements (public member function) | |
相关文章推荐
- 使用C++实现JNI接口需要注意的事项
- 关于指针的一些事情
- c++ primer 第五版 笔记前言
- share_ptr的几个注意点
- Lua中调用C++函数示例
- Lua教程(一):在C++中嵌入Lua脚本
- Lua教程(二):C++和Lua相互传递数据示例
- C++联合体转换成C#结构的实现方法
- C++编写简单的打靶游戏
- C++ 自定义控件的移植问题
- C++变位词问题分析
- C/C++数据对齐详细解析
- C++基于栈实现铁轨问题
- C++中引用的使用总结
- 使用Lua来扩展C++程序的方法
- C++中调用Lua函数实例
- Lua和C++的通信流程代码实例
- C与C++之间相互调用实例方法讲解
- 解析C++中派生的概念以及派生类成员的访问属性
- C++ Custom Control控件向父窗体发送对应的消息