文章目录
vector容器 1.构造函数
2.赋值
3.容量和大小
4.插入和删除
5.数据存取
6.互换容器
7.预留空间
vector容器
1.构造函数
代码工程
# define _CRT_SECURE_NO_WARNINGS
# include <iostream>
# include <algorithm>
# include <string>
# include <vector>
using namespace std;
void printVector ( const vector< int > & v)
{
for ( vector< int > :: const_iterator it = v. begin ( ) ; it != v. end ( ) ; it++ )
{
cout << * it << " " ;
}
cout << endl;
}
void test01 ( )
{
vector< int > v1;
for ( int i = 0 ; i < 5 ; i++ )
{
v1. push_back ( i) ;
}
cout << "v1容器的数据: " ;
printVector ( v1) ;
vector< int > v2 ( v1. begin ( ) , v1. end ( ) ) ;
cout << "v2容器的数据: " ;
printVector ( v2) ;
vector< int > v3 ( 5 , 100 ) ;
cout << "v3容器的数据: " ;
printVector ( v3) ;
vector< int > v4 ( v3) ;
cout << "v4容器的数据: " ;
printVector ( v4) ;
return ;
}
int main ( )
{
test01 ( ) ;
return 0 ;
}
运行结果
2.赋值
代码工程
# define _CRT_SECURE_NO_WARNINGS
# include <iostream>
# include <vector>
using namespace std;
void printVector ( const vector< int > & v)
{
for ( vector< int > :: const_iterator it = v. begin ( ) ; it != v. end ( ) ; it++ )
{
cout << * it << " " ;
}
cout << endl;
return ;
}
void test ( )
{
vector< int > v1;
for ( int i = 0 ; i < 5 ; i++ )
{
v1. push_back ( i) ;
}
cout << "v1容器的数据: " ;
printVector ( v1) ;
vector< int > v2;
v2 = v1;
cout << "v2容器的数据: " ;
printVector ( v2) ;
vector< int > v3;
v3. assign ( v2. begin ( ) , v2. end ( ) ) ;
cout << "v3容器的数据: " ;
printVector ( v3) ;
vector< int > v4;
v4. assign ( 5 , 200 ) ;
cout << "v4容器的数据: " ;
printVector ( v4) ;
return ;
}
int main ( )
{
test ( ) ;
return 0 ;
}
运行结果
3.容量和大小
代码工程
# define _CRT_SECURE_NO_WARNINGS
# include <iostream>
# include <vector>
using namespace std;
void printVector ( const vector< int > & v)
{
for ( vector< int > :: const_iterator it = v. begin ( ) ; it != v. end ( ) ; it++ )
{
cout << * it << " " ;
}
cout << endl;
return ;
}
void test ( )
{
vector< int > v1;
for ( int i = 0 ; i < 10 ; i++ )
{
v1. push_back ( i) ;
}
cout << "v1容器的数据: " ;
printVector ( v1) ;
if ( v1. empty ( ) )
{
cout << "v1容器为空" << endl;
return ;
}
cout << "v1的容量为:" << v1. capacity ( ) << endl;
cout << "v1的大小为:" << v1. size ( ) << endl;
cout << "v1重新指定长度为15时 :" ;
v1. resize ( 15 ) ;
printVector ( v1) ;
cout << "v1重新指定长度为5时 :" ;
v1. resize ( 5 ) ;
printVector ( v1) ;
return ;
}
int main ( )
{
test ( ) ;
return 0 ;
}
运行结果
4.插入和删除
代码工程
# define _CRT_SECURE_NO_WARNINGS
# include <iostream>
# include <vector>
using namespace std;
void printVector ( const vector< int > & v)
{
for ( vector< int > :: const_iterator it = v. begin ( ) ; it != v. end ( ) ; it++ )
{
cout << * it << " " ;
}
cout << endl;
}
void test ( )
{
vector< int > v1;
v1. push_back ( 10 ) ;
v1. push_back ( 20 ) ;
v1. push_back ( 30 ) ;
v1. push_back ( 40 ) ;
v1. push_back ( 50 ) ;
cout << "v1容器的数据: " ;
printVector ( v1) ;
v1. pop_back ( ) ;
cout << "尾删后v1容器中的数据: " ;
printVector ( v1) ;
v1. insert ( v1. begin ( ) , 1000 ) ;
cout << "插入后v1容器中的数据: " ;
printVector ( v1) ;
v1. insert ( v1. begin ( ) , 2 , 2000 ) ;
cout << "插入后v1容器中的数据: " ;
printVector ( v1) ;
v1. erase ( v1. begin ( ) ) ;
cout << "删除后v1容器中的数据: " ;
printVector ( v1) ;
v1. erase ( v1. begin ( ) , v1. end ( ) ) ;
if ( v1. empty ( ) )
{
cout << "当前容器为空!" << endl;
}
v1. push_back ( 10 ) ;
v1. push_back ( 20 ) ;
printVector ( v1) ;
v1. clear ( ) ;
cout << "清空v1容器中的数据: " ;
if ( v1. empty ( ) )
{
cout << "当前容器为空!" << endl;
}
return ;
}
int main ( )
{
test ( ) ;
return 0 ;
}
运行结果
5.数据存取
工程代码
# define _CRT_SECURE_NO_WARNINGS
# include <iostream>
# include <vector>
using namespace std;
void test ( )
{
vector< int > v1;
v1. push_back ( 10 ) ;
v1. push_back ( 20 ) ;
v1. push_back ( 30 ) ;
v1. push_back ( 40 ) ;
v1. push_back ( 50 ) ;
cout << "利用[]访问v1容器的数据: " ;
for ( int i = 0 ; i < v1. size ( ) ; i++ )
{
cout << v1[ i] << " " ;
}
cout << endl;
cout << "利用at访问v1容器的数据: " ;
for ( int i = 0 ; i < v1. size ( ) ; i++ )
{
cout << v1. at ( i) << " " ;
}
cout << endl;
cout << "访问v1容器的第一个数据: " ;
cout << v1. front ( ) << endl;
cout << "访问v1容器的最后一个数据: " ;
cout << v1. back ( ) << endl;
return ;
}
int main ( )
{
test ( ) ;
return 0 ;
}
运行结果
6.互换容器
代码工程
# define _CRT_SECURE_NO_WARNINGS
# include <iostream>
# include <vector>
using namespace std;
void printVector ( const vector< int > & v)
{
for ( vector< int > :: const_iterator it = v. begin ( ) ; it != v. end ( ) ; it++ )
{
cout << * it << " " ;
}
cout << endl;
}
void test ( )
{
vector< int > v1;
v1. push_back ( 10 ) ;
v1. push_back ( 20 ) ;
v1. push_back ( 30 ) ;
v1. push_back ( 40 ) ;
v1. push_back ( 50 ) ;
cout << "v1容器的数据: " ;
printVector ( v1) ;
vector< int > v2;
v2. push_back ( 100 ) ;
v2. push_back ( 200 ) ;
v2. push_back ( 300 ) ;
v2. push_back ( 400 ) ;
v2. push_back ( 500 ) ;
cout << "v2容器的数据: " ;
printVector ( v2) ;
cout << "-----------交换容器的数据后-----------" << endl;
v1. swap ( v2) ;
cout << "v1容器的数据: " ;
printVector ( v1) ;
cout << "v2容器的数据: " ;
printVector ( v2) ;
return ;
}
void test01 ( )
{
vector< int > v;
for ( int i = 0 ; i < 100000 ; i++ )
{
v. push_back ( i) ;
}
cout << "v容器的容量:" << v. capacity ( ) << endl;
cout << "v容器的大小:" << v. size ( ) << endl;
v. resize ( 5 ) ;
cout << "重新指定v容器的大小: " << endl;
cout << "v容器的容量:" << v. capacity ( ) << endl;
cout << "v容器的大小:" << v. size ( ) << endl;
cout << "收缩v容器的大小: " << endl;
vector< int > ( v) . swap ( v) ;
cout << "v容器的容量:" << v. capacity ( ) << endl;
cout << "v容器的大小:" << v. size ( ) << endl;
return ;
}
int main ( )
{
test ( ) ;
cout << endl;
cout << "swap的实际作用" << endl;
test01 ( ) ;
return 0 ;
}
运行结果
7.预留空间
代码工程
# define _CRT_SECURE_NO_WARNINGS
# include <iostream>
# include <vector>
using namespace std;
void printVector ( const vector< int > & v)
{
for ( vector< int > :: const_iterator it = v. begin ( ) ; it != v. end ( ) ; it++ )
{
cout << * it << " " ;
}
cout << endl;
}
void test ( )
{
vector< int > v1;
int * p = NULL ;
int num = 0 ;
for ( int i = 0 ; i < 100000 ; i++ )
{
v1. push_back ( i) ;
if ( p != & v1[ 0 ] )
{
num++ ;
p = & v1[ 0 ] ;
}
}
cout << "v1容器重新开辟空间的次数:" << num << endl;
return ;
}
void test01 ( )
{
vector< int > v1;
int * p = NULL ;
int num = 0 ;
v1. reserve ( 100000 ) ;
p = NULL ;
for ( int i = 0 ; i < 100000 ; i++ )
{
v1. push_back ( i) ;
if ( p != & v1[ 0 ] )
{
num++ ;
p = & v1[ 0 ] ;
}
}
cout << "预留空间后v1容器重新开辟空间的次数:" << num << endl;
return ;
}
int main ( )
{
test ( ) ;
cout << endl;
test01 ( ) ;
return 0 ;
}
运行结果