目录
set容器
set的默认构造
set的插入与迭代器
set集合的元素排序
set集合的初始化及遍历
从小到大(默认情况下)
从大到小
仿函数
set的查找
pair的使用
multiset容器
map和multimap容器
map的插入与迭代器
map的大小
map的删除
map的查找
关联式容器(Associated containers),元素位置取决于特定的排序准则,和插入顺序无关
在c++STL中主要有set、multiset、map、multimap
set
set
set
multiset
multi set
multi set
//集合 元素唯一 自动排序 不能按照[]方式插入元素
//默认情况下是从小到大
void main91()
{
set set1;
for (int i = 0; i < 5; i++)
{
int tmp = rand();
set1.insert(tmp);
}
set1.insert(100);
set1.insert(100);
set1.insert(100);
for (set::iterator it = set1.begin(); it != set1.end(); it++)
{
cout << *it << " ";
}
cout << endl;
while (!set1.empty())
{
set::iterator it = set1.begin();
cout << *it << " ";
set1.erase(set1.begin());
}
}
void main92()
{
set> set1;
for (int i = 0; i < 5; i++)
{
int tmp = rand();
set1.insert(tmp);
}
set1.insert(100);
set1.insert(100);
set1.insert(100);
for (set>::iterator it = set1.begin(); it != set1.end(); it++)
{
cout << *it << " ";
}
cout << endl;
}
对于复杂的数据类型set集合是通过用户自定义来排序的,主要利用的就是仿函数这个机制。
//仿函数
struct FuncStudent
{
bool operator()(const Student& left, const Student& right) const
{
if (left.m_age < right.m_age)
{
return true;
}
else
{
return false;
}
}
};
void main93()
{
set set1;
Student s1("s1", 32);
Student s2("s2", 22);
Student s3("s3", 16);
Student s4("s4", 55);
Student s5("s5", 32);
set1.insert(s1);
set1.insert(s2);
set1.insert(s3);
set1.insert(s4);
set1.insert(s5);//两个一样的值
//如何知道插入的结果
//遍历
for (set::iterator it = set1.begin(); it != set1.end(); it++)
{
cout << it->m_age << "\t" << it->m_name << endl;
}
}
set
... //往setInt容器插入元素1,3,5,7,9
pair< set
set
set
//此时 *itBeg==5 而 *itEnd == 7
//如何判断insert的返回值
void main94()
{
set set1;
Student s1("s1", 32);
Student s2("s2", 22);
Student s3("s3", 16);
Student s4("s4", 55);
Student s5("s5", 32);
pair::iterator, bool> pair1;
pair1 = set1.insert(s1);
if (pair1.second)
{
cout << "插入s1成功" << endl;
}
else
{
cout << "插入s1失败" << endl;
}
set1.insert(s2);
set1.insert(s3);
set1.insert(s4);
pair1 = set1.insert(s5);//两个一样的值
if (pair1.second)
{
cout << "插入s5成功" << endl;
}
else
{
cout << "插入s5失败" << endl;
}
//遍历
for (set::iterator it = set1.begin(); it != set1.end(); it++)
{
cout << it->m_age << "\t" << it->m_name << endl;
}
}
void main101()
{
multiset set1;
int tmp;
cout << "请输入multiset集合的值:";
scanf("%d", &tmp);
while (tmp != 0)
{
set1.insert(tmp);
cout << "请输入multiset集合的值:";
scanf("%d", &tmp);
}
//遍历
for (multiset::iterator it = set1.begin(); it != set1.end(); it++)
{
cout << *it << " ";
}
cout << endl;
while (!set1.empty())
{
multiset::iterator it = set1.begin();
cout << *it << " ";
set1.erase(set1.begin());
}
}
假设 map
mapStu.insert( pair
mapStu.inset(make_pair(-1, “校长-1”));
mapStu.insert( map
mapStu[3] = “小刘";
mapStu[5] = “小王";
//map的添加/遍历/删除
void main111()
{
map map1;
//方法一
map1.insert(pair(1, "teacher01"));
map1.insert(pair(2, "teacher02"));
//方法二
map1.insert(make_pair(3, "teacher03"));
map1.insert(make_pair(4, "teacher04"));
//方法三
map1.insert(map::value_type(5, "teacher05"));
map1.insert(map::value_type(6, "teacher06"));
//方法四
map1[7] = "teacher07";
map1[8] = "teacher08";
//容器的遍历
for (map::iterator it = map1.begin(); it != map1.end(); it++)
{
cout << it->first << "\t" << it->second << endl;
}
cout << "遍历结束" << endl;
//容器的删除
while (!map1.empty())
{
map::iterator it = map1.begin();
cout << it->first << "\t" << it->second << endl;
map1.erase(it);
}
}
//插入的四种方法异同
//前三种返回值是pair
//前三种方法若key已经存在则报错
//方法四 若key已经存在则覆盖
void main112()
{
map map1;
//方法一
pair
void main113()
{
map map1;
//方法一
map1.insert(pair(1, "teacher01"));
map1.insert(pair(2, "teacher02"));
//方法二
map1.insert(make_pair(3, "teacher03"));
map1.insert(make_pair(4, "teacher04"));
//方法三
map1.insert(map::value_type(5, "teacher05"));
map1.insert(map::value_type(6, "teacher06"));
//方法四
map1[7] = "teacher07";
map1[8] = "teacher08";
//容器的遍历
for (map::iterator it = map1.begin(); it != map1.end(); it++)
{
cout << it->first << "\t" << it->second << endl;
}
cout << "遍历结束" << endl;
//map的查找
map::iterator it2 = map1.find(100);
if (it2 == map1.end())
{
cout << "key 100的值不存在" << endl;
}
else
{
cout << it2->first << "\t" << it2->second << endl;
}
//equal_range
pair::iterator, map::iterator> mypair = map1.equal_range(5);//返回两个迭代器 形成一个pair
//第一个迭代器>=5的位置
//第二个迭代器>5的位置
if (mypair.first == map1.end())
{
cout << "第一个迭代器不存在" << endl;
}
else
{
cout << mypair.first->first << "\t" << mypair.first->second << endl;
}
//使用第二个迭代器
if (mypair.second == map1.end())
{
cout << "第一个迭代器不存在" << endl;
}
else
{
cout << mypair.second->first << "\t" << mypair.second->second << endl;
}
}