参考:http://www.cplusplus.com/reference/set/set/

一、set 是按特定顺序存储唯一元素的容器

实现是一种非常高效的平衡检索二叉树:红黑树(Red-Black Tree)。

二、set 的特性

1、set中的元素都是排好序的(与lower_bound()等结合使用能起到找前驱、后继的作用)

2、set集合中没有重复的元素(常常用于去重)

三、set 的成员函数

begin() 返回指向第一个元素的迭代器
end() 返回指向最后一个元素的迭代器
 // set::begin/end
#include <iostream>
#include <set> int main ()
{
int myints[] = {,,,,};
std::set<int> myset (myints,myints+); std::cout << "myset contains:";
for (std::set<int>::iterator it=myset.begin(); it!=myset.end(); ++it)
std::cout << ' ' << *it; std::cout << '\n'; return ;
}
empty () 如果集合为空,返回true
size () 集合中元素的数目
max_size() 返回集合能容纳的元素的最大限值
 // set::empty
#include <iostream>
#include <set> int main ()
{
std::set<int> myset; myset.insert();
myset.insert();
myset.insert(); std::cout << "myset contains:";
while (!myset.empty())
{
std::cout << ' ' << *myset.begin();
myset.erase(myset.begin());
}
std::cout << '\n'; return ;
} // set::size
#include <iostream>
#include <set> int main ()
{
std::set<int> myints;
std::cout << "0. size: " << myints.size() << '\n'; for (int i=; i<; ++i) myints.insert(i);
std::cout << "1. size: " << myints.size() << '\n'; myints.insert ();
std::cout << "2. size: " << myints.size() << '\n'; myints.erase();
std::cout << "3. size: " << myints.size() << '\n'; return ;
} // set::max_size
#include <iostream>
#include <set> int main ()
{
int i;
std::set<int> myset; if (myset.max_size()>)
{
for (i=; i<; i++) myset.insert(i);
std::cout << "The set contains 1000 elements.\n";
}
else std::cout << "The set could not hold 1000 elements.\n"; return ;
}
insert() 在集合中插入元素
erase() 删除集合中的元素
swap() 交换两个集合变量
clear() 清除所有元素
 // 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 ;
} // 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 ;
} // swap sets
#include <iostream>
#include <set> main ()
{
int myints[]={,,,,,};
std::set<int> first (myints,myints+); // 10,12,75
std::set<int> second (myints+,myints+); // 20,25,32 first.swap(second); std::cout << "first contains:";
for (std::set<int>::iterator it=first.begin(); it!=first.end(); ++it)
std::cout << ' ' << *it;
std::cout << '\n'; std::cout << "second contains:";
for (std::set<int>::iterator it=second.begin(); it!=second.end(); ++it)
std::cout << ' ' << *it;
std::cout << '\n'; return ;
} // set::clear
#include <iostream>
#include <set> int main ()
{
std::set<int> myset; myset.insert ();
myset.insert ();
myset.insert (); std::cout << "myset contains:";
for (std::set<int>::iterator it=myset.begin(); it!=myset.end(); ++it)
std::cout << ' ' << *it;
std::cout << '\n'; myset.clear();
myset.insert ();
myset.insert (); std::cout << "myset contains:";
for (std::set<int>::iterator it=myset.begin(); it!=myset.end(); ++it)
std::cout << ' ' << *it;
std::cout << '\n'; return ;
}
find() 返回一个指向被查找到元素的迭代器
count() 返回某个值元素的个数
lower_bound() 返回指向大于(或等于)某值的第一个元素的迭代器
upper_bound() 返回大于某个值元素的迭代器
 // 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 ;
} // set::count
#include <iostream>
#include <set> int main ()
{
std::set<int> myset; // set some initial values:
for (int i=; i<; ++i) myset.insert(i*); // set: 3 6 9 12 for (int i=; i<; ++i)
{
std::cout << i;
if (myset.count(i)!=)
std::cout << " is an element of myset.\n";
else
std::cout << " is not an element of myset.\n";
} return ;
} // set::lower_bound/upper_bound
#include <iostream>
#include <set> int main ()
{
std::set<int> myset;
std::set<int>::iterator itlow,itup; for (int i=; i<; i++) myset.insert(i*); // 10 20 30 40 50 60 70 80 90 itlow=myset.lower_bound (); // ^
itup=myset.upper_bound (); // ^ 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 ;
}

例题:http://poj.org/problem?id=3050

Hopscotch

Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 5441   Accepted: 3582

Description

The cows play the child's game of hopscotch in a non-traditional way. Instead of a linear set of numbered boxes into which to hop, the cows create a 5x5 rectilinear grid of digits parallel to the x and y axes.

They then adroitly hop onto any digit in the grid and hop forward, backward, right, or left (never diagonally) to another digit in the grid. They hop again (same rules) to a digit (potentially a digit already visited).

