一、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的更多相关文章

  1. C++ STL set和multiset的使用

    C++ STL set和multiset的使用 std::set<int> s;那个s这个对象里面存贮的元素是从小到大排序的,(因为用std::less作为比较工具.) 1,set的含义是 ...

  2. STL Set和multiset 容器

    STL Set和multiset 容器 set/multiset的简介 set是一个集合容器,其中所包含的元素是唯一的,集合中的元素按一定的顺序排列. 元素插入过程是按排序规则插入,所以不能指定插入位 ...

  3. STL - set和multiset

    set/multiset的简介 set是一个集合容器,其中所包含的元素是唯一的,集合中的元素按一定的顺序排列.元素插入过程是按排序规则插入,所以不能指定插入位置. set采用红黑树变体的数据结构实现, ...

  4. STL之set&multiset使用简介

    关于set,必须说明的是set关联式容器.set作为一个容器也是用来存储同一数据类型的数据类型,并且能从一个数据集合中取出数据,在set中每个元素的值都唯一,而且系统能根据元素的值自动进行排序.应该注 ...

  5. c++ STL -- set和multiset

    set和multiset 1.结构 set和multiset会根据特定的排序原则将元素排序.两者不同之处在于,multisets允许元素重复,而set不允许重复. 只要是assignable.copy ...

  6. STL:set/multiset用法详解

    集合 使用set或multiset之前,必须加入头文件<set> Set.multiset都是集合类,差别在与set中不允许有重复元素,multiset中允许有重复元素. sets和mul ...

  7. STL使用————SET&MULTISET

    SET函数的基本用法 by hhl 使用set的好处 1. 当增加元素后,集合会自动删重并从小到大排列(时间比快排还快)2. 相当于一棵伸展树(能快速求出后继) 使用基础 #include<se ...

  8. C++ STL——set和multiset

    目录 一 set和multiset 二 对组pair 注:原创不易,转载请务必注明原作者和出处,感谢支持! 注:内容来自某培训课程,不一定完全正确! 一 set和multiset set和multis ...

  9. 转自http://blog.sina.com.cn/daylive——C++ STL set&multiset

    C++ STL set和multiset的使用 1,set的含义是集合,它是一个有序的容器,里面的元素都是排序好的,支持插入,删除,查找等操作,就  像一个集合一样.所有的操作的都是严格在logn时间 ...

随机推荐

  1. PHP MySQL 读取数据

    PHP MySQL 读取数据 从 MySQL 数据库读取数据 SELECT 语句用于从数据表中读取数据: SELECT column_name(s) FROM table_name 如需学习更多关于 ...

  2. hdu 3371 Connect the Cities (最小生成树Prim)

    题目连接:http://acm.hdu.edu.cn/showproblem.php?pid=3371 题目不难 稍微注意一下 要把已经建好的城市之间的花费定义为0,在用普通Prim算法就可以了:我没 ...

  3. phpstorm配置xdebug

    首先配置好xdebug 在php.ini里面加入以下配置(修改完注意重启apache或nginx): [xdebug] zend_extension="/usr/local/opt/php5 ...

  4. UI基础 - UIScrollView

    scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(, , , )]; scrollView.backgroundColor = [ ...

  5. C语言:Message类

    message.h #ifndef MESSAGE_H #define MESSAGE_H #define TRUE 1 #define FALSE 0 typedef struct { int se ...

  6. VM虚拟机上 实现CentOS 6.X下部署LVS(DR)+keepalived实现高性能高可用负载均衡

    一.简介 LVS是Linux Virtual Server的简写,意即Linux虚拟服务器,是一个虚拟的服务器集群系统.本项目在1998年5月由章文嵩博士成立,是中国国内最早出现的自由软件项目之一. ...

  7. C51指针的使用

    指针就是指变量或数据所在的存储区地址.如一个字符型的变量 STR 存放在内存单元DATA 区的 51H 这个地址中,那么 DATA 区的 51H 地址就是变量 STR 的指针.在 C 语言中指针是一个 ...

  8. Windows8.1 preview硬盘安装(图解)

    本人小本系统是win7 32位的,想要装win8 64位 ,想直接镜像安装不可能,因为位数不同.U盘只有2G ,显然也放不下3.6G的win8系统.借助NT6 HDD Installer,可以在本地硬 ...

  9. PLSQL死循环

    begin loop insert into for1 values('a'); commit; end loop; end;

  10. 【HDOJ】4985 Little Pony and Permutation

    水题. #include <cstdio> #define MAXN 100005 int buf[MAXN], n; int main() { int i, j, k; while (s ...