merge:将两个容器的元素融合成一个容器中,并进行排序
- merge(v1.begin(), v1.end(), v2.begin(), v2.end(), v3.begin(), Compare());
- 进行操作前,两个容器的排序规则需要一样,第三个容器的排序规则也需要相同
- 默认是进行从小到大,如需要自己定义规则
- v1和v2可以是支持随机访问的,也可以是不支持随机访问的容器,如list容器(例2)
例1:
#include "stdafx.h"
#include <iostream>
#include <algorithm>
#include <vector>
#include <functional>
#include <time.h>
using namespace std;
struct Print
{
void operator()(int n)
{
cout << " " << n;
}
};
struct Compare
{
bool operator()(int n,int m)
{
return n>m;
}
};
int main()
{
vector<int> v1,v2;
srand((unsigned int) time(NULL));
for (int i = 0; i < 5; i++) //构造v1
{
v1.push_back(rand() % 10);
}
for (int i = 0; i < 10; i++)//构造v2
{
v2.push_back(rand() % 10);
}
for_each(v1.begin(), v1.end(), Print()); cout << endl; //打印原始元素
for_each(v2.begin(), v2.end(), Print()); cout << endl;
sort(v1.begin(), v1.end(), Compare()); //按从大到小顺序排列(默认从小到大)
sort(v2.begin(), v2.end(), Compare());
cout << "-----------------" << endl;
for_each(v1.begin(), v1.end(), Print()); cout << endl; //排序之后再打印
for_each(v2.begin(), v2.end(), Print()); cout << endl;
cout << "-----------------" << endl;
vector<int> v3;
v3.resize(v1.size() + v2.size());//构造v3
merge(v1.begin(), v1.end(), v2.begin(), v2.end(), v3.begin(), Compare()); //将v1 v2 融合排序 之后 交给v3
for_each(v3.begin(), v3.end(), Print()); cout << endl;
return 0;
}
结果:
例2:
#include "stdafx.h"
#include <iostream>
#include <algorithm>
#include <vector>
#include <functional>
#include <time.h>
#include <list>
using namespace std;
struct Print
{
void operator()(int n)
{
cout << " " << n;
}
};
struct Compare
{
bool operator()(int n,int m)
{
return n>m;
}
};
int main()
{
vector<int> v1;
list<int>l2;
srand((unsigned int) time(NULL));
for (int i = 0; i < 5; i++)
{
v1.push_back(rand() % 10);
} //构造一个vector,一个list ,输出结果
for (int i = 0; i < 10; i++)
{
l2.push_back(rand() % 10);
}
for_each(v1.begin(), v1.end(), Print()); cout << " ---> vector container"<<endl;
for_each(l2.begin(), l2.end(), Print()); cout <<" ---> list container"<< endl;
/*---------------------------------------------------------------------------------------------------*/
sort(v1.begin(), v1.end(), Compare());
l2.sort(Compare());
//进行排序,再输出
cout << "-----------------" << endl;
for_each(v1.begin(), v1.end(), Print()); cout << endl;
for_each(l2.begin(), l2.end(), Print()); cout << endl;
/*---------------------------------------------------------------------------------------------------*/
cout << "-----------------" << endl;
vector<int> v3;
v3.resize(v1.size() + l2.size());
//进行排序融合
merge(v1.begin(), v1.end(), l2.begin(), l2.end(), v3.begin(), Compare());
for_each(v3.begin(), v3.end(), Print()); cout << endl;
return 0;
}
结果:
sort:排序
- sort(v1.begin(),v1.end(),Comapre())
上面案列有用到
reverse:倒序
- reverse(v1.begin(),v1.end()):不一定是从开始到结束,也可以是中间的区域
#include "stdafx.h"
#include <iostream>
#include <vector>
#include <functional>
#include <algorithm>
using namespace std;
class Person
{
public:
Person(int age, int id) :m_age(age), m_id(id)
{
}
void Print()
{
cout << " age: " << m_age <<" id:" << m_id << endl;
}
int m_age;
int m_id;
};
int main()
{
vector <Person> v;
Person p1(10, 10) ;
Person p2(20, 20) ;
Person p3(30, 30) ;
Person p4(40, 40);
v.push_back(p1);
v.push_back(p2);
v.push_back(p3);
v.push_back(p4);
for_each(v.begin(), v.end(), mem_fun_ref(&Person::Print));
cout << "----------------------------" << endl;
reverse(v.begin(), v.end()-1);
for_each(v.begin(), v.end(), mem_fun_ref(&Person::Print));
return 0;
}
结果:只反转了前三个
random_shuffle:打乱顺序,洗牌
- random_shuffle( v1.begin(),v1.end())
#include "stdafx.h"
#include <iostream>
#include <vector>
#include <algorithm>
#include <time.h>
using namespace std;
class Person
{
public:
Person(int age, int id) :m_age(age), m_id(id)
{
}
int m_age;
int m_id;
};
struct Print
{
void operator()(const Person&p1)
{
cout << p1.m_age << " ";
}
};
int main()
{
vector<Person> v1;
Person p1(1, 1), p2(2, 2), p3(3, 3), p4(4, 4),p5(5,5);
srand((unsigned int)time(NULL));
v1.push_back(p1);
v1.push_back(p2);
v1.push_back(p3);
v1.push_back(p4);
v1.push_back(p4);
v1.push_back(p5);
for_each(v1.begin(), v1.end(), Print()); cout << endl;
cout << "-------------" << endl;
random_shuffle(v1.begin(), v1.end());
for_each(v1.begin(), v1.end(), Print()); cout << endl;
return 0;
}
结果: