STL之set && multiset
一、set
在了解关联容器set之前,让我们先来看看下面这个例子,并猜测该例子输出什么:
// stl/set1.cpp #include <iostream>
#include <set> int main()
{
//type of the collection
typedef std::set<int> IntSet; IntSet coll; //set container for int values /* insert elements from 1 to 6 in arbitray order
*- value 1 gets inserted twice
*/
coll.insert();
coll.insert();
coll.insert();
coll.insert();
coll.insert();
coll.insert();
coll.insert(); /* print all elements
*- iterate over all elements
*/
IntSet::const_iterator pos;
for (pos = coll.begin(); pos != coll.end(); ++pos) {
std::cout << *pos << ' ';
}
std::cout << std::endl;
}
其中,输出的结果为:1 2 3 4 5 6
下面,我们根据该输出结果对关联容器set做一个分析:
1. set元素的唯一性;
2. set默认按照从小到大排序;This type uses the default sorting criterion, which sorts the elements by using operator <.
如果你想要改变它的排序方法,需要传递额外的参数,例如:
typedef set<int,greater<int> > IntSet;
Note that you have to put a space between the two ">" characters. ">>" would be parsed as shift operator, which would result in a syntax error.
二、multiset
If you want to use a multiset rather than a set, you need only change the type of the container (the header file remains the same):
typedef multiset<int> IntSet;
A multiset allows duplicates, so it would contain two elements that have value 1. Thus, the output of the program would change to the following:
1 1 2 3 4 5 6 例如:
// stl/mmap1.cpp #include <iostream>
#include <map>
#include <string>
using namespace std; int main()
{
//type of the collection
typedef multimap<int, string> IntStringMMap; IntStringMMap coll; //set container for int/string values //insert some elements in arbitrary order
//- a value with key 1 gets inserted twice
coll.insert(make_pair(,"tagged"));
coll.insert(make_pair(,"a"));
coll.insert(make_pair(,"this"));
coll.insert(make_pair(,"of"));
coll.insert(make_pair(,"strings"));
coll.insert(make_pair(,"is"));
coll.insert(make_pair(,"multimap")); /* print all element values
*- iterate over all elements
*- element member second is the value
*/
IntStringMMap::iterator pos;
for (pos = coll.begin(); pos != coll.end(); ++pos) {
cout << pos->second << ' ';
}
cout << endl;
}
STL之set && multiset的更多相关文章
- C++ STL set和multiset的使用
C++ STL set和multiset的使用 std::set<int> s;那个s这个对象里面存贮的元素是从小到大排序的,(因为用std::less作为比较工具.) 1,set的含义是 ...
- STL Set和multiset 容器
STL Set和multiset 容器 set/multiset的简介 set是一个集合容器,其中所包含的元素是唯一的,集合中的元素按一定的顺序排列. 元素插入过程是按排序规则插入,所以不能指定插入位 ...
- STL - set和multiset
set/multiset的简介 set是一个集合容器,其中所包含的元素是唯一的,集合中的元素按一定的顺序排列.元素插入过程是按排序规则插入,所以不能指定插入位置. set采用红黑树变体的数据结构实现, ...
- STL之set&multiset使用简介
关于set,必须说明的是set关联式容器.set作为一个容器也是用来存储同一数据类型的数据类型,并且能从一个数据集合中取出数据,在set中每个元素的值都唯一,而且系统能根据元素的值自动进行排序.应该注 ...
- c++ STL -- set和multiset
set和multiset 1.结构 set和multiset会根据特定的排序原则将元素排序.两者不同之处在于,multisets允许元素重复,而set不允许重复. 只要是assignable.copy ...
- STL:set/multiset用法详解
集合 使用set或multiset之前,必须加入头文件<set> Set.multiset都是集合类,差别在与set中不允许有重复元素,multiset中允许有重复元素. sets和mul ...
- STL使用————SET&MULTISET
SET函数的基本用法 by hhl 使用set的好处 1. 当增加元素后,集合会自动删重并从小到大排列(时间比快排还快)2. 相当于一棵伸展树(能快速求出后继) 使用基础 #include<se ...
- C++ STL——set和multiset
目录 一 set和multiset 二 对组pair 注:原创不易,转载请务必注明原作者和出处,感谢支持! 注:内容来自某培训课程,不一定完全正确! 一 set和multiset set和multis ...
- 转自http://blog.sina.com.cn/daylive——C++ STL set&multiset
C++ STL set和multiset的使用 1,set的含义是集合,它是一个有序的容器,里面的元素都是排序好的,支持插入,删除,查找等操作,就 像一个集合一样.所有的操作的都是严格在logn时间 ...
随机推荐
- JetBrains公司的IDE使用技巧
1.自定义Live Templates: 点击+添加自己的.最后记住要点击,change或default来设置在哪些文件上使用代码片段.
- sublime text There are no packages 解决!
1.问题如下图 解决如下: 1.取得sublime.wbond.net的IPv4地址.在命令提示符中输入以下命令: ping sublime.wbond.net 获得 pv 4 ip 2.C ...
- Ubuntu安装google chrome过程
Ubuntu安装google chrome过程: # wget https://dl.google.com/linux/direct/google-chrome-stable_current_amd6 ...
- jQuery图片滑动
一个非常简单实用的jQuery插件 可以用在页面的顶部广告展示 http://slidesjs.com/ 一个需要注意的问题, 就是在手机等客户端(IOS8以上), 使用此插件时, 经常会触发插件的r ...
- 安装 mysql
1.安装mysql客户端 yum install mysql 2.安装mysql 服务器端 yum install mysql-server 3.配置 mysql字符集 /etc/my.cnf 加入 ...
- sublime2开发Python的编码问题
在sublime2文本编辑器直接开发python程序会出现错误 Traceback (most recent call last): File ".\sublime_plugin.py&qu ...
- Netty笔记--ByteBuf释放
参考资料:http://www.maljob.com/pages/newsDetail.html?id=394 参考资料:http://www.blogjava.net/liuguly/archive ...
- JS 输出与变量
1. JS的输出 innerHTML: <!DOCTYPE html> <html> <head> <meta charset="utf-8&quo ...
- cocos2d-x把json数据解析到数组或字典中(libjson库)
以前在cocos2d-x项目中用到json解析,集成了libjson库后发现网上提供的解析方法大多是在解析过程中取得值,并没有将解析结果有效的保存起来,于是摸索一番,把解析结果根据数据格式存到数组或字 ...
- Spring <context:annotation-config/>
在基于主机方式配置Spring的配置文件中,你可能会见到<context:annotation-config/>这样一条配置,他的作用是式地向 Spring 容器注册 AutowiredA ...