Skip to content

考前必看

return 絕對不能忘記

int dfs(int x){
    return 0;
}
void dfs(int x){
    return;
}
int main(){
    return 0;
}

sort 的 cmp

#include<bits/stdc++.h>
using namespace std;
#define pii pair<int,int>
#define all(x) x.begin(),x.end()
#define x first
#define y second

bool cmp(pii a,pii b){
    return a.y<b.y;
}

int main(){
    vector<pii>v={{4,4},{5,2},{2,3},{6,1}};
    sort(all(v),cmp);
    for(pii i:v){
        cout<<i.x<<" "<<i.y<<"\n";
    }
}

io加速

ios::sync_with_stdio(0);
cin.tie(0);

注意long long!!!

#define int long long
signed main(){

}

易忘英文

iterator

STL 的指標

拆解元素 (tie)

#include<bits/stdc++.h>
using namespace std;

using  pii = pair<int,int>;

int main(){
    pii p={9,2};
    int a,b;
    tie(a,b)=p;
    cout<<a; //9
}

tuple 、 get

#include <bits/stdc++.h>
using namespace std;
int main(){
    tuple<int,int,int> t;
    t={8,2,3};

    cout<<get<1>(t);
}

atoi、stoi、to_string

#include <bits/stdc++.h>
using namespace std;

int toint(char c[11]){
    if(c[0]=='-'){
        int re=0;
        for(int i=1;c[i]!='\0';i++){
            re*=10;
            re+=c[i]-'0';
        }
        return -re;
    }

    int re=0;
    for(int i=0;c[i]!='\0';i++){
        re*=10;
        re+=c[i]-'0';
    }
    return re;
}

int main(){
    char c[11]("1000");
    cout<<c<<"\n";

        cout<<atoi(c)-1<<"\n";

    string s=c;
    cout<<s<<"\n";

        cout<<stoi(s)-1<<"\n";
        cout<<to_string(1000)<<"\n";
        cout<<toint(c)<<"\n";
        cout<<toint("-1000");
}

system、pause

#include <bits/stdc++.h>
using namespace std;
int main(){
    system("pause");
    cout<<"hello";
}

資料型態

istringstream cin(" ");

istringstream cin("5 17 \
5 5 8 3 10");

STL

priority_queue

優先駐列

multiset

多元素set(會排序)

unordered_set

不排序set(單元素)

unordered_multiset

不排序且多重元素

priority_queue

優先駐列

prev_permutation/next_permutation

排列
prev_permutation(v,v+n)

isdigit(字元)/isalpha(字元)

數字、字母判斷

toupper(字元)/tolower(字元)

大小寫轉換

陣列

取字串長度(strlen)

字串轉數字(atoi)

複製(strcpy)

codeblocks 使用技巧

尋找與取代 ctrl + R

ctrl + R

多行註解Ctrl+Shift+C / Ctrl+Shift+x

alt text

Raw string literal 語法

他基本上是寫為:

R"(<字串內容>)"

請看以下程式碼:

code
#include <bits/stdc++.h>
using namespace std;
int main(){
    istringstream cin(R"((13 12\n
456 654\n
798))");
    string s;
    while(getline(cin,s)){
        cout<<s<<"%"; //'%'代表換行
    }
}
output
(13 12\n%456 654\n%798)%

有兩個重點:

  • 如果要'()'的話就要額外寫
  • 裡面的跳脫字元會被視為一般字元