一道綜合set,map,stack的stl題目,非常有意思!!
#include#include#include
#include
#include
#include
#include
using
namespace
std;
intn,m;
typedef
set jihe;//
棧(sta)裡存的都是int,而int所代表的集合裡也是int
mapint>quanzhi;
vector
idinsetout;
stack
sta;//
棧(sta)裡存的都是int(表示集合),因為是集合套集合所以集合裡也是int(表示另一集合)
#define all(x) x.begin(),x.end()
#define ins(x) inserter(x,x.begin())
intsetinidout(jihe c)
intmain()
sta.push(setinidout(x));
}cout
}cout
<<"
***"
<}
}
因為第一次思路太亂了錯了好幾處
所以我重打一遍來捋清思路
(首先標頭檔案肯定不變)
題意是集合入棧,我們的思路是用權值代表集合,因此
stacks;
1.給不同的集合賦不同的權值,並且可以雙向匯出
但map只能匯出集合的權值,所以我們需要另乙個容器來匯出集合
由於該集合容器不定長,所以可以用vector
map,int>idout;vector
> lib;
2.集合套集合,集合本身是能充當元素的(核心)
權值代替集合所以集合的資料型別為int
而不同權值代表的集合可以通過上面的vector得到
typedef setset;//
這裡typedef乙個set可以簡化上面兩行**
mapint>map;
vector
lib;
1.已知集合將其放入map中得到權值,通過函式匯出&新建
intsear (set a)
2.已知權值直接lib[序號]
s.push(空集權值);
空集權值-->sear(空集);
s.push(sear(set())); //空集入棧
(略)出棧兩個集合:
set a=lib[s.top()];s.pop();
set b=lib[s.top()];
s.pop();
並集: set_union( )
交集: set_intersection( )
插入: 集合.insert( )
//這裡可以用巨集定義簡化
最後把集合x壓入棧
s.push(sear(x));
intmain()
cout
《棧頂集合大小<}
cout
<<"
***"
<}
}
UVA 12096 幾種容器的使用
題目 題意 有幾種操作 如下圖 然後給定一些操作,問最後棧頂的集合中有幾個元素 define ins x inserter x,x.begin 插入迭代器 using namespace std typedef setset mapidcache 把集合對映成id vectorsetcache 根據...
劉汝佳第五章 UVa 12096
include include include include include include includeusing namespace std define ins x inserter x,x.begin define all x x.begin x.end typedef setset m...
UVa 12096 集合棧計算機
這題的話,我們讀入操作之後,首先對於空集就是初始化為空。我們可以使用typedef 對於 set 重新命名為set,這樣就可以直接用set 的語法進行空集的初始化了。這題主要是對於集合的處理,我們可以給集合乙個對映函式,這個函式對於已知集合就返回該id,讓主函式進行相應的操作。對於未知集合就按序存入...