set用法

一、set和multiset基础

  set和multiset会根据特定的排序准则,自动将元素进行排序。不同的是后者允许元素重复而前者不允许。

需要包含头文件:

#include <set>

二、创建元素

set<int> s1;                    //创建空的set对象,元素类型为int,
set<const char*, strLess> s2( strLess); //创建空的set对象,元素类型char*,比较函数对象(即排序准则)为自定义strLess
set<int> s3(s1);               //利用set对象s1,拷贝生成set对象s2
int iArray[] = {, , };
set<int> s4(iArray, iArray + );      //用迭代区间[&first, &last)所指的元素,创建一个set对象
const char* szArray[] = {"hello", "dog", "bird" };
set<const char*, strLess> s5(szArray, szArray + , strLess() ); //用迭代区间[&first, &last)所指的元素,及比较函数对象strLess,创建一个set对象

三、插入元素

set<string> set1;    //empty set
set1.insert("the"); //set1 now has one element
set1.insert("and"); //set1 now has two elements
set<int> set2; //empty set
set2.insert(iset.begin(), iset.end()); //set2 now has 10 elements

例如:

// set::insert (C++98)
#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=; i<=; ++i) myset.insert(i*); // set: 10 20 30 40 50 ret = myset.insert(); // no new element inserted if (ret.second==false) it=ret.first; // "it" now points to element 20 myset.insert (it,); // max efficiency inserting
myset.insert (it,); // max efficiency inserting
myset.insert (it,); // no max efficiency inserting int myints[]= {,,}; // 10 already in set, not inserted
myset.insert (myints,myints+); std::cout << "myset contains:";
for (it=myset.begin(); it!=myset.end(); ++it)
std::cout << ' ' << *it;
std::cout << '\n'; return ;
}

结果:

myset contains:          

 四、删除元素

// erasing from set
#include <iostream>
#include <set> int main ()
{
std::set<int> myset;
std::set<int>::iterator it; // insert some values:
for (int i=; i<; i++) myset.insert(i*); // 10 20 30 40 50 60 70 80 90 it = myset.begin();
++it; // "it" points now to 20 myset.erase (it);
myset.erase (); it = myset.find ();
myset.erase (it, myset.end()); std::cout << "myset contains:";
for (it=myset.begin(); it!=myset.end(); ++it)
std::cout << ' ' << *it;
std::cout << '\n'; return ;
}

结果:

myset contains:   

五、查找元素

iset.find();     //returns iterator that refers to the element with key==1
iset.find(); //returns iterator == iset.end()
iset.count(); //returns 1;
iset.count(); //returns 0; //set_it refers to the element with key==1
set<int>::iterator set_it = iset.find();
*set_it=; //error: keys in a set are read-only
cout<<*set_it<<endl; //ok: can read the key

例如:

// set::find
#include <iostream>
#include <set> int main ()
{
std::set<int> myset;
std::set<int>::iterator it; // set some initial values:
for (int i=; i<=; i++) myset.insert(i*); // set: 10 20 30 40 50 it=myset.find();
myset.erase (it);
myset.erase (myset.find()); std::cout << "myset contains:";
for (it=myset.begin(); it!=myset.end(); ++it)
std::cout << ' ' << *it;
std::cout << '\n'; return ;
}

结果:

myset contains:   

 六、其他

    #include <iostream>
#include <set>
using namespace std; int main()
{
typedef set<int,greater<int> > IntSet;
IntSet s1; s1.insert();
s1.insert();
s1.insert();
s1.insert();
s1.insert();
s1.insert();
s1.insert();
//the inserted element that has the same value with a element existed is emitted copy(s1.begin(),s1.end(),ostream_iterator<int>(cout," "));
cout << endl << endl; pair<IntSet::iterator,bool> status = s1.insert();
if(status.second)
cout << "4 is inserted as element "
<< distance(s1.begin(),status.first) + << endl;
else
cout << "4 already exists in s1" << endl;
copy(s1.begin(),s1.end(),ostream_iterator<int>(cout," "));
cout << endl << endl; set<int> s2(s1.begin(),s1.end());//default sort criterion is less<
copy(s2.begin(),s2.end(),ostream_iterator<int>(cout," "));
cout << endl << endl;
}

上述程序最后新产生一个set:s2,默认排序准则是less。以s1的元素作为初值。

注意:s1和s2有不同的排序准则,所以他们的型别不同,不能直接进行相互赋值或比较。

运行结果:


 already exist in s1

