C++ STL set和multiset的使用
1,set的含义是集合,它是一个有序的容器,里面的元素都是排序好的,支持插入,删除,查找等操作,就  像一个集合一样。所有的操作的都是严格在logn时间之内完成,效率非常高。 set和multiset的区别是:set插入的元素不能相同,但是multiset可以相同。
创建 multiset<ss> base;
删除:如果删除元素a,那么在定义的比较关系下和a相等的所有元素都会被删除
base.count( a ):set能返回0或者1,multiset是有多少个返回多少个.
Set和multiset都是引用<set>头文件,复杂度都是logn
 
2,Set中的元素可以是任意类型的,但是由于需要排序,所以元素必须有一个序,即大小的比较关系,比如  整数可以用<比较.
3,自定义比较函数;
  include<set>
  typedef struct
  { 定义类型 }
  ss(类型名);
  struct cmp
  {
        bool operator()( const int &a, const int &b ) const
          { 定义比较关系<}
  };
  (运算符重载,重载<)
  set<ss> base; ( 创建一个元素类型是ss,名字是base的set )
  注:定义了<,==和>以及>=,<=就都确定了,STL的比较关系都是用<来确定的,所以必须通  过定义< --“严格弱小于”来确定比较关
 
4,set的基本操作:
begin()        返回指向第一个元素的迭代器
clear()        清除所有元素
count()        返回某个值元素的个数
empty()        如果集合为空,返回true
end()          返回指向最后一个元素的迭代器
equal_range()  返回集合中与给定值相等的上下限的两个迭代器
erase()        删除集合中的元素
find()          返回一个指向被查找到元素的迭代器
get_allocator() 返回集合的分配器
insert()        在集合中插入元素
lower_bound()  返回指向大于(或等于)某值的第一个元素的迭代器
key_comp()      返回一个用于元素间值比较的函数
max_size()      返回集合能容纳的元素的最大限值
rbegin()        返回指向集合中最后一个元素的反向迭代器
rend()          返回指向集合中第一个元素的反向迭代器
size()          集合中元素的数目
swap()          交换两个集合变量
upper_bound()  返回大于某个值元素的迭代器
value_comp()    返回一个用于比较元素间的值的函数
 
 
5,自定义比较函数:
For example:
#include<iostream>
#include<set>
using namespace std;
 
typedef struct {
int a,b;
char s;
}newtype;
 
struct compare  //there is no ().
{
bool operator()(const newtype &a, const newtype &b) const
{
return a.s<b.s;
}
};//the “; ”  is  here;
 
set<newtype,compare>element;
int main()
{
newtype a,b,c,d,t;
a.a=1; a.s='b';
b.a=2; b.s='c';
c.a=4; c.s='d';
d.a=3; d.s='a';
 
element.insert(a);
element.insert(b);
element.insert(c);
element.insert(d);
set<newtype,compare>::iterator it;
 
for(it=element.begin(); it!=element.end();it++)
cout<<(*it).a<<" ";
cout<<endl;
for(it=element.begin(); it!=element.end();it++)
cout<<(*it).s<<" ";
}
element自动排序是按照char s的大小排序的;
 
6.其他的set构造方法;
#include <iostream>
#include <set>
using namespace std;
bool fncomp (int lhs, int rhs) {return lhs<rhs;}
struct classcomp {
bool operator() (const int& lhs, const int& rhs) const
{return lhs<rhs;}
};
 
int main ()
{
set<int> first;                        // empty set of ints
 
int myints[]= {10,20,30,40,50};
set<int> second (myints,myints+5);      // pointers used as iterators
 
set<int> third (second);                // a copy of second
 
set<int> fourth (second.begin(), second.end());  // iterator ctor.
 
set<int,classcomp> fifth;              // class as Compare
 
bool(*fn_pt)(int,int) = fncomp;
set<int,bool(*)(int,int)> sixth (fn_pt);  // function pointer as Compare
 
return 0;
}

