优先队列:priority_queue
实现的头文件
代码如下;
#include
#include
#include
using namespace std;
int main()
{
//priority_queue<int, vector > p1; //等价于 默认从大到小排序
//priority_queue<int, vector, less > p1;
priority_queue<int, vector, greater > p1;
p1.push(6);
p1.push(7);
p1.push(1);
p1.push(0);
p1.push(4);
p1.push(5);
cout << p1.top() << endl;
while (!p1.empty())
{
cout << p1.top() << " ";
p1.pop();
}
cout << endl;
return 0;
}
set容器:
是关联式容器,作为一个容器也是用来存储同一数据类型的。在set中每个元素的排序都是唯一的。而且系统能根据元素的值进行自动排序。set容器内部采用的是非常高效的平衡检索二叉树——红黑树。也称RB树。
代码如下:
#include
#include
using namespace std;
class Person
{
friend class com;
private:
int id;
char name[32];
int age;
public:
Person(int i, char *n, int a);
void showinfo() const;
bool operator<(const Person &p) const;
bool operator>(const Person &p) const;
};
Person::Person(int i, char *n, int a)
{
id = i;
strcpy(name, n);
age = a;
}
void Person::showinfo() const
{
cout << id << " " << name << " " << age << endl;
}
bool Person::operator<(const Person &p) const
{
/if (this->id > p.id)
{
return true;
}
else
{
return false;
}/
return this->id < p.id;
}
bool Person::operator>(const Person &p) const
{
//return this->id > p.id;
return this->age > p.age;
}
class com
{
public:
bool operator()(const Person &p1, const Person &p2) const //重载函数调用运算符
{
return p1.id > p2.id;
}
};
int main()
{
//set s1;
//set<int, less > s1; //默认是从小到大排序
set<int, greater > s1;
s1.insert(1);
s1.insert(5);
s1.insert(3);
s1.insert(7);
s1.insert(2);
s1.insert(0);
for (set<int>::iterator it = s1.begin(); it != s1.end(); it++)
{
cout << *it << " ";
}
cout << endl;
cout << "****************************************************" << endl;
Person p1(4, "aaaa", 20);
Person p2(5, "zzzz", 34);
Person p3(1, "ffff", 30);
Person p4(3, "dddd", 26);
Person p5(7, "eeee", 39);
Person p6(6, "gggg", 26);
Person p10(4, "abcd", 22);
//set<Person, less<Person> > s2; //需要重载<
//set<Person, greater<Person> > s2; //需要重载>
multiset<Person, com> s2;
/*com c;
c.operator()(p1, p2); ==== c(p1, p2) 函数对象 仿函数*/
s2.insert(p1);
s2.insert(p2);
s2.insert(p3);
s2.insert(p4);
s2.insert(p5);
s2.insert(p6);
s2.insert(p10);
for (multiset<Person, less<Person> >::iterator it = s2.begin(); it != s2.end(); it++)
{
it->showinfo();
}
#if 0
cout << “****” << endl;
s2.erase(s2.begin());
for (set<Person, less >::iterator it = s2.begin(); it != s2.end(); it++)
{
it->showinfo();
}
cout << "****" << endl;
s2.erase(s2.begin(), ++s2.begin());
for (set<Person, less<Person> >::iterator it = s2.begin(); it != s2.end(); it++)
{
it->showinfo();
}
cout << "****" << endl;
Person p7(4, "cccc", 22); //按照id删除
s2.erase(p7);
for (set<Person, less<Person> >::iterator it = s2.begin(); it != s2.end(); it++)
{
it->showinfo();
}
cout << "****" << endl;
Person p8(3, "ascd", 33);
set<Person, com>::iterator it = s2.find(p8);
if (it == s2.end()) //找不到
{
cout << "元素不存在" << endl;
}
else
{
it->showinfo();
}
int num = s2.count(p8);
cout << num << endl;
cout << "****" << endl;
it = s2.lower_bound(p8); //返回大于等于p8
it->showinfo();
cout << "****" << endl;
it = s2.upper_bound(p8); //返回大于p8
it->showinfo();
cout << "****" << endl;
pair<set<Person, com>::iterator, set<Person, com>::iterator> pa; //组对
pa = s2.equal_range(p8);
pa.first->showinfo();
pa.second->showinfo();
#endif
return 0;
}
map容器:
标准的关联式容器,一个map就是一个键值对(key,value),提供了快速检索能力。
map中key值唯一。集合中元素按一定顺序排列。具体实现也是采用红黑变体的平衡二叉树的数据结构。再插入和删除操作上比vector容器快。支持[ ]操作符。
代码如下:
#include
using namespace std;
int main()
{
map<int, string> m;
//pair组对插入
m.insert(pair<int, string>(1, "aaaaa"));
m.insert(pair<int, string>(2, "bbbbb"));
//make_pair
m.insert(make_pair(3, "ccccc"));
m.insert(make_pair(10, "ddddd"));
//value_type map类中的静态成员函数
m.insert(map<int, string>::value_type(7, "eeeee"));
m.insert(map<int, string>::value_type(4, "fffff"));
//直接赋值
m[6] = "ggggg"; //6键 ggggg值
m[9] = "hhhhh";
for (map<int, string>::iterator it = m.begin(); it != m.end(); it++)
{
cout << it->first << " : " << it->second << endl;
}
pair<map<int, string>::iterator, bool> p;
p = m.insert(make_pair(3, "jjjjj"));
if (p.second)
{
cout << "插入成功" << endl;
}
else
{
cout << "插入失败" << endl;
}
m[3] = "jjjjj"; //不会失败,直接覆盖
cout << "****" << endl;
for (map<int, string>::iterator it = m.begin(); it != m.end(); it++)
{
cout << it->first << " : " << it->second << endl;
}
return 0;
}
算法:
算法的头文件,,和组成。
所有STL文件中最大的,涉及的功能包括比较,交换,查找,遍历,复制,修改,翻转,排序,合并。
很小,只包括几个模板函数,如加法乘法的一些操作。
定义了一些模板类,用于声明函数对象。
非机动性算法:
for_each() 对每个元素执行某操作
count() 返回元素个数
min_element() 返回最小值元素
max_element() 返回最大值元素
find() 搜索等于某个值的第一个元素
search() 搜索某个子区间第一次出现的位置
equal 判断两个区间是否相等
变动性算法
for_each() 对每个元素执行某操作
copy() 从第一个元素开始,复制某段区间
transform() 变动元素,将两个区间的元素合并
merge() 合并两个区间
fill() 以给定值替换某个元素
replace() 将具有特定值得元素替换为为另一个值。
其他的包括移除性算法,变序性算法,排序性算法,已序区间算法,数值算法就不一一列举。
代码如下:
遍历算法:
#include
#include
using namespace std;
void print1(char ch)
{
cout << ch;
}
class print2
{
public:
void operator()(char ch)
{
cout << ch;
}
};
char print3(char &ch)
{
return ch + 1;
}
int main()
{
char *ptr = “helloworld”;
vector v1(ptr, ptr + strlen(ptr));
/*for_each遍历的过程中不能修改容器内的数据*/
//for_each(v1.begin(), v1.end(), print1); //print1函数指针 回调函数
for_each(v1.begin(), v1.end(), print2()); //print2()函数对象
cout << endl;
/*transform遍历的过程中可以修改容器内的数据*/
transform(v1.begin(), v1.end(), v1.begin(), print3);
for_each(v1.begin(), v1.end(), print2()); //print2()函数对象
cout << endl;
return 0;
}
查找算法:
#include
#include
#include
using namespace std;
void print(int a)
{
cout << a << " ";
}
bool greater_three(int n)
{
return n > 3;
}
int main()
{
multiset s;
srand(time(NULL));
for (int i = 0; i < 10; i++)
{
s.insert(rand() % 5);
}
for_each(s.begin(), s.end(), print);
cout << endl;
multiset<int>::iterator it;
it = adjacent_find(s.begin(), s.end()); //查找相邻的重复的元素
if (it == s.end())
{
cout << "不存在" << endl;
}
else
{
cout << *it << endl;
}
bool ret = binary_search(s.begin(), s.end(), 3); //在有序序列中查找
if (ret)
{
cout << "3存在" << endl;
}
else
{
cout << "3不存在" << endl;
}
int num = count(s.begin(), s.end(), 4); //统计4的个数
cout << num << endl;
num = count_if(s.begin(), s.end(), greater_three); //统计大于三的个数
cout << num << endl;
it = find(s.begin(), s.end(), 3); //查找等于3
if (it == s.end())
{
cout << "不存在" << endl;
}
else
{
cout << *it << endl;
}
it = find_if(s.begin(), s.end(), greater_three); //查找第一个大于3的元素
if (it == s.end())
{
cout << "不存在" << endl;
}
else
{
cout << *it << endl;
}
return 0;
}
排序算法:
#include
#include
using namespace std;
void print(int n)
{
cout << n << " ";
}
int main()
{
int a1[] = {1, 3, 5, 7, 9};
vector v1(a1, a1 + sizeof(a1) / sizeof(a1[0]));
int a2[] = {2, 4, 6, 8};
vector<int> v2(a2, a2 + sizeof(a2) / sizeof(a2[0]));
vector<int> v3;
v3.resize(9);
merge(v1.begin(), v1.end(), v2.begin(), v2.end(), v3.begin());
for_each(v3.begin(), v3.end(), print);
cout << endl;
random_shuffle(v3.begin(), v3.end());
for_each(v3.begin(), v3.end(), print);
cout << endl;
sort(v3.begin(), v3.end(), less<int>());
for_each(v3.begin(), v3.end(), print);
cout << endl;
reverse(v3.begin(), v3.end());
for_each(v3.begin(), v3.end(), print);
cout << endl;
return 0;
拷贝算法:
#include
#include
using namespace std;
void print(int n)
{
cout << n << " ";
}
int main()
{
int a1[] = {1, 3, 5, 7, 9};
vector v1(a1, a1 + sizeof(a1) / sizeof(a1[0]));
int a2[] = {2, 4, 6, 8};
vector<int> v2(a2, a2 + sizeof(a2) / sizeof(a2[0]));
vector<int> v3;
v3.resize(9);
merge(v1.begin(), v1.end(), v2.begin(), v2.end(), v3.begin());
for_each(v3.begin(), v3.end(), print);
cout << endl;
random_shuffle(v3.begin(), v3.end());
for_each(v3.begin(), v3.end(), print);
cout << endl;
sort(v3.begin(), v3.end(), less<int>());
for_each(v3.begin(), v3.end(), print);
cout << endl;
reverse(v3.begin(), v3.end());
for_each(v3.begin(), v3.end(), print);
cout << endl;
return 0;
剩余算法:
#include
#include
#include
using namespace std;
void print(int n)
{
cout << n << " " ;
}
int main()
{
int a1[] = {1, 2, 3, 4, 5};
vector v1(a1, a1 + sizeof(a1) / sizeof(a1[0]));
int sum = accumulate(v1.begin(), v1.end(), 10);
cout << sum << endl;
fill(v1.begin(), v1.end(), 10);
for_each(v1.begin(), v1.end(), print);
cout << endl;
int a2[] = {1, 2, 3, 4, 5, 6};
vector<int> v2(a2, a2 + sizeof(a2) / sizeof(a2[0]));
int a3[] = {4, 5, 6, 7, 8};
vector<int> v3(a3, a3 + sizeof(a3) / sizeof(a3[0]));
vector<int> v4;
v4.resize(13);
set_union(v2.begin(), v2.end(), v3.begin(), v3.end(), v4.begin());
for_each(v4.begin(), v4.end(), print);
cout << endl;
return 0;
}
函数适配器:
STL中定义了大量的函数对象,但是需要对函数返回值进行进一步的简单计算,或者填上多余的参数,不能直接代入算法。函数适配器可以实现这些功能。同时,函数适配器可以分为4大类:绑定适配器,组合适配器,指针适配器,和成员函数适配器。
代码如下:
#include
#include
using namespace std;
bool Equal(string str)
{
return str == “cccc”;
}
int main()
{
vector v;
v.push_back("aaaa");
v.push_back("bbbb");
v.push_back("cccc");
v.push_back("dddd");
v.push_back("eeee");
vector<string>::iterator it;
//it = find_if(v.begin(), v.end(), Equal);
it = find_if(v.begin(), v.end(), bind1st(equal_to<string>(), "cccc")); //bind1st绑定器
if (it != v.end())
{
cout << *it << endl;
}
return 0;
}