目录
基本理论容器——算法——迭代器
容器中可以嵌套容器,容器可以分为序列式容器和关联式容器。
序列式容器:容器的元素的位置是由进入容器时机和地点来决定。
关联式容器:容器已经有规则,进入容器的元素的位置不是由时机和地点决定。
迭代器:可以理解为指针,对指针的操作基本都可以对迭代器操作。实际上,,迭代器是一个类,这个类封装一个指针。
算法:通过有限的步骤去解决问题。
STL容器、算法、迭代器分离案例
//算法:负责统计某个元素的个数
int mycount(int* start,int* end, int val){
int num = 0;
while(start != end){
if(*start == val){
num++;
}
start++;
}
return num;
}
int main(){
int arr[] = {0,7,5,4,9,2,0};
int* pBegin = arr;
int* pEnd = &(arr[szeof(arr)/sizeof(int)]);
int num = mycount(pBegin,pEnd,0);
cout<<num<<endl;
return 0;
}
//STL基本语法
void test01(){
//定义一个容器,并且指定这个容器存放的元素类型是Int;
vector<int> v;
v.push_back(10);
v.push_back(20);
v.push_back(30);
v.push_back(40);
//通过STL提供的for_each算法
//容器提供的迭代器
//vector<int>::iterator 迭代器类型
vector<int>::iterator pBegin = v.begin();
vector<int>::iterator pEnd = v.end();
//容器中可能存放基础的数据类型,也可能存放自定义的数据类型。
for_each(pBegin,pEnd,PrintVector);
}
//容器也可以存放自定义数据类型
class Person{
public:
Person(int age, int id):age(age),id(id){}
public:
int age;
int id;
}
void test02(){
//创建容器,并且指定容器的元素类型是Person;
vector<Person> v;
Person p1(10,20), p2(30,40), p3(50,60);
v.push_back(p1);
v.push_back(p2);
v.push_back(p3);
//遍历
for(vector<Person>::iterator it = v.begin(); it!=v.end(); it++){
cout<<(*it).age<<" "<<(*it).id<<endl;
}
}
int main(){
test01();
test02();
}
string容器
char*与string的对比:string封装了char*,管理这个字符串,是一个char*型的容器。
string封装了很多实用的成员方法:find,copy,delete,replace,insert。
不用考虑内存的释放和越界。
string和char*可以互相转换:string转char*通过string提供的c_str方法。
string操作
#include<iostream>
#include<string>
using namespace std;
//char* 和string转换
void test01(){
//string类型转换为char*字符串
string s = "abc";
const char* str = s.c_str();
//char*类型转换为string类型字符串
char* str2 = "abcd";
string s2(str2);
}
//string初始化
void test02(){
string s;//默认
string s2 = "abcd";
string s3(s2);
string s4(10,'c');
cout<<s2<<endl;
cout<<s3<<endl;
cout<<s4<<endl;
}
//string容器字符串赋值和存取
void test03(){
//string容器赋值
string s;
s = "abcd";
string s2;
s2.assign("pppp");
//string容器存取
string s3 = "abcdefg";
for(int i = 0; i < s3.size(); i++){
cout<<s3[i]<<" ";
}
cout<<endl;
for(int i = 0; i < s3.size(); i++){
cout<<s3.at(i)<<" ";
}
cout<<endl;
}
//[]访问方式越界的时候,不会抛异常直接挂掉。
//at会抛异常。
try{
//cout<<s3[100]<<endl; 直接挂掉
}
catch(...){
cout<<"访问越界"<<endl;
}
//string容器拼接操作
void test04(){
string s1 = "aaa";
string s2 = "bbb";
//string s3 = s1 + s2;
//cout<<s3<<endl;
//s1+=s2;
//cout<<s1<<endl;
//成员方法方式
s1.append(s2);
cout<<s1<<endl;
}
//string查找和替换比较
void test05(){
string s = "acbdefg";
//查找
string target = "bd";
int pos = s.find(target);
char* target2 = "ef";
int pos2 = s.find(target2);
cout<<pos<<" "<<pos2<<endl;
//字符串替换
string s1 = "abcd";
s.replace(0,2,s1);
cout<<s<<endl;
}
//string比较 子串插入和删除
void test06(){
//比较
string s1 = "abc";
string s2 = "abd";
int ret = s1.compare(s2);
if(ret == 1){
cout<<"s1 > s2"<<endl;
}
//子串
string s3 = "abcdefg";
string s4 = s3.substr(0,2);
cout<<"s4"<<s4<<endl;
//插入和删除
string s5 = "abcd";
s5.insert(0,"pppp");
string s6 = "qqqq";
s5.insert(s5.size(),s6);
cout<<s5<<endl;
s5.erase(0,4);
cout<<s5<<endl;
}
swap技巧
#include<iostream>
#include<vector>
using namespace std;
void print(vector<int>& v){
for(vector<int>::iterator it = v.begin(); it != v.end(); it++){
cout<<*it<<" ";
}
}
int main(){
vector<int> v1,v2;
for(int i = 0; i < 5; i++){
v1.push_back(i);
}
for(int i=6; i < 10; i++){
v2.push_back(i);
}
print(v1);
print(v2);
v1.swap(v2);
cout<<"v1和v2交换之后:"<<endl;
print(v1);
print(v2);
//swap小技巧
vector<int> v;
v.resize(100);
cout << "容量:" << v.capacity() << endl;
cout << "大小:" << v.size() << endl;
v.clear();
v.push_back(2);
v.push_back(3);
cout << "容量:" << v.capacity() << endl;
cout << "大小:" << v.size() << endl;
vector<int>(v).swap(v);
cout << "容量:" << v.capacity() << endl;
cout << "大小:" << v.size() << endl;
system("pause");
return EXIT_SUCCESS;
}
deque容器基本操作
#niclude<iostream>
#include<deque>
using namespace std;
//deque容器初始化
void test01(){
deque<int> d1;//默认构造函数
deque<int> d2(10,5);//带参数构造函数
deque<int> d3(d2..begin(),d2.end());
deque<int> d4(d3);//拷贝构造
}
//deque赋值操作
void test02(){
deque<int> d1(10,3);
deque<int> d;
d.assign(10,5);//d.assign(d1.begin(),d1.end());
d = d1;
}
//大小操作
void test03(){
deque<int> d1(10,3);
cout<<d1.size()<<endl;
if(d1.empty()){
cout<<"空"<<endl;
}
else{
cout<<"不空"<<endl;
}
//d1.resize(5);
//cout<<d1.size()<<endl;
d.resize(15);
}
//deque的插入和删除
void test04(){
//第一种遍历方式
for (int i = 0; i < d.size();i++){
cout << d[i] << " ";
}
cout << endl;
//第二种遍历方式
for (int i = 0; i < d.size(); i++){
cout << d.at(i) << " ";
}
cout << endl;
//第三种方式
for (deque<int>::iterator it = d.begin(); it != d.end(); it++){
cout << *it << " ";
}
cout << endl;
//删除元素
/*
while (d.size() > 0){
cout << d.back() << "被删除!" << endl;
d.pop_back();
}
cout << "大小:" << d.size() << endl;
*/
//头删除
while (!d.empty()){
cout << d.front() << "被删除!" << endl;
d.pop_front();
}
//deque容器插入
void test06(){
deque<int> d;
d.insert(d.begin(),100); //头插法
d.insert(d.end(), 200); //尾差法
for (deque<int>::iterator it = d.begin(); it != d.end(); it++){
cout << *it << " ";
}
cout << endl;
}
int main(){
//test03();
//test04();
test06();
system("pause");
return EXIT_SUCCESS;
}
deque打分案例
#include<iostream>
#include<vector>
#include<dueue>
#include<algorithm>
#include<string>
using namespace std;
//评委打分案例(sort算法排序)
//创建5个选手(姓名,得分),10个评委对5个选手进行打分
//得分规则:去除最高分,去除最低分,取出平均分
//按得分对5名选手进行排名
//选手类
class Player{
public:
Player(){}
Player(string name,int score):mName(name),mScore(score){}
public:
string mName;
int mScore;
}
//创建选手
void Create_Player(vector<Player>& v){
string nameSeed = "ABCDE";
for(int i = 0; i < 5; i++){
Player p;
p.mName = "选手";
p.mName+=nameSeed[i];
p.mScore = 0;
v.push_back(p);
}
}
void PrintScore(int val){
cout<<val<<" ";
}
//打分
void Set_Score(vector<Player>& v){
for(vector<Player>::iterator it = v.begin();it!=v.end();it++){
//当前学生进行打分
deque<int> dScore;
for(int i=0;i<10;i++){
int score = rand()%41+60;
dScore.push_back(score);
}
//对分数排序,默认从小到大
sort(dScore.begin(),dScore.end());
//for_each(dScore.begin(),dScore.end(),PrintScore);
//cout<<endl;
//去除最高分,去除最低分
dScore.pop_front();
dScore.pop_back();
//求平均分
int totalScore = 0;
for(deque<int>::iterator dit = dScore.begin();dit!=dScore.end();dit++){
totalScore+=(*dit);
}
int aveScore = totalScore/dScore.size();
//保存分数
(*it).mScore = aveScore;
}
}
set容器
#include<iostream>
#include<functional>//预定义函数对象
#include<set>
#include<stdlib.h>
#include<time.h>
using namespace std;
//初始化
void test01(){
set<int> myset;//默认构造
set<int> myset2(myset)//拷贝构造
}
void printSet(set<int>& myset){
for(set<int>::iterator it = myset.begin();it!=myset.end();it++){
cout<<*it<<" ";
}
cout<<endl;
}
//set容器插入和删除
void test02(){
set<int> myset;//默认升序
myset.insert(4);
myset.insert(2);
myset.insert(1);
myset.insert(5);
myset.insert(2);
printSet(myset);
//删除
myset.erase(myset.begin());//删除第一个元素
myset.erase(2);//根据元素值删除
printSet(myset);
myset.erase(myset.begin();myset.end());//myset.clear();
cout<<"size:"<<myset.size()<<endl;
}
template<class T>
class mycompare03{
public:
bool operator()(T v1,T v2){
return v1>v2;
}
};
//set容器查找
void test03(){
//函数对象
//mycompare03 mycom;
//mycom(10);//函数对象,仿函数
set<int,mycompare03<int>> myset;//默认从小到大排序
myset.insert(4);
myset.insert(2);
myset.insert(1);
myset.insert(5);
for(set<int,greater<int>>::iterator it = myset.begin();it!=myset.end();it++){
cout<<*it<<" ";
}
cout<<endl;
}
class Teacher{
public:
bool operator()(Teacher t1,Teacher t2){
return t1.id>t2.id;
}
};
void test04(){
set<Teacher,myconpare04> myset;
Teacher t1(1,2),t2(3,4),t3(5,6);
myset.insert(t1);
myset.insert(t2);
myset.insert(t3);
for(set<Teacher,mycompare04>::iterator it=myset.begin();it!=myset.end();it++){
cout<<it->id<<" "<<it->age<<endl;
}
cout<<endl;
}
void test05(){
set<int> myset;
myset.insert(10);
myset.insert(5);
myset.insert(1);
myset.insert(8);
set<int>::iterator pos = myset.lower_bound(5); //返回大于等于5 迭代器
if (pos == myset.end()){
cout << "没有找到!" << endl;
}
else{
cout << "找到:" << *pos << endl;
}
pos = myset.upper_bound(5);
if (pos == myset.end()){
cout << "没有找到!" << endl;
}
else{
cout << "找到:" << *pos << endl;
}
pair<set<int>::iterator, set<int>::iterator> pos2 = myset.equal_range(5);
if (pos2.first == myset.end()){
cout << "meiyouzhaodao!" << endl;
}
else{
cout << "zhaodao!" << *(pos2.first) << endl;
}
if (pos2.second == myset.end()){
cout << "meiyouzhaodao!" << endl;
}
else{
cout << "zhaodao!" << *(pos2.second) << endl;
}
}
对组练习
#include<iostream>
#include<string>
using namespace std;
int main(){
//第一种方式 创建一个pair
pair<int,string> mypair(10,"aaa");
cout<<mypair.first<<endl;
cout<<mypair.second<<endl;
//第二种 创建pair
pair<string,string> mypair2 = make_pair("aaa","bbb");
//auto mypair2 = make_pair("aaa","bbb");
cout<<mypair2.first<<" "<<mypair2.second<<endl;
//第三种
pair<int,string> mypair3 = mypair;//拷贝构造
system("pause");
return EXIT_SUCCESS;
}
map容器
#include<iostream>
#include<map>
#include<string>
using namespace std;
//map容器初始化
void test01(){
//map容器的模板参数,需要指定key类型 value类型
map<int,string> mymap;//默认构造
map<int,string> mymap2(mymap);//拷贝构造
}
//map插入操作
void test02(){
map<int,int> mymap;
//第一种插入方式
mymap.insert(pair<int,int>(1,5));
//第二种
pair<map<int,int>::iterator,bool> ret = mymap.insert(make_pair(2,10));
if(ret.second){
cout<<"插入成功"<<endl;
}
else{
cout<<"插入失败"<<endl;
}
//第三种
mymap.insert(map<int,int>::value_type(3,15));
//第四种
//mymap[4] = 20;
//四种插入方式区别:
//如果Key存在,会修改容器指定key元素的值
for(map<int,int>::iterator it=mymap.begin();it!=mymap.end();it++){
cout<<"key:"<<it->first<<"value:"<<it->second<<endl;
}
cout<<endl;
//如果访问的key不存在,会创建这个数据插入进去
ret = mymap.insert(map<int,int>::value_type(2,10));
if(ret.second){
cout<<"插入成功"<<endl;
}
else{
cout<<"插入失败"<<endl;
}
}
void print(map<int,int>& mymap){
for (map<int, int>::iterator it = mymap.begin(); it != mymap.end();it++){
cout << it->first << " " << it->second << " ";
}
cout<<endl;
}
//删除和查找
void test03(){
map<int, int> mymap;
mymap.insert(make_pair(1, 2));
mymap.insert(make_pair(2, 3));
mymap.insert(make_pair(3, 4));
//查找
map<int,int>::iterator pos = mymap.find(3);
if(pos == mymap.end()){
cout<<"没有找到"<<endl;
}
else{
cout<<"查找到"<<pos->first<<"value:"<<pos->>second<<endl;
}
cout << "---------------" << endl;
pos = mymap.lower_bound(2);
if (pos == mymap.end()){
cout << "没有找到!" << endl;
}
else{
cout << "查找到:" << pos->first << " value:" << pos->second << endl;
}
pos = mymap.upper_bound(2);
if (pos == mymap.end()){
cout << "没有找到!" << endl;
}
else{
cout << "查找到:" << pos->first << " value:" << pos->second << endl;
}
pair<map<int, int>::iterator, map<int, int>::iterator> pos2 = mymap.equal_range(2);
if (pos2.first == mymap.end()){ //第一个迭代器
cout << "没有找到!" << endl;
}
else{
cout << "查找到:" << pos2.first->first << " value:" << pos2.first->second << endl;
}
if (pos2.second == mymap.end()){ //第二个迭代器
cout << "没有找到!" << endl;
}
else{
cout << "查找到:" << pos2.second->first << " value:" << pos2.second->second << endl;
}
}
mutimap员工分组
#include<iostream>
#include<vector>
#include<map>
#include<string>
using namespace std;
#define SALE_DEPARTMENT 1 //销售部
#define DEVELOP_DEPARTMENT 2 //开发部
#define FINACIAL_DEPARTMENT 3 //财务部
class Yuangong{
public:
string name;
int age;
string tele;
double salary;
};
//创建员工5名
void Create_Yuangong(vector<Yuangong>& v){
string namesed = "ABCDE";
for(int i=0;i<5;i++){
Yuangong yg;
yg.name = "员工";
yg.name += nameseed[i];
yg.age = rand()%30;
yg.salary = rand()%10000+10000;
yg.tele = "86-88888888";
v.push_back(yg);
}
}
//对员工指派部门
void Set_YG_Group(vector<Yuangong>& v,mutimap<int,Yuangong>& group){
for(vector<Yuangong>::iterator it = v.begin(); it!=v.end();it++){
cout<<"当前员工信息:"<<endl;
cout<<"名字:"<<it->name<<"年龄:"<<it->age<<"工资:"<<it->salary<<"电话:"<<it->tele;<<endl;
int departmentID = -1;
while (true){
cout << "请输入部门(1 销售部 2 开发部 3 财务部) : " << endl;
scanf("%d", &departmentID);
if (departmentID == SALE_DEPARTMENT){
group.insert(make_pair(SALE_DEPARTMENT,*it));
break;
}
else if (departmentID == DEVELOP_DEPARTMENT){
group.insert(make_pair(DEVELOP_DEPARTMENT, *it));
break;
}
else if (departmentID == FINACIAL_DEPARTMENT){
group.insert(make_pair(FINACIAL_DEPARTMENT, *it));
break;
}
else{
cout << "输入错误,请重新输入:" << endl;
}
}
}
}
//打印各部门员工信息
void Show_YG_Info(mutimap<int,Yuangong>& group){
int departmentID = -1;
while(true){
cout<<"请输入要查看的部门(1 销售部 2 开发部 3 财务部):"<<endl;
scanf("%d",&departmentID);
//验证输入有效性
if(departmentID<1||departmentID>3){
continue;
}
mutimap<int,Yuangong>::iterator pos = group.find(departmentId);
int ygcount = group.count(departmentID);
int num = 0;
while(pos != group.end()&&num<ygcount){
cout << "姓名: " << pos->second.name << " 年龄:" << pos->second.age << " 工资" << pos->second.salary << " 电话" << pos->second.tele << endl;
num++;
pos++;
}
}
}
int main(){
//multimap 案例
//公司今天招聘了 5 个员工,5 名员工进入公司之后,需要指派员工在那个部门工作
//人员信息有: 姓名 年龄 电话 工资等组成
//通过 Multimap 进行信息的插入 保存 显示
//分部门显示员工信息 显示全部员工信息
vector<Yuangong> v; //放员工信息 未分组之前
multimap<int, Yuangong> Ygroup; //存放分组后的员工信息
Create_Yuangong(v); //创建员工
Set_YG_Group(v, Ygroup); //员工分组
Show_YG_Info(Ygroup); //按分组显示员工信息
system("pause");
return EXIT_SUCCESS;
}
深拷贝和浅拷贝问题
#include<iostream>
#include<vector>
using namespace std;
class Teacher{
public:
Teacher(char* name,int age){
int len = strlen(name)+1;
this->name = new char[len];
strcpy(this->name,name);
this->age = age;
}
//拷贝构造
Teacher(const Teacher& t){
int len = strlen(t.name)+1;
this->name = new char[len];
strcpy(this->name,t.name);
this->age = t.age;
}
//重载
Teacher& operator=(Teacher& t){
int len = strlen(t.name)+1;
if(this->name!=NULL){
delete[] this->name;
}
this->name = new char[len];
strcpy(this->name,t.name);
this->age = t.age;
return *this;
}
~Teacher(){
if(this->name!=NULL){
delete[] this->name;
}
this->age = 0;
}
char* name;
int age;
};
void test01(){
Teacher t1("aaa",20);
vector<Teacher> v;
v.push_back(t1);
}
int main(){
test01();
system("pause");
return EXIT_SUCCESS;
}
函数对象
#include<iostream>
#include<string>
#include<vector>
#include<algorithm>
using namespace std;
void print(){
cout<<"hello world"<<endl;
}
class myprint{
public:
void operator()(){
cout<<"hello world"<<endl;
}
};
//函数对象和普通函数对比
void test01(){
print();
myprint myp;
myp();
}
void print02(int index){
cout<<"hello world:"<<endl;
}
class myprint02{
public:
void operator()(int index){
cout<<"hello world:"<<index<<endl;
}
};
void test02(){
print02(10);
myprint02 myp02;
myp02(10);
}
//函数对象超出了普通函数的概念,可以保存函数调用状态
//统计函数调用次数
int q_count = 0;
void print03(){
q_count++;
}
struct myprint03{
myprint03():count(0){}
void operator(){
count++;
}
int count;
}
void test03(){
print03();
print03();
cout << g_count << endl;
myprint03 myp03;
myp03();
myp03();
myp03();
cout << myp03.count << endl;
}
//函数对象做参数 做返回值
class myprint04{
public:
myprint04() :count(0){}
void operator()(int v){
count++;
cout << v << " ";
}
int count;
};
void test04(){
vector<int> v;
for (int i = 0; i < 10;i++){
v.push_back(i);
}
myprint04 myp04;
myprint04 myp05 = for_each(v.begin(), v.end(), myp04); //函数对象做参数
cout << "count:" << myp04.count << endl; cout << endl;
cout << "count:" << myp05.count << endl; cout << endl;
}
一元二元函数对象和一元二元谓词
#include<iostream>
#include<vector>
#include<algorithm>
using namespace std;
//如果你写的函数对象接受一个参数,那么就叫 一元函数对象
//如果你写的函数对象接受两个参数,那么叫 二元函数对象
//如果你写的函数对象或者普通函数接受一个参数,并且返回值是Bool,叫一元谓词
//如果你写的函数对象或者普通函数接受二个参数,并且返回值是Bool,叫二元谓词
//一元函数对象 应用举例 : for_each
class print{
public:
void operator()(int v){
cout << v << " ";
}
};
void print2(int v){
cout << v << " ";
}
void test01(){
vector<int> v;
for (int i = 0; i < 10;i++){
v.push_back(i);
}
//print p;
//for_each(v.begin(), v.end(), print()); //函数对象
for_each(v.begin(), v.end(), print2); //普通函数
cout << endl;
}
//一元谓词 应用举例:find_if
class mycompare{
public:
bool operator()(int v){
if (v > 7){
return true;
}
else{
return false;
}
}
};
void test02(){
vector<int> v;
for (int i = 0; i < 10; i++){
v.push_back(i);
}
/*
template<class _InIt,
class _Pr> inline
_InIt find_if(_InIt _First, _InIt _Last, _Pr _Pred)
{ // find first satisfying _Pred
_DEBUG_RANGE(_First, _Last);
_DEBUG_POINTER(_Pred);
return (_Rechecked(_First,
_Find_if(_Unchecked(_First), _Unchecked(_Last), _Pred)));
}
template<class _InIt,
class _Pr> inline
_InIt _Find_if(_InIt _First, _InIt _Last, _Pr _Pred)
{ // find first satisfying _Pred
for (; _First != _Last; ++_First)
if (_Pred(*_First))
break;
return (_First);
}
*/
vector<int>::iterator pos = find_if(v.begin(), v.end(), mycompare()); //匿名函数对象
if(pos == v.end()){
cout << "没找到" << endl;
}
else{
cout << "找到:" << *pos << endl;
}
}
//二元函数对象 应用举例 : transform
class myplus{
public:
int operator()(int v1,int v2){
return v1 + v2;
}
};
void test03(){
vector<int> v1,v2,v3;
for (int i = 0; i < 10; i++){
v1.push_back(i);
v2.push_back(i + 1);
}
/*
template<class _InIt1,
class _InIt2,
class _OutIt,
class _Fn2> inline
_OutIt transform(_InIt1 _First1, _InIt1 _Last1,
_InIt2 _First2, _OutIt _Dest, _Fn2 _Func)
{ // transform [_First1, _Last1) and [_First2, ...) with _Func
_DEBUG_RANGE(_First1, _Last1);
_DEBUG_POINTER(_Dest);
_DEBUG_POINTER(_Func);
if (_First1 != _Last1)
return (_Transform2(_Unchecked(_First1), _Unchecked(_Last1),
_First2, _Dest, _Func,
_Is_checked(_Dest)));
return (_Dest);
}
template<class _InIt1,
class _InIt2,
class _OutIt,
class _Fn2> inline
_OutIt _Transform(_InIt1 _First1, _InIt1 _Last1,
_InIt2 _First2, _OutIt _Dest, _Fn2 _Func)
{ // transform [_First1, _Last1) and [_First2, ...) with _Func
for (; _First1 != _Last1; ++_First1, ++_First2, ++_Dest)
*_Dest = _Func(*_First1, *_First2);
return (_Dest);
}
*/
v3.resize(v1.size());
for_each(v1.begin(), v1.end(), print2); //普通函数
cout << endl;
for_each(v2.begin(), v2.end(), print2); //普通函数
cout << endl;
for_each(v3.begin(), v3.end(), print2); //普通函数
cout << endl;
transform(v1.begin(), v1.end(), v2.begin(), v3.begin(), myplus()); //匿名函数对象
cout << "------------------------------" << endl;
for_each(v1.begin(), v1.end(), print2); //普通函数
cout << endl;
for_each(v2.begin(), v2.end(), print2); //普通函数
cout << endl;
for_each(v3.begin(), v3.end(), print2); //普通函数
cout << endl;
}
//二元谓词 应用举例 : sort
class mycompare04{
public:
bool operator()(int v1,int v2){
return v1 > v2; //从大到小
}
};
void test04(){
vector<int> v;
v.push_back(5);
v.push_back(2);
v.push_back(7);
v.push_back(9);
/*
template<class _RanIt,
class _Pr> inline
void sort(_RanIt _First, _RanIt _Last, _Pr _Pred)
{ // order [_First, _Last), using _Pred
_DEBUG_RANGE(_First, _Last);
_DEBUG_POINTER(_Pred);
_Sort(_Unchecked(_First), _Unchecked(_Last), _Last - _First, _Pred);
}
*/
for_each(v.begin(), v.end(), print2); //普通函数
cout << endl;
sort(v.begin(), v.end(), mycompare04());
for_each(v.begin(), v.end(), print2); //普通函数
cout << endl;
}
int main(){
//test01();
//test02();
//test03();
test04();
system("pause");
return EXIT_SUCCESS;
}
预定义函数对象
#include<iostream>
#include<functional>
#include<string>
#include<vector>
#include<algorithm>
using namespace std;
/*
template<class T> T plus<T>//加法仿函数
template<class T> T minute<T>//减法仿函数
template<class T> T multiplies<T>//乘法仿函数
template<class T> T divides<T>//除法仿函数
template<class T> T modulus<T>//取模仿函数
template<class T> T negate<T>//取反仿函数
*/
void test01(){
plus<int> myplus;
int ret = myplus(10,20);
cout<<ret<<endl;
plus<string> myplus2;
string s1 = "aaa";
string s2 = "bbb";
string ret2 = myplus2(s1,s2);
cout<<ret2<<endl;
cout<<plus<int>()(10,20)<<endl;
}
//transform
void print(int v){
cout<<v<<" ";
}
void test02(){
vector<int> v1,v2,v3;
for(int i=0;i<10;i++){
v1.push_back(i);
v2.push_back(i+1);
}
v3.resize(v1.size());
//plus<int> myplus;
for_each(v3.begin(),v3.end(),print);
cout<<endl;
transform(v1.begin(),v1.end(),v2.begin(),v3.begin(),plus<int>());
for_each(v3.begin(),v3.end(),print);
cout<<endl;
}