参考: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. sourceTree免登陆

    https://www.cnblogs.com/dereckbu/articles/7659674.html

  2. Struts2中Action对象的set方法和get方法调用规则

    Struts的Action是采用的是多实例多线程设计,而不是像Servlet那样采用单实例多线程设计,因此在struts中,一个请求就对应一个Action对象,个对象之间的数据相互之间互不干扰.没接到 ...

  3. FZU 1924——死锁——————【topo判环】

    死锁 Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u Submit Status Pr ...

  4. CSS 专业的技巧

    目录 专业的技巧 支持情况 贡献准则   专业的技巧 使用CSS复位 继承 box-sizing 使用 :not() 选择器来决定表单是否显示边框 为 body 元素添加行高 垂直居中任何元素 逗号分 ...

  5. 项目搭建系列之二:SpringMVC框架下配置MyBatis

    1.什么是MyBatis? MyBatis是一个支持普通SQL查询,存储过程和高级映射的优秀持久层框架.MyBatis消除了几乎所有的JDBC代码和参数的手工设置以及对结果集的检索封装.MyBatis ...

  6. Zookeeper之集群搭建(Linux)

    Zookeeper集群搭建(Linux环境) 条件准备:准备三台Linux服务器 vt-serv1.vt-serv2.vt-serv3(虚拟机/物理机均可,服务器数量一定要是单数,不要问我为什么,据说 ...

  7. 编程提取字符串"Java is a programming language"中的各个单词,并打印输出。

    import java.lang.String; import java.util.StringTokenizer; public class StringGetWord{ /* 编程提取字符串&qu ...

  8. poj 1655 树的重心 && define注意事项

    http://blog.csdn.net/acdreamers/article/details/16905653 题意:给定一棵树,求树的重心的编号以及重心删除后得到的最大子树的节点个数size,如果 ...

  9. Spring课程 Spring入门篇 4-6 Spring bean装配之基于java的容器注解说明--@ImportResource和@Value java与properties文件交互

    1 解析 1.1 这两个注解应用在什么地方 1.2 应用方式 1.3 xml方式实现取值 2 代码演练 2.1 @ImportResource和@Value代码演练 1 解析 1.1 这两个注解应用在 ...

  10. vue的一些特殊特性

    一.使用$ref特性获取DOM元素 代码示例如下所示: <!DOCTYPE html> <html lang="en"> <head> < ...