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时间 ...
随机推荐
- 二、C# 数据类型
C#语言的基本类型包括8种整数类型.2种用于科学计算的二进制浮点类型.1种用于金融计算的十 进制浮点类型.1种布尔类型以及1种字符类型. 2.1 基本数值类型 C#中的基本数据类型都有关键字和它们关联 ...
- python3 解析apk图标
有两处值小点,一是如何解压缩,另一个是如何写文件,第二点上我找的是phthon2的代码,一直写文件的时候报不是字符串的问题,将打开方式加上"b“的模式搞定 print文件出来直接删除了,原因 ...
- VirtualBox开发环境的搭建详解(转)
VirtualBox开发环境的搭建详解 有关VirtualBox的介绍请参考:VirtualBox_百度百科 由于VirtualBox官网提供的搭建方法不够详细,而且本人在它指导下,从下载所需的开 ...
- [HTML5 Canvas学习]使用颜色和透明度
在canvas中使用颜色和透明度,通过context的strokeStyle和fillStyle属性设置,strokeStyle和fillStyle的值可以是任意有效的css颜色字串.可以用RGB.R ...
- python3可变与不可变数据类型
Python3中有六个标准的数据类型: Number(数字) String(字符串) List(列表) Dictionary(字典) Tuple(元组) Set(集合) 我理解的可变就是当一个变量创建 ...
- jquery实现DIV层拖动
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...
- HDU1465 第六周L题(错排组合数)
L - 计数,排列 Time Limit:1000MS Memory Limit:32768KB 64bit IO Format:%I64d & %I64u Descrip ...
- Renting Boats
Description 长江游艇俱乐部在长江上设置了n 个游艇出租站1,2,…,n.游客可在这些游艇出租站租用游艇,并在下游的任何一个游艇出租站归还游艇.游艇出租站i 到游艇出租站j 之间的租金为r( ...
- C语言学习笔记--枚举&结构体
枚举 枚举是一种用户定义的数据类型,它用关键字enum以如下语法格式来声明: enum 枚举类型名字 {名字0,名字1,...,名字n}: 枚举类型名字通常并不真的使用,要用的是大括号里面的名字,因为 ...
- 前端工程之模块化(来自百度FEX)
模块化 是一种处理复杂系统分解成为更好的可管理模块的方式,它可以把系统代码划分为一系列职责单一,高度解耦且可替换的模块,系统中某一部分的变化将如何影响其它部分就会变得显而易见,系统的可维护性更加简单易 ...