With a total of five intra-grid hops, their hops create a six-digit integer (which might have leading zeroes like 000201).

Determine the count of the number of distinct integers that can be created in this manner.

Input

* Lines 1..5: The grid, five integers per line

Output

* Line 1: The number of distinct integers that can be constructed

Sample Input

1 1 1 1 1
1 1 1 1 1
1 1 1 1 1
1 1 1 2 1
1 1 1 1 1

Sample Output

15

Hint

OUTPUT DETAILS: 
111111, 111112, 111121, 111211, 111212, 112111, 112121, 121111, 121112, 121211, 121212, 211111, 211121, 212111, and 212121 can be constructed. No other values are possible.

Source

题目大意:

有一个 5*5 矩阵,你可以在矩阵里朝上下左右行走五步,起点任意。问能走出多少种不同的序列。

大概思路:

DFS + set去重

AC code(492K 32MS):

 #include <cstdio>
#include <iostream>
#include <algorithm>
#include <set>
#define INF 0x3f3f3f3f
using namespace std; int mp[][];
set<int> S;
int ans; void dfs(int x, int y, int step, int sum)
{
if(step == )
{
sum = sum* + mp[x][y];
S.insert(sum);
//ans = S.size();
//printf("%d\n", ans);
return;
}
else
{
sum = sum* + mp[x][y];
if(x- >= ) dfs(x-, y, step+, sum);
if(x+ <= ) dfs(x+, y, step+, sum);
if(y- >= ) dfs(x, y-, step+, sum);
if(y+ <= ) dfs(x, y+, step+, sum);
}
return;
} int main()
{
for(int i = ; i <= ; i++)
for(int j = ; j <= ; j++)
{
scanf("%d", &mp[i][j]);
} for(int i = ; i <= ; i++)
for(int j = ; j <= ; j++)
{
dfs(i, j, , );
}
ans = S.size();
printf("%d\n", ans);
return ;
}

例题:https://www.lydsy.com/JudgeOnline/problem.php?id=1588

1588: [HNOI2002]营业额统计

Time Limit: 5 Sec  Memory Limit: 162 MB
Submit: 19175  Solved: 8093

Description

营业额统计 Tiger最近被公司升任为营业部经理,他上任后接受公司交给的第一项任务便是统计并分析公司成立以来的营业情况。 Tiger拿出了公司的账本,账本上记录了公司成立以来每天的营业额。分析营业情况是一项相当复杂的工作。由于节假日,大减价或者是其他情况的时候,营业额会出现一定的波动,当然一定的波动是能够接受的,但是在某些时候营业额突变得很高或是很低,这就证明公司此时的经营状况出现了问题。经济管理学上定义了一种最小波动值来衡量这种情况: 该天的最小波动值 当最小波动值越大时,就说明营业情况越不稳定。 而分析整个公司的从成立到现在营业情况是否稳定,只需要把每一天的最小波动值加起来就可以了。你的任务就是编写一个程序帮助Tiger来计算这一个值。 第一天的最小波动值为第一天的营业额。  输入输出要求

Input

第一行为正整数 ,表示该公司从成立一直到现在的天数,接下来的n行每行有一个整数(有可能有负数) ,表示第i
天公司的营业额。
天数n<=32767,
每天的营业额ai <= 1,000,000。
最后结果T<=2^31

Output

输出文件仅有一个正整数,即Sigma(每天最小的波动值) 。结果小于2^31 。

Sample Input

6
5
1
2
5
4
6

Sample Output

12

HINT

结果说明:5+|1-5|+|2-1|+|5-5|+|4-5|+|6-5|=5+4+1+0+1+1=12

题目大意:显而易见

大概思路:set + lower_bound() 求前驱或者后继

AC code:

 #include <bits/stdc++.h>
#define INF 0x3f3f3f3f
#define ll long long int
using namespace std; set<ll> T;
int N; int main()
{
scanf("%d", &N);
set<ll>::iterator it;
if(N == )
{
printf("0\n");
return ;
}
ll res = ;
ll p = ;
N--;
scanf("%lld", &p);
res+=p;
T.insert(p); while(N--)
{ ll t = INF;
scanf("%lld", &p);
it = T.lower_bound(p);
if(it != T.end())
{
t = min(t, abs((*it)-p));
}
if(it != T.begin())
{
t = min(t, abs((*--it)-p));
}
res+=t;
T.insert(p);
}
printf("%lld\n", res); return ;
}