STL --> set用法的更多相关文章

  1. STL vector用法介绍

    STL vector用法介绍 介绍 这篇文章的目的是为了介绍std::vector,如何恰当地使用它们的成员函数等操作.本文中还讨论了条件函数和函数指针在迭代算法中使用,如在remove_if()和f ...

  2. STL set 用法

      c++ stl集合set介绍 c++ stl集合(Set)是一种包含已排序对象的关联容器.set/multiset会根据待定的排序准则,自动将元素排序.两者不同在于前者不允许元素重复,而后者允许. ...

  3. STL map 用法

    首先make_pair Pairs C++标准程序库中凡是"必须返回两个值"的函数, 也都会利用pair对象  class pair可以将两个值视为一个单元.容器类别map和mul ...

  4. 记一些stl的用法(持续更新)

    有些stl不常用真的会忘qwq,不如在这里记下来,以后常来看看 C++中substr函数的用法 #include<string> #include<iostream> usin ...

  5. 日常笔记6C++标准模板库(STL)用法介绍实例

    一.vector常见用法详解 vector翻译为向量,但是这里翻译成变长数组的叫法更好理解. 如果typename是一个STL容器,定义的时候要记得在>>符号之间加上空格,因为在C++11 ...

  6. c++ STL map 用法

    map是STL的一个关联容器,它提供一对一(其中第一个可以称为关键字,每个关键字只能在map中出现一次,第二个可能称为该关键字的值)的数据 处理能力,由于这个特性,它完成有可能在我们处理一对一数据的时 ...

  7. codevs 1285 二叉查找树STL基本用法

    C++STL库的set就是一个二叉查找树,并且支持结构体. 在写结构体式的二叉查找树时,需要在结构体里面定义操作符 < ,因为需要比较. set经常会用到迭代器,这里说明一下迭代器:可以类似的把 ...

  8. STL vector 用法介绍

    介绍 这篇文章的目的是为了介绍std::vector,如何恰当地使用它们的成员函数等操作.本文中还讨论了条件函数和函数指针在迭代算法中使用,如在remove_if()和for_each()中的使用.通 ...

  9. STL --> list用法

    List介绍 Lists将元素按顺序储存在链表中.与 向量(vectors)相比, 它允许快速的插入和删除,但是随机访问却比较慢. assign() // 给list赋值 back() // 返回最后 ...

随机推荐

  1. 机器学习中应用到的各种距离介绍(附上Matlab代码)

    转载于博客:各种距离 在做分类时常常需要估算不同样本之间的相似性度量(SimilarityMeasurement),这时通常采用的方法就是计算样本间的"距离"(Distance). ...

  2. 【javascript】jquery jsonp跨越请求

    <html> <head> <meta charset="utf-8"> <title></title> <scr ...

  3. Caused by: java.lang.NoClassDefFoundError: org/hibernate/cfg/Configuration

    1.错误描述 usage: java org.apache.catalina.startup.Catalina [ -config {pathname} ] [ -nonaming ] { -help ...

  4. Ubuntu14.04下安装Flash Player

    Ubuntu14.04下安装Flash Player youhaidong@youhaidong:~$ sudo apt-get install flashplugin-nonfree [sudo] ...

  5. LeetCode 456. 132 Pattern

    问题描述 给一组数,判断这组数中是否含有132 pattern. 132 pattern: i < j < k, 且 ai < ak < aj 第一种解法 使用栈来保存候选的子 ...

  6. spring的PathVariable和value={}小技巧(shiro项目中来的三)

    <property name="successUrl" value="/main/index" /> @RequestMapping(value=& ...

  7. 消息队列mq的原理及实现方法

    消息队列技术是分布式应用间交换信息的一种技术.消息队列可驻留在内存或磁盘上,队列存储消息直到它们被应用程序读走.通过消息队列,应用程序可独立地执行--它们不需要知道彼此的位置.或在继续执行前不需要等待 ...

  8. 将nodejs代码部署到阿里云服务器

    概述 最近在做一个小项目,其中用nodejs做了个数据转发的接口,之后需要将这部分代码部署到服务器上面,并使用Nginx做反向代理.期间使用搜索引擎大量查阅了其他同鞋的经验,不过写的大多很笼统,因此踩 ...

  9. 关于access_token过期的解决办法

    最近在做微信的发送模版消息,在测试的时候发现有的时候能够发送,有时候无法发送,查了相关的日志(日志记录发送结果很重要!!),看到了微信返回的错误消息,发现是 invalid credential, a ...

  10. 【UOJ207】共价大爷游长沙(Link-Cut Tree,随机化)

    [UOJ207]共价大爷游长沙(Link-Cut Tree,随机化) 题面 UOJ 题解 这题太神了 \(\%\%\%myy\) 看到动态的维护边很容易的想到了\(LCT\) 然后能否堵住一条路 我们 ...