set容器
cpp
#define _CRT_SECURE_NO_WARNINGS
#include<iostream>
#include<string>
#include<set>
#include<algorithm>
using namespace std;
//set不给有重复的值,但是插入相同的数值不会报错,只是不会插入进去
void test01()
{
set<int>s;
set<int>::iterator it;
it++;
it--;
//it + 2;
//双向迭代器
}
void printset(set<int>& s)
{
for (set<int>::iterator it = s.begin(); it != s.end(); it++)
{
cout << *it << endl;
}
}
void test02()
{
set<int>s;
s.insert(10);
s.insert(2);
s.insert(5);
printset(s);//2 5 10//自身规则进行排序,升序
}
//改变set容器的规则,改为降序
struct myfunc
{
bool operator()( int val1, int val2)const//添加 const 限定符,以表明该成员函数不会修改对象的状态
//否则会报错:具有类型"const myfunc"的表达式会丢失一些 const-volatile 限定符以调用"bool myfunc::operator ()(int,int)"
{
return val1 > val2;
}
};
void printset2(set<int, myfunc>& s)
{
for (set<int, myfunc>::iterator it = s.begin(); it != s.end(); it++)
{
cout << *it << endl;
}
}
void test03()
{
set<int, myfunc> s;
s.insert(10);
s.insert(2);
s.insert(5);
printset2(s);
s.erase(2);
printset2(s);
}
//利用算法打印
void print(int val)
{
cout << val << " ";
}
void test04()
{
set<int>s;
s.insert(10);
s.insert(2);
s.insert(5);
for_each(s.begin(), s.end(), print);
//不能用sort
}
//find(key)//查找键key是否存在,返回该键的迭代器,如不存在,返回set.end();
//lower_bound(keyelem)//返回第一个key>=keyelem元素的迭代器
//upper_bound(keyelem)//返回第一个key>keyelem元素的迭代器
void test05()
{
set<int>s;
s.insert(10);
s.insert(2);
s.insert(5);
set<int>::iterator it=s.find(3);
if (it == s.end())
{
cout << "不存在" << endl;;
}
else
{
cout << " 查找成功" << endl;
}
it = s.lower_bound(2);//查找大于等于2的最小数
if (it == s.end())
{
cout << "查找失败" << endl;
}
else
{
cout << "查找成功:" << *it << endl;
}
it = s.upper_bound(2);//查找大于2的最小数
if (it == s.end())
{
cout << "查找失败" << endl;
}
else
{
cout << "查找成功:" << *it << endl;
}
//返回大于等于2的两个最小的数,如果有2那么就返回2和大于2的最小数
pair<set<int>::iterator, set<int>::iterator>ret = s.equal_range(2);
cout << *(ret.first) << endl;
cout << *(ret.second) << endl;
multiset<int>s1;
s1.insert(4);
s1.insert(4);
s1.insert(4);
s1.insert(2);
s1.insert(3);
cout << s1.count(4) << endl;
}
class maker
{
public:
maker(string name, int age)
{
this->name = name;
this->age = age;
}
public:
string name;
int age;
};
//存储对象时,要告诉set规则
struct makerfunc
{
bool operator()(const maker& m1, const maker& m2)const
{
//return m1.age > m2.age;
return m1.name>m2.name;
}
};
void test06()
{
set<maker,makerfunc>s;
s.insert(maker("aaa", 53));
s.insert(maker("bbb", 20));
s.insert(maker("ccc", 33));
for (set<maker,makerfunc>::iterator it = s.begin(); it != s.end(); it++)
{
cout << (*it).name << " " << (*it).age << endl;
}
}
int main()
{
test06();
system("pause");
return 0;
}
map容器
cpp
#define _CRT_SECURE_NO_WARNINGS
#include<iostream>
#include<string>
#include<map>
#include<algorithm>
using namespace std;
void test01()
{
map<int, string>::iterator it;
it++;
it--;
//it + 2;//err,双向迭代器
}
template <class T>
void printmap(T& m)
{
for (map<int, string>::iterator it = m.begin(); it != m.end(); it++)
{
cout << "key:" << (*it).first << " " << "value:" << (*it).second << endl;
}
}
struct myfunc
{
bool operator()(int ker1, int key2)const
{
return ker1 > key2;
}
};
void test02()
{
map<int, string,myfunc>mymap;
//1:
mymap.insert(pair<int, string>(3, "aaa"));
//2:
mymap.insert(make_pair(6, "bbb"));
mymap.insert(make_pair(3,"ccc"));//不允许key值一样的
//3:
mymap[4] = "ddd";//4是键值,ddd是实值
printmap(mymap);
}
//[]方式存入数据,如果没有实值,那么键值也是存在的
void test03()
{
map<int, string, myfunc>mymap;
mymap.insert(pair<int, string>(3, "aaa"));
mymap.insert(make_pair(6, "bbb"));
mymap[4] = "ddd";
printmap(mymap);
cout << "size:" << mymap.size() << endl;//3
mymap[100];//插入键值,返回实值
cout << "size:" << mymap.size() << endl;//4
}
void test04()
{
map<int, string>m;
m[1] = "aaa";
m[3] = "ccc";
m[6] = "bbb";
map<int, string>::iterator it = m.find(3);
if (it == m.end())
{
cout << "查找失败" << endl;
}
else
{
cout << "查找成功" << "key:" << it->first << " " << "value:"<<it->second << endl;
}
it = m.lower_bound(3);//键值大于等于3
if (it == m.end())
{
cout << "查找失败" << endl;
}
else
{
cout << "查找成功" << "key:" << it->first << " " << "value:" << it->second << endl;
}
it = m.upper_bound(3);//键值大于3
if (it == m.end())
{
cout << "查找失败" << endl;
}
else
{
cout << "查找成功" << "key:" << it->first << " " << "value:" << it->second << endl;
}
pair<map<int, string>::iterator, map<int, string>::iterator>ret = m.equal_range(3);
if (ret.first != m.end()) {
cout << "key:" << ret.first->first << "value:" << ret.second->second << endl;
}
if (ret.second != m.end()) {
cout << "key:" << ret.second->first << "value:" << ret.second->second << endl;
}
}
int main()
{
test04();
system("pause");
return 0;
}