文章目录
遍历算法 1. sort()
2. random_shuffle()
3. merge()
4. reverse()
遍历算法
1. sort()
代码工程
sort ( ) 函数默认是升序排列,如果想要降序排列需要添加一个函数或者仿函数。
# define _CRT_SECURE_NO_WARNINGS
# include <iostream>
# include <vector>
# include <algorithm>
using namespace std;
void printVector ( const vector< int > & v)
{
for ( int i = 0 ; i < v. size ( ) ; i++ )
{
cout << v[ i] << " " ;
}
cout << endl;
}
class Greator
{
public:
bool operator ( ) ( int v1, int v2)
{
return v1 > v2;
}
} ;
void test01 ( )
{
vector< int > v;
v. push_back ( 50 ) ;
v. push_back ( 10 ) ;
v. push_back ( 30 ) ;
v. push_back ( 20 ) ;
v. push_back ( 40 ) ;
cout << "排序前: " ;
printVector ( v) ;
sort ( v. begin ( ) , v. end ( ) , Greator ( ) ) ;
cout << "排序后: " ;
printVector ( v) ;
return ;
}
int main ( )
{
test01 ( ) ;
return 0 ;
}
运行结果
2. random_shuffle()
random_shuffle ( ) 函数是一个打乱容器元素排列的一种算法,俗称“洗牌”算法;
从运行的三次结果可以看出,每次打乱的顺序都是不一样的;
需要注意的是如果你没有添加随机种子,那么每次运行的结果都是一样的。
代码工程
# define _CRT_SECURE_NO_WARNINGS
# include <iostream>
# include <vector>
# include <algorithm>
# include <ctime>
using namespace std;
class print
{
public:
void operator ( ) ( int v)
{
cout << v << " " ;
}
} ;
void test01 ( )
{
srand ( ( unsigned int ) time ( NULL ) ) ;
vector< int > v;
v. push_back ( 10 ) ;
v. push_back ( 20 ) ;
v. push_back ( 30 ) ;
v. push_back ( 40 ) ;
v. push_back ( 50 ) ;
cout << "打乱前: " ;
for_each ( v. begin ( ) , v. end ( ) , print ( ) ) ;
random_shuffle ( v. begin ( ) , v. end ( ) ) ;
cout << endl;
cout << "打乱后: " ;
for_each ( v. begin ( ) , v. end ( ) , print ( ) ) ;
return ;
}
int main ( )
{
test01 ( ) ;
return 0 ;
}
运行结果
第一次运行结果
第二次运行结果
第三次运行结果
3. merge()
将两个容器的元素合并到另一个容器中;
需要注意的是目标容器需要开辟空间,开辟空间的大小为两个融合容器中元素个数之和。
代码工程
# define _CRT_SECURE_NO_WARNINGS
# include <iostream>
# include <vector>
# include <algorithm>
using namespace std;
class print
{
public:
void operator ( ) ( int v)
{
cout << v << " " ;
}
} ;
void test01 ( )
{
vector< int > v1;
v1. push_back ( 10 ) ;
v1. push_back ( 20 ) ;
v1. push_back ( 30 ) ;
vector< int > v2;
v2. push_back ( 100 ) ;
v2. push_back ( 200 ) ;
v2. push_back ( 300 ) ;
vector< int > vTarge;
vTarge. resize ( v1. size ( ) + v2. size ( ) ) ;
merge ( v1. begin ( ) , v1. end ( ) , v2. begin ( ) , v2. end ( ) , vTarge. begin ( ) ) ;
cout << "融合后: " ;
for_each ( vTarge. begin ( ) , vTarge. end ( ) , print ( ) ) ;
cout << endl;
return ;
}
int main ( )
{
test01 ( ) ;
return 0 ;
}
运行结果
4. reverse()
将容器中的元素进行反转
代码工程
# define _CRT_SECURE_NO_WARNINGS
# include <iostream>
# include <vector>
# include <algorithm>
using namespace std;
class print
{
public:
void operator ( ) ( int v)
{
cout << v << " " ;
}
} ;
void test01 ( )
{
vector< int > v;
v. push_back ( 10 ) ;
v. push_back ( 20 ) ;
v. push_back ( 30 ) ;
v. push_back ( 40 ) ;
v. push_back ( 50 ) ;
cout << "反转前: " ;
for_each ( v. begin ( ) , v. end ( ) , print ( ) ) ;
cout << endl;
reverse ( v. begin ( ) , v. end ( ) ) ;
cout << "反转后: " ;
for_each ( v. begin ( ) , v. end ( ) , print ( ) ) ;
cout << endl;
return ;
}
int main ( )
{
test01 ( ) ;
return 0 ;
}
运行结果