文章目录
遍历算法 1. copy()
2. replace()
3. replace_if()
4. swap()
遍历算法
1. copy()
代码工程
copy ( ) 函数不要因为使用而使用
# define _CRT_SECURE_NO_WARNINGS
# include <iostream>
# include <vector>
# include <algorithm>
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;
v1. push_back ( 10 ) ;
v1. push_back ( 20 ) ;
v1. push_back ( 30 ) ;
v1. push_back ( 40 ) ;
vector< int > v2;
v2. resize ( v1. size ( ) ) ;
copy ( v1. begin ( ) , v1. end ( ) , v2. begin ( ) ) ;
printVector ( v2) ;
return ;
}
int main ( )
{
test01 ( ) ;
return 0 ;
}
运行结果
2. replace()
replace ( ) 函数是将容器中旧元素替换为指定的新元素;
注意:是将容器中的所有符合的旧元素,全部替换为新元素。
代码工程
# define _CRT_SECURE_NO_WARNINGS
# include <iostream>
# include <vector>
# include <algorithm>
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;
v1. push_back ( 10 ) ;
v1. push_back ( 20 ) ;
v1. push_back ( 30 ) ;
v1. push_back ( 40 ) ;
v1. push_back ( 30 ) ;
cout << "替换前:" ;
printVector ( v1) ;
replace ( v1. begin ( ) , v1. end ( ) , 30 , 100 ) ;
cout << "替换后:" ;
printVector ( v1) ;
return ;
}
int main ( )
{
test01 ( ) ;
return 0 ;
}
运行结果
3. replace_if()
replace_if ( ) 函数是将容器中符合条件的旧元素替换为指定的新元素;
注意:是将容器中的所有符合条件的旧元素,全部替换为新元素。
代码工程
# define _CRT_SECURE_NO_WARNINGS
# include <iostream>
# include <vector>
# include <algorithm>
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;
}
class GreatTwenty
{
public:
bool operator ( ) ( int val)
{
return val > 20 ;
}
} ;
void test01 ( )
{
vector< int > v1;
v1. push_back ( 10 ) ;
v1. push_back ( 20 ) ;
v1. push_back ( 30 ) ;
v1. push_back ( 40 ) ;
v1. push_back ( 30 ) ;
v1. push_back ( 50 ) ;
v1. push_back ( 20 ) ;
cout << "替换前:" ;
printVector ( v1) ;
replace_if ( v1. begin ( ) , v1. end ( ) , GreatTwenty ( ) , 2000 ) ;
cout << "替换后:" ;
printVector ( v1) ;
return ;
}
int main ( )
{
test01 ( ) ;
return 0 ;
}
运行结果
4. swap()
swap ( ) 函数,将两个容器的元素进行交换;
注意:入参是将容器变量直接填入。
代码工程
# define _CRT_SECURE_NO_WARNINGS
# include <iostream>
# include <vector>
# include <algorithm>
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;
v1. push_back ( 10 ) ;
v1. push_back ( 20 ) ;
v1. push_back ( 30 ) ;
v1. push_back ( 40 ) ;
vector< int > v2;
v2. push_back ( 100 ) ;
v2. push_back ( 200 ) ;
v2. push_back ( 300 ) ;
v2. push_back ( 400 ) ;
cout << "交换前" << endl;
cout << "v1容器元素:" ;
printVector ( v1) ;
cout << "v2容器元素:" ;
printVector ( v2) ;
cout << endl;
swap ( v1, v2) ;
cout << "交换后" << endl;
cout << "v1容器元素:" ;
printVector ( v1) ;
cout << "v2容器元素:" ;
printVector ( v2) ;
return ;
}
int main ( )
{
test01 ( ) ;
return 0 ;
}
运行结果