vector
初始化,基本属性,访问,删除最后一个元素
vector< int > vec;
cout<< "vector size = " << vec. size ( ) << endl;
cout<< "vector capcity = " << vec. capacity ( ) << endl;
for ( int i= 0 ; i< 5 ; i++ )
{
vec. push_back ( i) ;
}
cout<< "extent vector size = " << vec. size ( ) << endl;
cout<< "extent vector capcity = " << vec. capacity ( ) << endl;
for ( int i= 0 ; i< 5 ; i++ )
{
cout<< "value of vec [" << i<< "] is " << vec[ i] << endl;
}
for ( vector< int > :: iterator v= vec. begin ( ) ; v!= vec. end ( ) ; v++ )
{
cout<< * v<< endl;
}
vec. pop_back ( ) ;
cout<< "the size of vec after pop_back : " << vec. size ( ) << endl;
vec_insert ( ) ;
vec_del ( ) ;
插入
void vec_insert ( )
{
vector< char > alphaVector;
for ( int i= 0 ; i< 10 ; i++ )
{
alphaVector. push_back ( i+ 65 ) ;
}
vector< char > :: iterator i= alphaVector. begin ( ) ;
alphaVector. insert ( i+ 1 , 4 , 'C' ) ;
for ( i= alphaVector. begin ( ) ; i!= alphaVector. end ( ) ; i++ )
cout<< * i;
cout<< endl;
sort ( alphaVector. begin ( ) , alphaVector. end ( ) ) ;
for ( i= alphaVector. begin ( ) ; i!= alphaVector. end ( ) ; i++ )
cout<< * i;
cout<< endl;
cout<< endl;
}
删除
void vec_del ( )
{
vector< char > alphaVector;
for ( int i= 0 ; i<= 10 ; i++ )
{
alphaVector. push_back ( i+ 65 ) ;
}
int size= alphaVector. size ( ) ;
vector< char > :: iterator start;
vector< char > :: iterator temp;
for ( temp= alphaVector. begin ( ) ; temp!= alphaVector. end ( ) ; temp++ )
cout<< * temp;
cout<< endl;
for ( int i= 0 ; i< size; i++ )
{
start= alphaVector. begin ( ) ;
alphaVector. erase ( start) ;
for ( temp= alphaVector. begin ( ) ; temp!= alphaVector. end ( ) ; temp++ )
cout<< * temp;
cout<< endl;
}
}
stack
入栈,取栈顶元素,出栈
stack< int > my_stack;
my_stack. push ( 1 ) ;
my_stack. push ( 2 ) ;
my_stack. push ( 3 ) ;
cout<< "size of mystack: " << my_stack. size ( ) << endl;
while ( ! my_stack. empty ( ) )
{
cout<< my_stack. top ( ) << endl;
my_stack. pop ( ) ;
}
list
初始化,访问,插入,删除
std:: list< double > values;
values. push_back ( 3.1 ) ;
values. push_back ( 2.2 ) ;
values. push_back ( 2.9 ) ;
cout<< "values size: " << values. size ( ) << endl;
values. sort ( ) ;
for ( std:: list< double > :: iterator it= values. begin ( ) ; it!= values. end ( ) ; it++ )
cout<< * it<< " " ;
cout<< endl;
list< double > :: iterator begin= values. begin ( ) ;
begin++ ;
values. insert ( begin, 5.6 ) ;
for ( std:: list< double > :: iterator it= values. begin ( ) ; it!= values. end ( ) ; it++ )
cout<< * it<< " " ;
cout<< endl;
list< double > :: iterator end= values. end ( ) ;
end-- ;
values. erase ( end) ;
for ( std:: list< double > :: iterator it= values. begin ( ) ; it!= values. end ( ) ; it++ )
cout<< * it<< " " ;
cout<< endl;
string
.substr()生成子串
void str_substr ( )
{
string s= "abcdefg" ;
string s2= s. substr ( 1 , 5 ) ;
cout<< s2<< endl;
string s3= s. substr ( 4 ) ;
cout<< s3<< endl;
}
.find() .find_first_of()串中字符查找
void str_find ( )
{
string s1 ( "Source Code" ) ;
int n;
if ( ( n= s1. find ( 'u' ) ) != string:: npos)
cout<< "1) " << n<< "," << s1. substr ( n) << endl;
if ( ( n= s1. find ( "Source" , 1 ) ) == string:: npos)
cout<< "2) " << "Not Found" << endl;
if ( ( n= s1. find ( "Source" , 0 ) ) == string:: npos)
cout<< "2) " << "Not Found" << endl;
else
cout<< "2) " << n<< "," << s1. substr ( n) << endl;
if ( ( n= s1. find_first_of ( "ceo" ) ) != string:: npos)
cout<< "4) " << n<< "," << s1. substr ( n) << endl;
if ( ( n= s1. find_last_of ( 'e' ) ) != string:: npos)
cout<< "5) " << n<< "," << s1. substr ( n) << endl;
if ( ( n= s1. find_first_not_of ( "ceo" , 1 ) ) != string:: npos)
cout<< "4) " << n<< "," << s1. substr ( n) << endl;
}
.replace()子串替换
void str_replace ( )
{
string s1 ( "Real steel" ) ;
s1. replace ( 1 , 3 , "123456" , 2 , 4 ) ;
cout<< s1<< endl;
string s2 ( "Harry Potter" ) ;
s2. replace ( 2 , 3 , 5 , '0' ) ;
cout<< s2<< endl;
int n= s2. find ( "00000" ) ;
s2. replace ( n, 5 , "XXX" ) ;
cout<< s2<< endl;
}
.erase()删除子串
void str_erase ( )
{
string s1 ( "Real Steel" ) ;
s1. erase ( 1 , 3 ) ;
cout<< s1<< endl;
s1. erase ( 5 ) ;
cout<< s1<< endl;
}
.insert()插入子串
void str_insert ( )
{
string s1 ( "Limitless" ) ;
string s2 ( "00" ) ;
s1. insert ( 2 , "123" ) ;
cout<< s1<< endl;
s1. insert ( 3 , s2) ;
cout<< s1<< endl;
s1. insert ( 3 , 5 , 'X' ) ;
cout<< s1<< endl;
}
串的深拷贝
string s;
string s1 ( "ssss" ) ;
string s2 ( s1) ;
s1[ 0 ] = 'b' ;
cout<< s1<< endl;
cout<< s2<< endl;
string s3= s2;
set
set< int > ss;
ss. insert ( 1 ) ;
ss. insert ( 2 ) ;
ss. insert ( 3 ) ;
ss. insert ( 1 ) ;
cout<< "size of s = " << ss. size ( ) << endl;
cout<< "maxsize of s = " << ss. max_size ( ) << endl;
cout<< "first element of s = " << * ss. begin ( ) << endl;
cout<< "last element of s = " << * ss. end ( ) << endl;
map
map< int , string> mapStudent;
mapStudent. insert ( pair < int , string> ( 1 , "student_one" ) ) ;
mapStudent. insert ( pair < int , string> ( 2 , "student_two" ) ) ;
mapStudent. insert ( pair < int , string> ( 3 , "student_three" ) ) ;
mapStudent. insert ( pair < int , string> ( 1 , "student_three" ) ) ;
cout<< mapStudent. size ( ) << endl;
for ( map< int , string> :: iterator it= mapStudent. begin ( ) ; it!= mapStudent. end ( ) ; it++ )
cout<< it-> first<< " " << it-> second<< endl;
map< int , string> :: iterator iter;
iter= mapStudent. find ( 1 ) ;
cout<< iter-> second<< endl;
mapStudent. erase ( iter) ;
int n= mapStudent. erase ( 1 ) ;
cout<< n;
mapStudent. erase ( mapStudent. begin ( ) , mapStudent. end ( ) ) ;
完整代码
# include <iostream>
# include <algorithm>
# include <stack>
# include <list>
# include <vector>
# include <string.h>
# include <set>
# include <map>
using namespace std;
void modifynum ( int & b)
{
b++ ;
}
void modify_pointer ( char * & p)
{
p= ( char * ) malloc ( 20 ) ;
strcpy ( p, "hello" ) ;
}
void vec_insert ( )
{
vector< char > alphaVector;
for ( int i= 0 ; i< 10 ; i++ )
{
alphaVector. push_back ( i+ 65 ) ;
}
vector< char > :: iterator i= alphaVector. begin ( ) ;
alphaVector. insert ( i+ 1 , 4 , 'C' ) ;
for ( i= alphaVector. begin ( ) ; i!= alphaVector. end ( ) ; i++ )
cout<< * i;
cout<< endl;
sort ( alphaVector. begin ( ) , alphaVector. end ( ) ) ;
for ( i= alphaVector. begin ( ) ; i!= alphaVector. end ( ) ; i++ )
cout<< * i;
cout<< endl;
cout<< endl;
}
void vec_del ( )
{
vector< char > alphaVector;
for ( int i= 0 ; i<= 10 ; i++ )
{
alphaVector. push_back ( i+ 65 ) ;
}
int size= alphaVector. size ( ) ;
vector< char > :: iterator start;
vector< char > :: iterator temp;
for ( temp= alphaVector. begin ( ) ; temp!= alphaVector. end ( ) ; temp++ )
cout<< * temp;
cout<< endl;
for ( int i= 0 ; i< size; i++ )
{
start= alphaVector. begin ( ) ;
alphaVector. erase ( start) ;
for ( temp= alphaVector. begin ( ) ; temp!= alphaVector. end ( ) ; temp++ )
cout<< * temp;
cout<< endl;
}
}
void str_substr ( )
{
string s= "abcdefg" ;
string s2= s. substr ( 1 , 5 ) ;
cout<< s2<< endl;
string s3= s. substr ( 4 ) ;
cout<< s3<< endl;
}
void str_find ( )
{
string s1 ( "Source Code" ) ;
int n;
if ( ( n= s1. find ( 'u' ) ) != string:: npos)
cout<< "1) " << n<< "," << s1. substr ( n) << endl;
if ( ( n= s1. find ( "Source" , 1 ) ) == string:: npos)
cout<< "2) " << "Not Found" << endl;
if ( ( n= s1. find ( "Source" , 0 ) ) == string:: npos)
cout<< "2) " << "Not Found" << endl;
else
cout<< "2) " << n<< "," << s1. substr ( n) << endl;
if ( ( n= s1. find_first_of ( "ceo" ) ) != string:: npos)
cout<< "4) " << n<< "," << s1. substr ( n) << endl;
if ( ( n= s1. find_last_of ( 'e' ) ) != string:: npos)
cout<< "5) " << n<< "," << s1. substr ( n) << endl;
if ( ( n= s1. find_first_not_of ( "ceo" , 1 ) ) != string:: npos)
cout<< "4) " << n<< "," << s1. substr ( n) << endl;
}
void str_replace ( )
{
string s1 ( "Real steel" ) ;
s1. replace ( 1 , 3 , "123456" , 2 , 4 ) ;
cout<< s1<< endl;
string s2 ( "Harry Potter" ) ;
s2. replace ( 2 , 3 , 5 , '0' ) ;
cout<< s2<< endl;
int n= s2. find ( "00000" ) ;
s2. replace ( n, 5 , "XXX" ) ;
cout<< s2<< endl;
}
void str_erase ( )
{
string s1 ( "Real Steel" ) ;
s1. erase ( 1 , 3 ) ;
cout<< s1<< endl;
s1. erase ( 5 ) ;
cout<< s1<< endl;
}
void str_insert ( )
{
string s1 ( "Limitless" ) ;
string s2 ( "00" ) ;
s1. insert ( 2 , "123" ) ;
cout<< s1<< endl;
s1. insert ( 3 , s2) ;
cout<< s1<< endl;
s1. insert ( 3 , 5 , 'X' ) ;
cout<< s1<< endl;
}
int main ( )
{
int b= 10 ;
char * p= NULL ;
modifynum ( b) ;
modify_pointer ( p) ;
stack< int > my_stack;
my_stack. push ( 1 ) ;
my_stack. push ( 2 ) ;
my_stack. push ( 3 ) ;
cout<< "size of mystack: " << my_stack. size ( ) << endl;
while ( ! my_stack. empty ( ) )
{
cout<< my_stack. top ( ) << endl;
my_stack. pop ( ) ;
}
vector< int > vec;
cout<< "vector size = " << vec. size ( ) << endl;
cout<< "vector capcity = " << vec. capacity ( ) << endl;
for ( int i= 0 ; i< 5 ; i++ )
{
vec. push_back ( i) ;
}
cout<< "extent vector size = " << vec. size ( ) << endl;
cout<< "extent vector capcity = " << vec. capacity ( ) << endl;
for ( int i= 0 ; i< 5 ; i++ )
{
cout<< "value of vec [" << i<< "] is " << vec[ i] << endl;
}
for ( vector< int > :: iterator v= vec. begin ( ) ; v!= vec. end ( ) ; v++ )
{
cout<< * v<< endl;
}
vec. pop_back ( ) ;
cout<< "the size of vec after pop_back : " << vec. size ( ) << endl;
vec_insert ( ) ;
vec_del ( ) ;
std:: list< double > values;
values. push_back ( 3.1 ) ;
values. push_back ( 2.2 ) ;
values. push_back ( 2.9 ) ;
cout<< "values size: " << values. size ( ) << endl;
values. sort ( ) ;
for ( std:: list< double > :: iterator it= values. begin ( ) ; it!= values. end ( ) ; it++ )
cout<< * it<< " " ;
cout<< endl;
list< double > :: iterator begin= values. begin ( ) ;
begin++ ;
values. insert ( begin, 5.6 ) ;
for ( std:: list< double > :: iterator it= values. begin ( ) ; it!= values. end ( ) ; it++ )
cout<< * it<< " " ;
cout<< endl;
list< double > :: iterator end= values. end ( ) ;
end-- ;
values. erase ( end) ;
for ( std:: list< double > :: iterator it= values. begin ( ) ; it!= values. end ( ) ; it++ )
cout<< * it<< " " ;
cout<< endl;
string s;
string s1 ( "ssss" ) ;
string s2 ( s1) ;
s1[ 0 ] = 'b' ;
cout<< s1<< endl;
cout<< s2<< endl;
string s3= s2;
str_substr ( ) ;
str_find ( ) ;
str_replace ( ) ;
str_erase ( ) ;
str_insert ( ) ;
set< int > ss;
ss. insert ( 1 ) ;
ss. insert ( 2 ) ;
ss. insert ( 3 ) ;
ss. insert ( 1 ) ;
cout<< "size of s = " << ss. size ( ) << endl;
cout<< "maxsize of s = " << ss. max_size ( ) << endl;
cout<< "first element of s = " << * ss. begin ( ) << endl;
cout<< "last element of s = " << * ss. end ( ) << endl;
map< int , string> mapStudent;
mapStudent. insert ( pair < int , string> ( 1 , "student_one" ) ) ;
mapStudent. insert ( pair < int , string> ( 2 , "student_two" ) ) ;
mapStudent. insert ( pair < int , string> ( 3 , "student_three" ) ) ;
mapStudent. insert ( pair < int , string> ( 1 , "student_three" ) ) ;
cout<< mapStudent. size ( ) << endl;
for ( map< int , string> :: iterator it= mapStudent. begin ( ) ; it!= mapStudent. end ( ) ; it++ )
cout<< it-> first<< " " << it-> second<< endl;
map< int , string> :: iterator iter;
iter= mapStudent. find ( 1 ) ;
cout<< iter-> second<< endl;
mapStudent. erase ( iter) ;
int n= mapStudent. erase ( 1 ) ;
cout<< n;
mapStudent. erase ( mapStudent. begin ( ) , mapStudent. end ( ) ) ;
return 0 ;
}