STL之五:set/multiset用法详解

集合

转载于:http://blog.csdn.net/longshengguoji/article/details/8546286

使用set或multiset之前,必须加入头文件<set>

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;
}
时间: 2024-08-29 01:32:22

STL之五:set/multiset用法详解的相关文章

STL list链表的用法详解(转)

本文以List容器为例子,介绍了STL的基本内容,从容器到迭代器,再到普通函数,而且例子丰富,通俗易懂.不失为STL的入门文章,新手不容错过! 0 前言 1 定义一个list 2 使用list的成员函数push_back和push_front插入一个元素到list中 3 list的成员函数empty() 4 用for循环来处理list中的元素 5 用STL的通用算法for_each来处理list中的元素 6 用STL的通用算法count_if()来统计list中的元素个数 7 使用count_i

STL中的map用法详解

STL中map用法详解 说明:如果你具备一定的C++ template知识,即使你没有接触过STL,这个文章你也应该可能较轻易的看懂.本人水平有限,不当之处,望大家辅正. 一.map概述 map是STL的一个关联容器,它提供一对一(其中第一个可以称为关键字,每个关键字只能在map中出现一次,第二个可能称为该关键字的值)的数据处理能力,由于这个特性,它完成有可能在我们处理一对一数据的时候,在编程上提供快速通道.这里说下map内部数据的组织,map内部自建一颗红黑树(一种非严格意义上的平衡二叉树),

STL之一:字符串用法详解

转载于:http://blog.csdn.net/longshengguoji/article/details/8539471 字符串是程序设计中最复杂的变成内容之一.STL string类提供了强大的功能,使得许多繁琐的编程内容用简单的语句就可完成.string字符串类减少了C语言编程中三种最常见且最具破坏性的错误:超越数组边界:通过违背初始化或被赋以错误值的指针来访问数组元素:以及在释放了某一数组原先所分配的存储单元后仍保留了“悬挂”指针. string 函数列表 函数名 描述 begin

C++ STL replace()函数常用用法详解

replace算法: replace函数包含于头文件#include<string>中. 泛型算法replace把队列中与给定值相等的所有值替换为另一个值,整个队列都被扫描,即此算法的各个版本都在 线性时间内执行---其复杂度为O(n). 即replace的执行要遍历由区间[frist,last)限定的整个队列,以把old_value替换成new_value. 下面介绍replace()函数常用的九中方法: 用法一: 用str替换指定字符串从起始位置pos开始长度为len的字符 示例代码: #

STL之三:deque用法详解

转载于:http://blog.csdn.net/longshengguoji/article/details/8519812 deque函数: deque容器为一个给定类型的元素进行线性处理,像向量一样,它能够快速地随机访问任一个元素,并且能够高效地插入和删除容器的尾部元素.但它又与vector不同,deque支持高效插入和删除容器的头部元素,因此也叫做双端队列.deque类常用的函数如下. (1)    构造函数 deque():创建一个空deque deque(int nSize):创建一

C++ string 用法详解

/////////////////////////////////////////////////////////////////////////////////// 任何人对本文进行引用都要标明作者是Nicolai M.Josuttis /////////////////////////////////////////////////////////////////////////////////// C++ 语言是个十分优秀的语言,但优秀并不表示完美.还是有许多人不愿意使用C或者C++,为什

c++中vector的用法详解

c++中vector的用法详解 vector(向量): C++中的一种数据结构,确切的说是一个类.它相当于一个动态的数组,当程序员无法知道自己需要的数组的规模多大时,用其来解决问题可以达到最大节约空间的目的. 用法: 1.文件包含: 首先在程序开头处加上#include<vector>以包含所需要的类文件vector 还有一定要加上using namespace std; 2.变量声明: 2.1 例:声明一个int向量以替代一维的数组:vector <int> a;(等于声明了一个

js的offsetParent属性用法详解

js的offsetParent属性用法详解:此属性是javascript中较为常用的属性,对于它的良好掌握也是非常有必要的,下面就通过代码实例介绍一下它的用法,希望能够给需要的朋友带来一定的帮助.一.基本介绍:此属性可以返回距离指定元素最近的采用定位(position属性值为fixed.relative或者absolute)父级元素,如果父级元素中没有采用定位的元素,则返回body对象的引用.语法结构: obj.offsetParent 二.代码实例: <!DOCTYPE html> <

python处理word文件:win32com用法详解

目标:用python处理doc文件 方法:引入win32com模块 ************************************************************************** 一.安装 ************************************************************************** 首先要先下载安装win32com模块(起先在linux下装不成功,后在windows下面成功了...) 下载地址:http