做着一些和考试无关的事情
常用查找算法——续
FIND_IF
find_if //按条件查找元素,返回迭代器POS / END()
find_if(beg,end,_Fred) _Fred函数或谓词(返回BOOL类型的仿函数)
#include<iostream>
#include<string>
#include<vector>
#include<algorithm>
using namespace std;
//find_if //按条件查找元素 find_if(beg,end,_Fred)_Fred函数或谓词(返回BOOL类型的仿函数)
class Greater {
public:bool operator()(int val) {return val>5;}
};
class Person {
public:Person(string n, int a) {this->_name = n;this->_age = a;}bool operator==(const Person& p) {if (this->_name == p._name && this->_age == p._age) {return true;}else {return false;}}string _name;int _age;
};
class Greater02 {
public:bool operator()(const Person &p) {return p._age==9;}
};
void test01() {vector<int>v;for (int i = 0; i < 10; i++) {v.push_back(i);}vector<int>::iterator it=find_if(v.begin(), v.end(), Greater());if (it == v.begin()) {cout << " no find" << endl;}else {cout << "find : " <<*it<< endl;}
}
void test02() {vector<Person>p;Person p0("fsdef", 23);Person p1("复合工艺", 28);Person p2("粉色", 9);Person p3("得分·", 45);Person p4("啊上网服务", 53);p.push_back(p0);p.push_back(p1);p.push_back(p2);p.push_back(p3);p.push_back(p4);vector<Person>::iterator it = find_if(p.begin(), p.end(), Greater02());if (it == p.begin()) {cout << " no find" << endl;}else {cout << "find : " << it->_name<<it->_age<< endl;}
}
int main() {test01();test02();return 0;
}
ADJACENT_FIND
adjacent_find //查找相邻重复元素
adjacent_find(begin,end)返回相邻元素的第一个的POS
#include<iostream>
#include<string>
#include<vector>
#include<algorithm>
using namespace std;
//adjacent_find //查找相邻重复元素 adjacent_find(begin,end)返回相邻元素的第一个的POS
class Person {
public:Person(string n, int a) {this->_name = n;this->_age = a;}bool operator==(const Person& p) {if (this->_name == p._name && this->_age == p._age) {return true;}else {return false;}}string _name;int _age;
};
void test01() {vector<int>v;v.push_back(0);v.push_back(1);v.push_back(0);v.push_back(3);v.push_back(3);vector<int>::iterator it = adjacent_find(v.begin(), v.end());if (it == v.begin()) {cout << " no find" << endl;}else {cout << "find : " <<*it<< endl;}
}
void test02() {vector<Person>p;Person p1("复合工艺", 28);Person p2("粉色", 9);Person p3("得分·", 45);p.push_back(p2);p.push_back(p1);p.push_back(p2);p.push_back(p3);p.push_back(p3);vector<Person>::iterator it = adjacent_find(p.begin(), p.end());if (it == p.begin()) {cout << " no find" << endl;}else {cout << "find : " << it->_name<<it->_age<< endl;}
}
int main() {test01();test02();return 0;
}
BINARY_SEARCH
binary_search //二分法查找
bool binary_search(beg,end,value) //无序序列中不可用
#include<iostream>
#include<string>
#include<vector>
#include<algorithm>
using namespace std;
//binary_search //二分法查找 bool binary_search(beg,end,value)//无序序列中不可用void test01() {vector<int>v;for (int i = 0; i < 10; i++) {v.push_back(i);}if (binary_search(v.begin(),v.end(),7)) {cout << " no find" << endl;}else {cout << "find : " << endl;}
}
void test02() {}
int main() {test01();test02();return 0;
}
COUNT
count //统计元素个数 count(beg,end,value)
#include<iostream>
#include<string>
#include<vector>
#include<algorithm>
using namespace std;
//count //统计元素个数 count(beg,end,value)
class Person {
public:Person(string n, int a) {this->_name = n;this->_age = a;}bool operator==(const Person& p) {if (this->_name == p._name && this->_age == p._age) {return true;}else {return false;}}string _name;int _age;
};
void test01() {vector<int>v;v.push_back(0);v.push_back(0);v.push_back(0);v.push_back(3);v.push_back(3);int it=count(v.begin(), v.end(), 0);if (it==0) {cout << " no find" << endl;}else {cout << "find : " <<it<<" ge" << endl;}
}
void test02() {vector<Person>p;Person p1("复合工艺", 28);Person p2("粉色", 9);Person p3("得分·", 45);p.push_back(p2);p.push_back(p3);p.push_back(p2);p.push_back(p3);p.push_back(p3);int it = count(p.begin(), p.end(), p1);if (it == 0) {cout << " no find" << endl;}else {cout << "find : " << it << " ge" << endl;}
}
int main() {test01();test02();return 0;
}
COUNT_IF
count_if //按条件统计元素个数 count_if(beg,end,——Pred)
#include<iostream>
#include<string>
#include<vector>
#include<algorithm>
using namespace std;
//count_if //按条件统计元素个数 count_if(beg,end,_Pred)
class grater {
public:bool operator()(int val) {return val > 5;}
};class Person {
public:Person(string n, int a) {this->_name = n;this->_age = a;}/*bool operator==(const Person& p) {if (this->_name == p._name && this->_age == p._age) {return true;}else {return false;}}*/string _name;int _age;
};
class grater02 {
public:bool operator()(const Person &p) {return p._age > 5;}
};
void test01() {vector<int>v;for (int i = 0; i < 10; i++) {v.push_back(i);}int it=count_if(v.begin(), v.end(), grater());if (it==0) {cout << " no find" << endl;}else {cout << "find : " <<it<<" ge" << endl;}
}
void test02() {vector<Person>p;Person p2("粉色", 4);Person p3("得分·", 45);p.push_back(p2);p.push_back(p3);p.push_back(p2);p.push_back(p3);p.push_back(p3);int it = count_if(p.begin(), p.end(), grater02());if (it == 0) {cout << " no find" << endl;}else {cout << "find : " << it << " ge" << endl;}
}
int main() {test01();test02();return 0;
}
常用排序算法
sort //对容器内元素进行排序
random_shuffle //洗牌,指定范围内顺序变随机
merge //容器元素合并,并存储道另一容器中
reverse //反转指定范围的元素
SORT
sort //对容器内元素进行排序 sort(beg,end,_Pred/谓词)
#include<iostream>
#include<string>
#include<vector>
#include<algorithm>
#include<functional>
using namespace std;
/*
sort //对容器内元素进行排序
sort(beg,end,_Pred/谓词)
*/
class grater {
public:bool operator()(int v1,int v2) {return v1>v2;}
};class Person {
public:Person(string n, int a) {this->_name = n;this->_age = a;}/*bool operator==(const Person& p) {if (this->_name == p._name && this->_age == p._age) {return true;}else {return false;}}*/string _name;int _age;
};
class grater02 {
public:bool operator()(const Person &p1,const Person &p2) {return p1._age > p2._age;}
};
void test01() {vector<int>v;for (int i = 0; i < 10; i++) {v.push_back(i);}for (vector<int>::iterator it = v.begin(); it != v.end(); it++) {cout << *it << " ";}cout << endl;sort(v.begin(), v.end(), grater());for (vector<int>::iterator it = v.begin(); it != v.end(); it++) {cout << *it << " ";}cout << endl;v.push_back(3);sort(v.begin(), v.end(), greater<int>());for (vector<int>::iterator it = v.begin(); it != v.end(); it++) {cout << *it << " ";}cout << endl;
}
void test02() {vector<Person>v;Person p0("fsdef", 23);Person p1("复合工艺", 28);Person p2("粉色", 9);Person p3("得分·", 45);Person p4("啊上务", 53);v.push_back(p0);v.push_back(p1);v.push_back(p2);v.push_back(p3);v.push_back(p4);for (vector<Person>::iterator it = v.begin(); it != v.end(); it++) {cout <<"name: " << it->_name << "\tage:" << it->_age << endl;}cout << endl;sort(v.begin(), v.end(), grater02());for (vector<Person>::iterator it = v.begin(); it != v.end(); it++) {cout << "name: " << it->_name << "\tage:" << it->_age << endl;}cout << endl;
}
int main() {test01();test02();return 0;
}
RANDOM_SHUFFLE
random_shuffle //洗牌,指定范围内顺序变随机
random_shuffle(beg,end)——加上随机数种子
#include<iostream>
#include<string>
#include<vector>
#include<algorithm>
#include<functional>
#include<ctime>
using namespace std;class Person {
public:Person(string n, int a) {this->_name = n;this->_age = a;}string _name;int _age;
};
void test01() {srand((unsigned int)time(NULL));//加上随机数种子vector<int>v;for (int i = 0; i < 10; i++) {v.push_back(i);}for (vector<int>::iterator it = v.begin(); it != v.end(); it++) {cout << *it << " ";}cout << endl;random_shuffle(v.begin(), v.end());for (vector<int>::iterator it = v.begin(); it != v.end(); it++) {cout << *it << " ";}cout << endl;}
void test02() {srand((unsigned int)time(NULL));//加上随机数种子vector<Person>v;Person p0("fsdef", 2);Person p1("复合工艺", 28);Person p2("粉色", 29);Person p3("得分·", 45);Person p4("啊上务", 53);v.push_back(p0);v.push_back(p1);v.push_back(p2);v.push_back(p3);v.push_back(p4);for (vector<Person>::iterator it = v.begin(); it != v.end(); it++) {cout <<"name: " << it->_name << "\tage:" << it->_age << endl;}cout << endl;random_shuffle(v.begin(), v.end());for (vector<Person>::iterator it = v.begin(); it != v.end(); it++) {cout << "name: " << it->_name << "\tage:" << it->_age << endl;}cout << endl;
}
int main() {test01();test02();return 0;
}
MERGE
merge //容器元素合并,并存储道另一容器中 PS:两个容器必须是有序的
merge(v1.beg ,v1.end ,v2.beg ,v2.end ,iterator dest) iterator dest目标容器开始迭代器
自定义类型失败
#include<iostream>
#include<string>
#include<vector>
#include<algorithm>
#include<functional>
using namespace std;void test01() {vector<int>v;vector<int>v1;vector<int>vv;for (int i = 0; i < 4; i++) {v.push_back(i);}for (int i = 0; i < 6; i++) {v1.push_back(i+7);}vv.resize(v.size() + v1.size()); //先开辟空间merge(v.begin(), v.end(), v1.begin(), v1.end(), vv.begin());for (vector<int>::iterator it = vv.begin(); it != vv.end(); it++) {cout << *it << " ";}cout << endl;
}int main() {test01();return 0;
}
REVERSE
reverse //反转指定范围的元素 reverse(beg,end)
#include<iostream>
#include<string>
#include<vector>
#include<algorithm>
#include<functional>
using namespace std;class Person {
public:Person() {};Person(string n, int a) {this->_name = n;this->_age = a;}string _name;int _age;
};
void test01() {vector<int>v;for (int i = 0; i < 10; i++) {v.push_back(i);}for (vector<int>::iterator it = v.begin(); it != v.end(); it++) {cout << *it << " ";}cout << endl << "revers after:" << endl;reverse(v.begin(), v.end());for (vector<int>::iterator it = v.begin(); it != v.end(); it++) {cout << *it << " ";}cout << endl;
}
void test02() {vector<Person>v;Person p0("fsdef", 2);Person p1("复合工艺", 28);Person p2("粉色", 29);Person p3("得分·", 45);Person p4("啊上务", 53);v.push_back(p0);v.push_back(p1);v.push_back(p2);v.push_back(p3);v.push_back(p4);for (vector<Person>::iterator it = v.begin(); it != v.end(); it++) {cout << "name: " << it->_name << "\tage:" << it->_age << endl;}cout << endl << "revers after:" << endl;reverse(v.begin(), v.end());for (vector<Person>::iterator it = v.begin(); it != v.end(); it++) {cout << "name: " << it->_name << "\tage:" << it->_age << endl;}
}
int main() {test01();test02();return 0;
}
常用拷贝和替换算法
copy //容器内指定范围拷贝到另一容器中
replace //指定范围 旧元素改为新元素
replace_if //指定范围 旧元素替换为新元素
swap //互换两个容器元素
COPY
copy //容器内指定范围拷贝到另一容器中 copy(beg,end,iterator dest) ---iterator dest目标起始迭代器
#include<iostream>
#include<string>
#include<vector>
#include<algorithm>
#include<functional>
using namespace std;
/*
copy(beg,end,iterator dest)
*/
void myprint(int val) {cout << val << " ";
}
void test01() {vector<int>v;vector<int>v2;for (int i = 0; i < 10; i++) {v.push_back(i);}v2.resize(v.size());//开辟空间copy(v.begin(), v.end(),v2.begin());for_each(v2.begin(), v2.end(), myprint);cout << endl;
}
void test02() {}
int main() {test01();test02();return 0;
}
REPLACE
replace //指定范围 旧元素改为新元素 replace(beg,end,old val,new val)
#include<iostream>
#include<string>
#include<vector>
#include<algorithm>
#include<functional>
using namespace std;
/*
replace(beg,end,old val,new val)
*/
class Person {
public:Person() {};Person(string n, int a) {this->_name = n;this->_age = a;}bool operator==(const Person &p) {if (this->_name == p._name && this->_age == p._age) {return true;}else {return false;}}string _name;int _age;
};
void myprint(int val) {cout << val << " ";
}
void myprint02(Person p) {cout << "name: " << p._name << "\tage:" << p._age << endl;
}
void test01() {vector<int>v;for (int i = 0; i < 10; i++) {int t = i;if ((t % 2) == 0) {v.push_back(i);}else {v.push_back(0);}}for_each(v.begin(), v.end(), myprint);cout << endl;replace(v.begin(), v.end(),0,3000);for_each(v.begin(), v.end(), myprint);cout << endl;
}
void test02() {vector<Person>v;Person p2("粉色", 29);Person p3("得 分·", 45);v.push_back(p2);v.push_back(p2);v.push_back(p3);for_each(v.begin(), v.end(), myprint02);cout << endl;replace(v.begin(), v.end(), p2, p3);for_each(v.begin(), v.end(), myprint02);cout << endl;
}
int main() {test01();test02();return 0;
}
REPLACE_IF
replace_if //指定范围 旧元素替换为新元素 replace_if(beg,end,_pred,new val)
#include<iostream>
#include<string>
#include<vector>
#include<algorithm>
#include<functional>
using namespace std;
/*
replace_if(beg,end,_pred,new val)
*/
class Person {
public:Person(string n, int a) {this->_name = n;this->_age = a;}bool operator==(const Person &p) {if (this->_name == p._name && this->_age == p._age) {return true;}else {return false;}}string _name;int _age;
};
void myprint(int val) {cout << val << " ";
}
class greater5 {
public:bool operator()(int val) {return val > 5;}
};
class greater30 {
public:bool operator()(Person &p2) {return p2._age<30;}
};
void myprint02(Person p) {cout << "name: " << p._name << "\tage:" << p._age << endl;
}
void test01() {vector<int>v;for (int i = 0; i < 10; i++) {v.push_back(i);}for_each(v.begin(), v.end(), myprint);cout << endl;replace_if(v.begin(), v.end(), greater5(), 3000);for_each(v.begin(), v.end(), myprint);cout << endl;
}
void test02() {vector<Person>v;Person p2("粉色", 29);Person p3("得 分·", 45);v.push_back(p2);v.push_back(p2);v.push_back(p3);for_each(v.begin(), v.end(), myprint02);cout << endl;replace_if(v.begin(), v.end(), greater30(), p3);for_each(v.begin(), v.end(), myprint02);cout << endl;
}
int main() {test01();test02();return 0;
}
SWAP
swap //互换两个容器元素 swap(container c1,container c2)
#include<iostream>
#include<string>
#include<vector>
#include<algorithm>
#include<functional>
using namespace std;
/*
replace_if(beg,end,_pred,new val)
*/
void myprint(int val) {cout << val << " ";
}
void test01() {vector<int>v;vector<int>v1;for (int i = 0; i < 10; i++) {v.push_back(i);if(i<5){v1.push_back(0);}}cout << "swap before:" << endl;for_each(v.begin(), v.end(), myprint);cout << endl;for_each(v1.begin(), v1.end(), myprint);cout << endl;cout << "swap after:" << endl;swap(v, v1);for_each(v.begin(), v.end(), myprint);cout << endl;for_each(v1.begin(), v1.end(), myprint);cout << endl;
}
void test02() {}
int main() {test01();test02();return 0;
}