参考: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 ;
}

最新文章

  1. FolderSync PC 电脑 FTP 同步方法
  2. js做灯泡明灭特效
  3. ACM题目————Aggressive cows
  4. html5重定义标签
  5. K2工作流的使用
  6. 剑指offer系列45---和为s的两个数字
  7. 4.Knockout.Js(事件绑定)
  8. Python 安装 httpie
  9. IOS流媒体播放
  10. linux分区工具fdisk的使用
  11. 【百度地图API】建立全国银行位置查询系统(四)——如何利用百度地图的数据生成自己的标注
  12. redis beforesleep
  13. python实现邮件发送
  14. Vue框架创建项目常遇到问题
  15. C# Cookie方法
  16. Velocity ${} 和$!{}、!${}区别
  17. SkylineGlobe 如何二次开发获取三维模型的BBOX和设置Tint属性
  18. 20155326 实验三 敏捷开发与XP实践
  19. vue实用组件——页面公共头部
  20. [转]查看SQL Server被锁的表以及如何解锁

热门文章

  1. 解决dns服务器未找到问题 &amp;&amp;DNS解析服务器&amp;&amp;连接问题
  2. Python 进阶
  3. FZU 2207 ——以撒的结合——————【LCA + 记录祖先】
  4. 上下文(Context)和作用域(Scope)
  5. 通过js操作样式(评分)
  6. artDialog组件应用学习(一)
  7. [转]谷歌Chrome浏览器开发者工具教程—JS调试篇
  8. Java反射机制集中学习
  9. Spring @Resource, @Autowired and @Inject 注入
  10. 集合异常之List接口