一:容器的介绍
1、序列容器:vector ,list,deque,string
2、关联容器:map,set,multiset
3、其他:stack,queue
二:用法
1.set的用法
#include <set>
#include <iostream>
using namespace std;
/*
set/multiset会根据待定的排序准则,自动将元素排序。两者不同在于前者不允许元素重复,而后者允许。
1) 不能直接改变元素值,因为那样会打乱原本正确的顺序,要改变元素值必须先删除旧元素,则插入新元素
2) 不提供直接存取元素的任何操作函数,只能通过迭代器进行间接存取,而且从迭代器角度来看,元素值是常数
3) 元素比较动作只能用于型别相同的容器(即元素和排序准则必须相同)
set模板原型://Key为元素(键值)类型
template <class Key, class Compare=less<Key>, class Alloc=STL_DEFAULT_ALLOCATOR(Key) >
从原型可以看出,可以看出比较函数对象及内存分配器采用的是默认参数,因此如果未指定,它们将采用系统默认方式,
另外,利用原型,可以有效地辅助分析创建对象的几种方式
*/
int _tmain(int argc, _TCHAR* argv[])
{
//增加
set<int> setKey;
setKey.insert(10);
setKey.insert(20);
setKey.insert(10);
setKey.insert(11);
setKey.insert(12);
setKey.insert(15);
//删除
setKey.erase(10);
//查找
set<int>::iterator result = setKey.find(12);
if(result == setKey.end())
{
cout << "not found" << endl;
}else
{
cout << "find" << endl;
}
return 0;
}
2、multiset 的用法
#include <set>
#include <iostream>
using namespace std;
int _tmain(int argc, _TCHAR* argv[])
{
multiset<int> ms;
ms.insert(10);
ms.insert(13);
ms.insert(11);
ms.insert(19);
ms.insert(13);
ms.insert(19);
ms.insert(19);
// 打印数据
multiset<int>::iterator i, iend;
iend = ms.end();
for (i=ms.begin(); i!=iend; ++i)
cout << *i << ' ';
cout << endl;
//反向遍历
multiset<int>::reverse_iterator ri, riend;
riend = ms.rend();
for (ri=ms.rbegin(); ri!=riend; ++ri)
cout << *ri << ' ';
cout << endl;
//查找
int v = 19;
multiset<int>::iterator i_v = ms.find(v);
cout << *i_v << endl;
// equal_range 搜索元素 13
v = 13;
pair<multiset<int>::iterator, multiset<int>::iterator> p = ms.equal_range(v);
cout << "大于等于" << v << "的第一个元素 ( X >= k ) 为:" << *p.first << endl;
cout << "大于" << v << "的第一个元素( x > k ) 为:" << *p.second << endl;
// 打印重复键值元素 13
multiset<int>::iterator j;
cout << "键值为" << v << "的所有元素为:";
for (j=p.first; j!=p.second; ++j)
cout << *j << ' ';
cout << endl << endl;
//其他的函数的用法
return 0;
}
//其他的函数的用法
/*
下面的示例程序以学生记录为元素插入 multiset 容器,比较函数是以年龄作比较,利用 size 和 count 函数统计了元素个数和某个键值下的元素个数
*/
//-------------------------------------------------------- multiset 的其他函数用法
#pragma warning(disable:4786)
#include <set>
#include <iostream>
using namespace std;
// 学生结构体
struct Student
{
char* name;
int year;
char* addr;
};
// 比较函数
struct StudentLess
{
bool operator()(const Student& s1, const Student& s2) const
{
return s1.year < s2.year; // 比较学生年龄
}
};
int main()
{
Student stuArray[] = {
{"张三", 23, "北京"},
{"李四", 24, "浙江"},
{"王五", 25, "上海"},
{"何亮", 22, "武汉"},
{"何生亮", 23, "深圳"}
};
// 创建 multiset 对象 ms
multiset<Student, StudentLess> ms(stuArray, stuArray + 5, StudentLess());
// 统计
cout << "学生人数:" << ms.size() << endl << endl;
cout << "年龄为21岁的学生人数:" << ms.count(stuArray[0]) << endl << endl;
// 打印元素
multiset<Student, StudentLess>::iterator i, iend;
iend = ms.end();
cout << "姓名 " << "年龄 " << "地址 \n";
for (i=ms.begin(); i!=iend; ++i)
cout << (*i).name << " " << (*i).year << " " << (*i).addr << endl;
cout << endl;
return 0;
}
/* 从 count 函数输出可看到,真正作为键值的是 Student 结构体中的 year 变量值,而不是一个 Student 元素。因此,虽然 count(stuArray[0]) 传递的是一个 stuArray[0] 元素,但并不是统计等于 stuArray[0] 的元素个数,而是统计等于 stuArray[0].year 的元素个数,即年龄为 21 岁的学生人数为 2。
*/