STL - set【集合】的更多相关文章

  1. STL语法——集合:set 安迪的第一个字典(Andy's First Dictionary,UVa 10815)

    Description Andy, , has a dream - he wants to produce his very own dictionary. This is not an easy t ...

  2. C++ STL Set 集合

    前言 set是STL中的一种关联容器.集合具有无序性,互异性等特点.熟练使用STL中的set模板类,可以比较简单的解决一些编程问题. 关联容器:元素按照关键字来保存和访问,STL中的map,set就是 ...

  3. STL的集合set

    集合: 集合是由元素组成的一个类,其成员可以是一个集合,也可以是一个原子,通常一个元素在一个集合中不能多次出现:由于对实现集合不是很理解,只简单写下已有的STL中的set集合使用: C++中set基本 ...

  4. (转)C++ STL set() 集合

    set是STL中一种标准关联容器(vector,list,string,deque都是序列容器,而set,multiset,map,multimap是标准关联容器),它底层使用平衡的搜索树——红黑树实 ...

  5. 【STL】集合运算

    STL中有可以实现交集.并集.差集.对称差集的算法. 使用前需要包含头文件: #include <algorithm> 注:使用计算交集和并集的算法必须保证参与运算的两个集合有序!!! 交 ...

  6. STL&&用法集合

    .....STL是c++里很强势很好用的一系列容器(函数)之类的,之前一直不太会用,所以总是暴毙....想着快比赛了,是时候理一下这些东西了. -1.pair 存放两个基本元素的东西 定义方法: pa ...

  7. C++ STL set集合容器

    汇总了一些set的常用语句,部分参考了这篇:http://blog.163.com/jackie_howe/blog/static/199491347201231691525484/ #include ...

  8. stl的集合set——安迪的第一个字典(摘)

    set就是数学上的集合——每个元素最多只出现一次,和sort一样,自定义类型也可以构造set,但同样必须定义“小于”运算符 以下代码测试set中无重复元素 #include<iostream&g ...

  9. STL set集合用法总结(multiset)

    2017-08-20 15:21:31 writer:pprp set集合容器使用红黑树的平衡二叉树检索树,不会将重复键值插入,检索效率高 logn 检索使用中序遍历,所以可以将元素从小到大排列出来 ...

  10. 单词数 (STL set集合)

    单词数 Problem Description lily的好朋友xiaoou333近期非常空.他想了一件没有什么意义的事情.就是统计一篇文章里不同单词的总数.以下你的任务是帮助xiaoou333解决问 ...

随机推荐

  1. python-OS.path.join()路径拼接

    os.path.join()函数: 第一个以”/”开头的参数开始拼接,之前的参数全部丢弃. 以上一种情况为先.在上一种情况确保情况下,若出现”./”开头的参数,会从”./”开头的参数的上一个参数开始拼 ...

  2. win7与ubuntu双系统安装

    机器型号:联想V470 对系统引导一直不是很明白,导致我出现几次失败. 一直挺喜欢ubuntu的,因为,第一我感觉它比较友好,第二我初次接触linux就是ubuntu,当初还是同学帮助我wubi进行安 ...

  3. 关于重定向printf出错 Error[Pe020]: identifier "FILE" is undefined 解决方案

    IAR或者Keil用到重定向printf函数出现的错误解决方案 转发请注明出处,谢谢 原创:李剀 https://www.cnblogs.com/kevin-nancy/articles/105851 ...

  4. JAVA 利用反射自定义数据层框架

    之前的随笔一直都在介绍c#,主要公司最近的业务都是做桌面程序,那么目前c#中的WPF肯定是我做桌面程序的不二之选,做了半年的WPF,也基本摸清了c#写代码的套路和规则(本人之前是两年多的JAVA开发者 ...

  5. [转]微信小程序,开发大起底

    本文转自:http://blog.csdn.net/baiyuzhong2012/article/details/54378497 作者简介:张智超,北京微函工坊开发工程师,CSDN微信开发知识库特邀 ...

  6. Java Mail邮件发送的简单实现

    1.什么是java mail JAVA MAIL是利用现有的邮件账户发送邮件的工具,通过JAVA Mail的操控,让程序自动的使用设置的邮箱发送邮件. 这一机制被广泛的用在注册激活和垃圾邮件的发送等方 ...

  7. uiautomator 1使用简介

    1.生成build.xml android create uitest-project -n jar_name  -t id -p projectPah 2.修改build.xml 改成默认执行 bu ...

  8. Oracle JDBC 连接卡死后 Connection Reset

    坑 这绝对是我碰计算机以来遇到的第一大坑! 症状: 在Linux主机上远程登录,执行一个简单的Oracle的JDBC连接程序(jar包),结果硬生生的卡在了连接建立验证阶段,然后等上几分钟后因为连接超 ...

  9. springmvc 全局的异常拦截处理 @ControllerAdvice注解 @ExceptionHandler

    第一步: Dispatcher前端控制器的源码中 默认的 private boolean throwExceptionIfNoHandlerFound = false;说明如果没有找到匹配的执行器,不 ...

  10. H5分享到微信好友朋友圈QQ好友QQ空间微博二维码

    这是分享按钮: <button onclick="call()">通用分享</button> <button onclick="call(' ...