转自http://blog.sina.com.cn/daylive——C++ STL set&multiset的更多相关文章

  1. 转自http://blog.sina.com.cn/daylive——C++ STL map

    Map是c++的一个标准容器,她提供了很好一对一的关系,在一些程序中建立一个map可以起到事半功倍的效果,总结了一些map基本简单实用的操作! 1.map最基本的构造函数: map<string ...

  2. http://blog.sina.com.cn/s/blog_4c3b6a070100etad.html

    http://blog.sina.com.cn/s/blog_4c3b6a070100etad.html

  3. http://blog.sina.com.cn/s/blog_5bd6b4510101585x.html

    http://blog.sina.com.cn/s/blog_5bd6b4510101585x.html

  4. quartus ii13.0~16.0 调用uedit (转载http://blog.sina.com.cn/s/blog_6d5560f00102vax6.html)

    转自 http://blog.sina.com.cn/s/blog_6d5560f00102vax6.html Quartus II 中的文本编辑软件不好用,比较习惯与UE(Uedit32/ultra ...

  5. http://blog.sina.com.cn/s/blog_5f103c9c0101atny.html

    http://blog.sina.com.cn/s/blog_5f103c9c0101atny.html http://www.oschina.net/question/117304_51525

  6. http://blog.sina.com.cn/s/blog_6940cab30101hn9j.html

    http://blog.sina.com.cn/s/blog_6940cab30101hn9j.html

  7. http://blog.sina.com.cn/s/blog_705cc5dd01012ehb.html

    http://blog.sina.com.cn/s/blog_705cc5dd01012ehb.html

  8. 新浪博客地址 http://blog.sina.com.cn/u/2145079955

    原来 新浪博客地址 http://blog.sina.com.cn/u/2145079955

  9. http://blog.sina.com.cn/s/blog_5b9b4abe01017638.html

    http://blog.sina.com.cn/s/blog_5b9b4abe01017638.html

随机推荐

  1. [Javascript] Refactoring: Polymorphic Functions

    if-statements can add serious complexity and beg for refactoring. You can use polymorphic functions ...

  2. [转] 智能指针(三):unique_ptr使用简介

    PS: 1. auto_ptr太不安全,可能多个auto_ptr指向一个对象,出现重复释放的问题 2. unique_ptr解决了这个问题,不允许拷贝构造函数和赋值操作符,但是!它支持移动构造函数,通 ...

  3. .NET注册页面代码

    using System;using System.Collections.Generic;using System.Linq;using System.Web;using System.Web.UI ...

  4. linux系统用户锁定与解锁

    1.使用passwd命令锁定与解锁账号 [root@rhel7 ~]# passwd -l lxj --- -l 锁定 Locking password for user lxj. passwd: S ...

  5. Ubuntu 14.04 配置vsftpd实现FTP服务器 - 通过FTP连接AWS

    测试主机:亚马逊AWS EC2 系统:Ubuntu 14.04 想用AWS来做服务器玩,结果发现其不能像简单使用阿里云服务器那样用ftp连接,反正也不熟悉ftp服务器搭建,那就乘这个机会学习一下如何利 ...

  6. Error prompt:“xxx is not in the sudoers file”----Solution

    //Situation    System prompts "xxx is not in the sudoers file"(xxx equals the user name) w ...

  7. 《CSS网站布局实录》学习笔记(五)

    第五章 CSS内容排版 5.1 文字排版 5.1.1 通栏排版 进行网页通栏排版时,只要直接将段落文字放置于p或者其他对象中,再对段落文字应用间距.行距.字号等样式控制,便形成了排版雏形. 5.1.2 ...

  8. MyEclipse内存不足的问题

    今早打开MyEclipse莫名其妙弹出提示框,然后我各种搜索,用了网上能查到的各种办法去试图解决问题,方法包括但不限于  修改eclipse.ini .设置Default VM Arguments . ...

  9. Java中如何判断当前环境是大端字节顺序还是小端字节顺序

    Java非字节类型的基本类型,除了布尔型都是由组合在一起的几个字节组成的.这些数据类 型及其大小总结在表 2-1 中. 表:基本数据类型及其大小 数据类型 大小(以字节表示) Byte 1 Char ...

  10. php获取mac用于网站绑定服务器

    php获取mac用于网站绑定服务器 <?php class GetMacAddr{ var $return_array = array(); // 返回带有MAC地址的字串数组 var $mac ...