檔案讀寫:

1.重新導向
#include<iostream>
#include<cstdio>
using namespace std;
int main()
{
freopen("pa.in","r",stdin);
freopen("pa.out","w",stdout);
return 0;
}

適用實機:
將標準輸入從螢幕&鍵盤改到檔案

2.fstream
#include<iostream>
#include<fstream>
using namespace std;
int main()
{
int q;
ofstream fout;
fout.open("test.txt");
fout<<123<<endl;
fout.close();

ifstream fin;
fin.open("test.txt");
fin>>q;
cout<<q<<endl;
fin.close();

}
適用實機:
需要同時針對鍵盤與螢幕以及檔案做輸入書出的動作

Sort:

Case1: 不需要自訂compare函數

#include<iostream>
#include<algorithm>
using namespace std;

int main()
{
int q[]={2,1,4,3,5};
sort(q,q+5);
}

Case2: 需要自訂compare函數

#include<iostream>
#include<algorithm>
using namespace std;
struct PG
{
PG(){};
PG(int a,string b) : num(a),id(b){}
int num;
string id;
};
bool cp(PG a,PG b)
{
if(a.num == b.num)
return a.id < b.id;
else return a.num > b.num;
//若num一樣 則比較id的字典順序 字典順序小的在前面
//否則比較num大小 大的在前面
}

PG miko[5];
//以下測試用data--------------------------------------
miko[1]=PG(5,"Sona");
miko[2]=PG(4,"Janna");
miko[3]=PG(5,"Morgana");
miko[4]=PG(3,"Tristina");
//-------------------------------------------------
int main()
{
sort(miko+1,miko+5,cp);
for(int i=1;i<=4;i++)cout<<miko[i].num<<" "<<miko[i].id<<endl;
}

Queue:
#include<iostream>
#include<queue>
using namespace std;
int main()
{
int t;
queue<int> PG; //宣告
PG.push(123);
PG.push(456);
cout << PG.front() << endl; // queue前端元素
PG.pop(); //移除queue前端元素
}

Stack:
#include<iostream>
#include<stack>
using namespace std;
int main()
{
int t;
stack<int> PG; //宣告
PG.push(123);
PG.push(456);
cout << PG.top() << endl; // stack前端元素
PG.pop(); //移除stack前端元素
}

容器通用操作

cout << PG.empty() << endl;
//若容器為空 則回傳true

cout << PG.size() << endl;
//回傳容器的大小

PG.clear();
//清空容器內的元素

Map:
#include<iostream>
#include<map>
using namespace std;
int main()
{
map<string,int> PG;
map<string,int>::iterator iter; //指向元素的指標

PG["Eruru"] = 1;
PG["Fate.T.H"] = 2;
PG["Shamaru"] = 3;
//以上新增三筆資料

cout << PG["Fate.T.H"] << endl;
//取得資料的便捷方法
//但是此方法若該筆資料不存在,會自動新增
//以本例子為例,若"Fate"不存在
//則map會插入"Fate"這筆資料

iter = PG.find("Fate.T.H");
cout << iter->first << " " << iter->second << endl;
//取得資料的傳統方法

if(PG.find("Eruru")==PG.end())cout<<"Not exist";
//判斷某筆資料是否存在

for(iter=PG.begin(); iter!=PG.end(); ++iter)
{
cout << iter->first << " " << iter->second << endl;
}
//把整個map資料印出

system("pause");

}

Set:

#include<iostream>
#include<set>
using namespace std;
int main()
{
set<int> PG;
set<int>::iterator iter; // 指向元素的指標

PG.insert(123);
PG.insert(456);
//插入資料

if(PG.find(456)==PG.end())cout<<"Not found";
//檢索元素

PG.erase(PG.find(456));
//移除元素

}

Priority Queue:
#include<iostream>
#include<queue>
using namespace std;
struct cp
{

bool operator()(int &a,int &b)
{
return a < b ;
//在此自行定義compare函數
}
};
int main()
{
priority_queue<int,vector<int>,cp> PG;
PG.push(2);
PG.push(5);
PG.push(3);
PG.push(1);
PG.push(4);

while(!PG.empty())
{
cout << PG.top() << endl;
PG.pop();
}

}

STL quick reference

by PG @ TFcis
http://www.eruru.tw

This work is licensed under a Creative Commons Attribution-NonCommercial-ShareAlike 3.0 Unported License.

String:
#include<string>
#include<iostream>
using namespace std;
int main()
{
string q="Sona Janna Morgana Tristina";
cout << q.find("Janna") << endl;
cout << (q.find("Katarina")==string::npos) << endl;
//find若有找到 則回傳index 若無
// 則回傳string::npos (一個整數)
cout << q.erase(5,4) << endl;
//自index5開始刪除4個字元
cout << q.insert(5,"Jann") << endl;
//自index5插入Jann
cout << q.substr(0,4)<<endl;
//自index0取出長度4的字串
cout << q.replace(q.find("Tristina"),8,"Sona") << endl;
// replace(位址,刪除長度,新字串)

/*****************************************
輸出結果:
5
1
Sona a Morgana Tristina
Sona Janna Morgana Tristina
Sona
Sona Janna Morgana Sona
******************************************/
}

GCD:

Ver1:
#include<iostream>
using namespace std;
int gcd(int a,int b)
{
while( (a%=b) && (b%=a));
return a+b;
}
int main()
{
int a,b;
while(cin >> a >> b)cout << gcd(a,b) << endl;
}

Ver2:
#include<iostream>
using namespace std;
int main()
{
int a,b;
while(cin>>a>>b)cout<<__gcd(a,b)<<endl;
}

Floyd–Warshall:
#include < iostream >
using namespace std;
int n;
int map[50][50];
int backtrace[50][50];

void re(int i, int j)
{
    int k = backtrace[i][j];
    if(!k)return;
    re(i, k);
    cout << " => " << k ;
    re(k, j);
}

int main()
{
	
    n = 10;
    for(int i=1; i<=n; i++)
		for(int j=1; j<=n; j++)
		    map[i][j]=999999999;

    for(int k=1; k<=n; k++)
        for(int i=1; i<=n; i++)
            for(int j=1; j<=n; j++)
                if(map[i][k] + map[k][j] < map[i][j])
                {
                    map[i][j] = map[i][k] + map[k][j];
                    backtrace[i][j] = k;
                }
    cout << "從3到5的最短路徑是: ";
    cout << "3";
    re(3,5);
    cout << " => 5" << endl;
    cout << "從3到5的最短路徑長度  : " << map[3][5] << endl;

}