set/multiset用法详解
集合
Set、multiset都是集合类,差别在与set中不允许有重复元素,multiset中允许有重复元素。
sets和multiset内部以平衡二叉树实现
1. 常用函数
1) 构造函数和析构函数
set c:创建空集合,不包含任何元素
set c(op):以op为排序准则,产生一个空的set
set c1(c2):复制c2中的元素到c1中
set c(const value_type *first, const value_type* last):复制[first, last)之间元素构成新集合
set c(const value_type *first, const value_type* last,op):以op为排序准则,复制[first, last)之间元素构成新集合。
c.~set()销毁所有元素,释放内存
multiset mc:创建空集合,不包含任何元素
multiset mc(op):以op为排序准则,产生一个空的set
multiset c1(c2):复制c2中的元素到c1中
multiset c(const value_type *first, const value_type* last):复制[first, last)之间元素构成新集合
multiset c(const value_type *first, const value_type* last,op):以op为排序准则,复制[first, last)之间元素构成新集合。
c.~set()销毁所有元素,释放内存
- // constructing sets
- #include <iostream>
- #include <set>
- 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 ()
- {
- std::set<int> first; // empty set of ints
- int myints[]= {10,20,30,40,50};
- std::set<int> second (myints,myints+5); // range
- std::set<int> third (second); // a copy of second
- std::set<int> fourth (second.begin(), second.end()); // iterator ctor.
- std::set<int,classcomp> fifth; // class as Compare
- bool(*fn_pt)(int,int) = fncomp;
- std::set<int,bool(*)(int,int)> sixth (fn_pt); // function pointer as Compare
- return 0;
- }
2) 大小、判断空函数
int size() const:返回容器元素个数
bool empty() const:判断容器是否为空,若返回true,表明容器已空
3) 增加、删除函数
pair<iterator,bool> insert( x):插入元素x
iterator insert(iterator it,x):在迭代器it处插入元素x
void insert(const value_type *first,const value_type *last):插入[first, last)之间元素
iterator erase(iterator it):删除迭代器指针it处元素
iterator erase(iterator first,iterator last):删除[first, last)之间元素
size_type erase(const Key& key):删除元素值等于key的元素
- #include <iostream>
- #include <set>
- int main ()
- {
- std::set<int> myset;
- std::set<int>::iterator it;
- std::pair<std::set<int>::iterator,bool> ret;
- // set some initial values:
- for (int i=1; i<=5; ++i) myset.insert(i*10); // set: 10 20 30 40 50
- ret = myset.insert(20); // no new element inserted
- if (ret.second==false) it=ret.first; // "it" now points to element 20
- myset.insert (it,25); // max efficiency inserting
- myset.insert (it,24); // max efficiency inserting
- myset.insert (it,26); // no max efficiency inserting
- int myints[]= {5,10,15}; // 10 already in set, not inserted
- myset.insert (myints,myints+3);
- std::cout << "myset contains:";
- for (it=myset.begin(); it!=myset.end(); ++it)
- std::cout << ' ' << *it;
- std::cout << '\n';
- return 0;
- }
- #include <iostream>
- #include <set>
- int main ()
- {
- std::set<int> myset;
- std::set<int>::iterator it;
- // insert some values:
- for (int i=1; i<10; i++) myset.insert(i*10); // 10 20 30 40 50 60 70 80 90
- it = myset.begin();
- ++it; // "it" points now to 20
- myset.erase (it);
- myset.erase (40);
- it = myset.find (60);
- myset.erase (it, myset.end());
- std::cout << "myset contains:";
- for (it=myset.begin(); it!=myset.end(); ++it)
- std::cout << ' ' << *it;
- std::cout << '\n';
- return 0;
- }
4) 遍历函数
iterator begin():返回首元素的迭代器指针
iterator end():返回尾元素的迭代器指针
reverse_iterator rbegin():返回尾元素的逆向迭代器指针
reverse_iterator rend():返回首元素前一个位置的迭代器指针
- #include <iostream>
- #include <set>
- int main ()
- {
- int myints[] = {75,23,65,42,13};
- std::set<int> myset (myints,myints+5);
- std::cout << "myset contains:";
- for (std::set<int>::iterator it=myset.begin(); it!=myset.end(); ++it)
- std::cout << ' ' << *it;
- std::cout << '\n';
- return 0;
- }
5) 操作函数
const_iterator lower_bound(const Key& key):返回容器中大于等于key的迭代器指针
const_iterator upper_bound(const Key& key):返回容器中大于key的迭代器指针
int count(const Key& key) const:返回容器中元素等于key的元素的个数
pair<const_iterator,const_iterator> equal_range(const Key& key) const:返回容器中元素值等于key的迭代指针[first, last)
const_iterator find(const Key& key) const:查找功能,返回元素值等于key的迭代器指针
void swap(set& s):交换集合元素
void swap(multiset& s):交换多集合元素
- #include <iostream>
- #include <set>
- int main ()
- {
- std::set<int> myset;
- std::set<int>::iterator itlow,itup;
- for (int i=1; i<10; i++) myset.insert(i*10); // 10 20 30 40 50 60 70 80 90
- itlow=myset.lower_bound (30); // ^
- itup=myset.upper_bound (60); // ^
- myset.erase(itlow,itup); // 10 20 70 80 90
- std::cout << "myset contains:";
- for (std::set<int>::iterator it=myset.begin(); it!=myset.end(); ++it)
- std::cout << ' ' << *it;
- std::cout << '\n';
- return 0;
- }
- #include "stdafx.h"
- #include <iostream>
- #include <set>
- using namespace std;
- int main ()
- {
- set<int> myset;
- for (int i=1; i<=5; i++) myset.insert(i*10); // myset: 10 20 30 40 50
- pair<set<int>::const_iterator,set<int>::const_iterator> ret;
- ret = myset.equal_range(30);
- cout << "the lower bound points to: " << *ret.first << '\n';
- cout << "the upper bound points to: " << *ret.second << '\n';
- return 0;
- }
- #include "stdafx.h"
- #include <iostream>
- #include <set>
- using namespace std;
- int main ()
- {
- int myints[]={12,75,10,32,20,25};
- set<int> first (myints,myints+3); // 10,12,75
- set<int> second (myints+3,myints+6); // 20,25,32
- first.swap(second);
- cout << "first contains:";
- for (set<int>::iterator it=first.begin(); it!=first.end(); ++it)
- cout << ' ' << *it;
- cout << '\n';
- cout << "second contains:";
- for (set<int>::iterator it=second.begin(); it!=second.end(); ++it)
- cout << ' ' << *it;
- cout << '\n';
- return 0;
- }
set/multiset用法详解的更多相关文章
- [STL]set/multiset用法详解[自从VS2010开始,set的iterator类型自动就是const的引用类型]
集合 使用set或multiset之前,必须加入头文件<set> Set.multiset都是集合类,差别在与set中不允许有重复元素,multiset中允许有重复元素. sets和mul ...
- STL:set/multiset用法详解
集合 使用set或multiset之前,必须加入头文件<set> Set.multiset都是集合类,差别在与set中不允许有重复元素,multiset中允许有重复元素. sets和mul ...
- STL之五:set/multiset用法详解
集合 转载于:http://blog.csdn.net/longshengguoji/article/details/8546286 使用set或multiset之前,必须加入头文件<set&g ...
- 7-set用法详解
C++中set用法详解 转载 http://blog.csdn.net/yas12345678/article/details/52601454 C++ / set 更详细见:http://www.c ...
- C#中string.format用法详解
C#中string.format用法详解 本文实例总结了C#中string.format用法.分享给大家供大家参考.具体分析如下: String.Format 方法的几种定义: String.Form ...
- @RequestMapping 用法详解之地址映射
@RequestMapping 用法详解之地址映射 引言: 前段时间项目中用到了RESTful模式来开发程序,但是当用POST.PUT模式提交数据时,发现服务器端接受不到提交的数据(服务器端参数绑定没 ...
- linux管道命令grep命令参数及用法详解---附使用案例|grep
功能说明:查找文件里符合条件的字符串. 语 法:grep [-abcEFGhHilLnqrsvVwxy][-A<显示列数>][-B<显示列数>][-C<显示列数>] ...
- mysql中event的用法详解
一.基本概念mysql5.1版本开始引进event概念.event既“时间触发器”,与triggers的事件触发不同,event类似与linux crontab计划任务,用于时间触发.通过单独或调用存 ...
- CSS中伪类及伪元素用法详解
CSS中伪类及伪元素用法详解 伪类的分类及作用: 注:该表引自W3School教程 伪元素的分类及作用: 接下来让博主通过一些生动的实例(之前的作业或小作品)来说明几种常用伪类的用法和效果,其他的 ...
随机推荐
- AngularJs调用NET MVC 控制器中的函数进行后台操作
题目中提到的控制器指的是.NET MVC的控制器,不是angularjs的控制器. 首先看主页面的代码: <!DOCTYPE html> <html> <head> ...
- 把sed当作命令解释器使用
[root@sishen ~]# vim script.sed #!/bin/sed -f #交换第一列和第二列 s/\([^,]*\),\([^,]*\),\(.*\).*/\2,\1, \3/g ...
- DLL线程中坑爹的Synchronize?
1, 缘起 某次开发语音对讲windows程序,采用delphi语言,及delphix的TDXSound控件. DXSound提供了TSoundCaptureStream类,可以实现指定频率.位数.声 ...
- 关于mapState和mapMutations和mapGetters 和mapActions辅助函数的用法及作用(二)-----mapMutations
在组件中提交Mutations: import { mapState, mapMutations } from 'vuex' export default { data() { return { ms ...
- java web 学习笔记 - servlet03
1.Servlet可以分为三种类型 普通Servlet,需要在基本程序架构中体现. Servlet过滤器,在web容器启动时初始化,不需要手动调用. Servlet 监听器. 2. Servlet过 ...
- Swift 关键字 inout - 让值类型以引用方式传递
两种参数传递方式 值类型 传递的是参数的一个副本,这样在调用参数的过程中不会影响原始数据. 引用类型 把参数本身引用(内存地址)传递过去,在调用的过程会影响原始数据. 在 Swift 众多数据类型中, ...
- MIPS的寄存器、指令和寻址方式的分类
MIPS的32个寄存器 助记符 编号 作用 zero 0 恒为0 at 1 (assembly temporary)保留给汇编器使用 v0,v1 2-3 (values)子程序返回,即函数调用时的返回 ...
- ALTER TRIGGER - 修改一个触发器的定义
SYNOPSIS ALTER TRIGGER name ON table RENAME TO newname DESCRIPTION 描述 ALTER TRIGGER 改变一个现有触发器的属性. RE ...
- 【计算机网络】2.2 Web和HTTP
第二章第二节 Web和HTTP 这一章中,我们需要讨论5种重要的应用:Web.文件传输.电子邮件.目录服务.P2P:这一节中,我们将讨论Web和它的应用层协议HTTP. Outline Web简介 H ...
- php基础查找算法
1.顺序查找 function line_search($array,$tar) { if(!is_array($array) || count($array) < 1) return fals ...