筆記 第五章(上)劉汝佳 演算法競賽入門經典

2021-09-12 00:11:58 字數 2207 閱讀 8633

void

swap2

(int

&a,int

&b)//&表示傳引用

intmain()

//例 知識點解說詳見 p106

struct point

//建構函式,特點:沒有返回值;引數中的0是預設值,沒有指明引數時相當於呼叫 point(0,0) 來構造;

//x(x)是簡化寫法,等同於 this->x = x};

point a,b(

1,2)

;

#include

#include

#include

#include

set dict;

//定義string集合

string s,buf;

for(

int i=

0;ilength()

;i++);

isalpha

(s[i]))

//判斷是否是字母,大小寫都行

s[i]

=tolower

(s[i]);

//轉化成小寫;相對應函式:toupper(p50 提示3-20)

stringstream ss

(s);

//能像使用cin一樣使用ss

while

(ss>>buf)

dict.

insert

(buf)

;//向集合中插入元素

for(set

::iterator it=dict.

begin()

;it!=dict.

end();

++it)

//迭代器iterator的使用

cout<<

*it month_name;

month_name[

"july"]=

7;//過載了運算子,用起來像陣列

#include

#include

#include

#include

#include

mapint> cnt;

string ans;

sort

(ans.

begin()

,ans.

end())

;//string也有begin()和end();也能用sortif(

!cnt.

count

(r))

//set和map都有count操作

cnt[r]=0

;中的int型也要初始化;2.map用起來有點像陣列

#include

stack<

int> s;

s.push

(s.top()

);//push元素入棧;pop元素出棧;top檢視棧頂元素;

s.pop()

;

typedef set<

int> set;

mapint> idcache;

// 把集合對映成id

vector setcache;

// 根據id取集

setcache.

push_back

(x);

// 新增新集合

return idcache[x]

= setcache.

size()

-1;//集合的id就是在向量中的秩

#define all(x) x.begin(),x.end()

#define ins(x) inserter(x,x.begin())

if(op[0]

=='u'

) set_union (

all(x1)

,all

(x2)

,ins

(x))

;//取並集

if(op[0]

=='i'

) set_intersection (

all(x1)

,all

(x2)

,ins

(x))

;//取交集

if(op[0]

=='a'

)

劉汝佳第五章 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 230

這道題對於不熟悉基本操作 尤其是string 的我來說,實在是太難了。先總結一下基本操作 結構體內重構操作符 bool operater 可以讓結構體可以比較大小。string.find a,num 是從string的第num開始找a,並且返回a的位置,如果找不到,則返回string nops st...

劉汝佳第五章 UVA 10391

這個題,學習了很多關於迭代器和string的知識。對於集合set 建立迭代器用格式 set iterator it 目前不是很清楚雙冒號什麼用。set.find 函式,如果找到,返回此處的迭代器,如果找不到,返回set.end set.insert x 在集合中插入x 對於string 流式輸入ci...