一、C++結構體
#include <iostream>
using namespace std;
struct Point{
int x;
int y;
Point(int x=0,int y=0):x(x),y(y){}
};
Point operator +(const Point &A,const Point &B){
return Point(A.x+B.x,A.y+B.y);
}
ostream& operator <<(ostream &out,const Point A){
out<<'('<<A.x<<','<<A.y<<')'<<endl;
}
int main()
{
Point a(1,2);
Point b(2,3);
cout<<a+b<<endl;
}
注意
- 在C++中定義struct類型,可以直接用,可以不再用typedef取別名
- 對結構體編寫重構函數參考實例如上
- 結構體可以有成員函數,而且有它獨有的初始化方式,
- C++中的函數參數可以擁有默認值
- C++結構體可以有多個構造函數
- 以上,同樣在class中適用
二、模板
#include <iostream>
using namespace std;
struct Point{
int x;
int y;
Point(int x=0,int y=0):x(x),y(y){}
};
Point operator +(const Point &A,const Point &B){
return Point(A.x+B.x,A.y+B.y);
}
ostream& operator <<(ostream &out,const Point A){
out<<'('<<A.x<<','<<A.y<<')'<<endl;
}
// template sum
template<typename T>
T sum(T *p,T *q){
T result=0;//initiate 0*****
while(p!=q){
result=result+*p;
p++;
}
return result;
}
int main()
{
Point points[4]={Point(0,0),Point(1,1),Point(2,2),Point(3,3)};
cout<<sum(points,points+4)<<endl;
}
三、STL
3.1 排序與檢索
例題1
現有N個大理石,每個大理石上寫了一個非負整數、首先把各數從小到大排序;然后回答Q個問題。每個問題問是否有一個大理石寫着某個整數x,如果是,還要回答哪個大理石上寫着x。排序后的大理石從左到右編號為1~N。
(在樣例中,為了節約篇幅,所有大理石的數合並到一行,所有問題也合並到一行。)
樣例輸入:
4 1
2 3 5 1
5
5 2
1 3 3 3 1
2 3
樣例輸出:
CASE# 1:
5 found at 4
CASE# 2:
2 not found
3 found at 3
#define LOCAL
#include<cstdio>
#include<algorithm>
using namespace std;
const int MAX=100;
int main()
{
#ifdef LOCAL
freopen("data.in","r",stdin);
freopen("data.out","w",stdout);
#endif // LOCAL
int n,q;
int object;
int time=0;
int array[MAX];
while(scanf("%d%d",&n,&q)==2 && n){
printf("CASE# %d:\n",++time);
//input n numbers
int i=n;
while(i--)scanf("%d",&array[i]);
sort(array,array+n);//use sort c++ gives
while(q--){
scanf("%d",&object);
//find
int p=lower_bound(array,array+n,object)-array;//to find the index of the object in array
if(array[p] == object) printf("%d found at %d\n",object,p+1);
else printf("%d not found\n",object);
}
}
return 0;
}
以上注意:
- 重定向操作,在本地可以這么做,放到oj上要改變寫法,所以用ifdef的預定義方式。
- sort是一個模板函數,可以接收任何有<運算的類型
- 可以對普通數組array進行排序,也可以對vector進行排序,vector的排序方式為sort(v.begin(),v.end());
- lower_bound()函數接受三個參數,一二兩個是數組的起始位置,第三個是待查找的元素,表示在數組中尋找第一次大於或者等於待查元素的位置。
- scanf()的返回值:正確輸入的變量個數,注意正確包括類型正確,個數正確。
3.2 vector
3.3 set
例題
輸入一個文本,找出所有不同的單詞,按字典序從小到大進行輸出,單詞不區分大小寫。
樣例輸入:
Adventures in Disneyland
Two blondes were going to Disneyland when they came to a fork in the
road. the sign read:"Disneyland Left."
So they went home.
樣例輸出:
a
adventures
blondes
came
disneyland
fork
going
home
in
left
read
road
sign
so
the
they
to
two
went
were
when
#define LOCAL
#include<sstream>
#include<string>
#include<iostream>
#include<set>
using namespace std;
set<string> dict;
int main()
{
#ifdef LOCAL
freopen("data.in","r",stdin);
freopen("data.out","w",stdout);
#endif
string line,x;
while(cin>>line){
//1.to lower case
for(int i=0;i<line.length();i++){
if(isalpha(line[i])) line[i]=tolower(line[i]);
else line[i]=' ';
}
stringstream ss(line);
while(ss>>x) dict.insert(x);
}
for(set<string>::iterator it=dict.begin();it!=dict.end();++it){
cout<<*it<<'\n';
}
return 0;
}
代碼中用到了一些自帶的函數如isapha()、tolower()等,具體使用參見C++中常用系統函數
注意:
- 要對輸入樣例進行處理,非字母要變成空格,大寫字母變成小寫字母。
- 在將string進行插入到set的時候就已經按照字典序插入的了,因為string類型的<定義,就是定義為字典序的。
四、總結
vector、set和map的速度都很快,其中vector的速度接近數組,而set和map的速度也遠遠超過“用一個vector保存所有,然后逐個查找”的速度,set和map每次插入,查找和刪除時間和元素個數的對數呈線性關系。但有些對時間要求很高的題目中,STL可能成為性能瓶頸。