Junie's Blog

C++ 语法复习

全文共 251预计阅读 1 分钟

基础语法

基本数据类型

#include <iostream>
using namespace std;
int main() {
    int i = 1;
    long long l = 2;
    float f = 3.0;
    double d = 4.0;
    char c = 'c';
    string s = "string";
}

输入输出

#include <iostream>
using namespace std;
int main() {
    ios::sync_with_stdio(false);
    cin.tie(0);
    int a;
    cin >> a;
    cout << a;
    double b;
    while (cin >> b) {
        printf("%.2f", b);
    }
}

基本控制结构

#include <iostream>
using namespace std;
int main() {
    int a;
    cin >> a;
    if (a > 10) {
        cout << "a > 10" << '\n';
    }
    else if (a > 5) {
        cout << "5 < a <= 10" << '\n';
    }
    else {
        cout << "a <= 5" << '\n';
    }
}
#include <iostream>
using namespace std;
int main() {
    for (int i=0;i<10;i++) {
        cout << i << '\n';
    }
    int T = 10;
    while (T--) {
        cout << T << '\n';
    }
}

数组

#include <iostream>
using namespace std;
int main() {
    int a[10] = {1, 2, 3};
    for (int i=0;i<10;i++) {
        cout << a[i] << '\n';
    }
    int b[5][10] = 
}

结构体

#include <iostream>
using namespace std;
struct P {
    int x, y;
};
int main() {
    P p[10];
    p[0].x = 1, p[0].y = 2;
}

常用STL

vector

#include <iostream>
#include <vector>
using namespace std;
int main() {
    vector<int> a(5);
    a.push_back(1);
    a.push_back(2);
    a.push_back(3);
    a[1] = 99;
    a.pop_back();
    cout << a[1] << ' ' << a.size();
}

string

#include <iostream>
#include <string>
using namespace std;
int main() {
    string s = "hello";
    string s2;
    getline(cin, s2);
    s[1] = 'a';
    s = s.substr(2, 3); //位置+数量
    s = s + "olo";
    cout << s << ' ' << s.size() << '\n';
    int index = s.find("lo");
    while (index != s.npos) {
        cout << index << '\n';
        s.replace(index, 2, "?");
        s.insert(index + 1, "!!");
        index = s.find("lo", index + 1);
    }
    cout << s;
}

map/unorder_map

#include <iostream>
#include <map>
using namespace std;
int main() {
    map<string, int> cnt;
    string ss[5] = {"hello", "hello", "bye", "hello", "bye"};
    for (int i=0;i<5;i++) {
        cnt[ss[i]] ++;
    }
    cout << cnt["hello"] << '\n';
    for (auto i = cnt.begin();i != cnt.end(); i++) {
        cout << i->first << ' ' << i->second << endl;
    }
}

set

#include <iostream>
#include <set>
using namespace std;
int main() {
    set<int> sett;
    sett.insert(2);
    sett.insert(1);
    sett.insert(3);
    sett.insert(2);
    sett.erase(1);
    for (auto i = sett.begin(); i != sett.end(); i++) {
        cout << *i << '\n';
    }
    for (auto i : sett) {
        cout << i << '\n';
    }
    cout << sett.count(1) << ' ' << sett.size();
}

queue/stack

#include <iostream>
#include <queue>
using namespace std;
int main() {
    queue<int> q;
    q.push(1);
    q.push(2);
    q.push(3);
    cout << q.front() << ' ' << q.size() << '\n';
    q.pop();
    cout << q.front() << ' ' << q.size() << '\n';
}
#include <iostream>
#include <stack>
using namespace std;
int main() {
    stack<int> stk;
    stk.push(1);
    stk.push(2);
    stk.push(3);
    cout << stk.top() << ' ' << stk.size() << '\n';
    stk.pop();
    cout << stk.top() << ' ' << stk.size() << '\n';
}

sort

#include <iostream>
#include <algorithm>
using namespace std;
struct P {
    int x, y;
};
bool cmp(int x, int y) {
    return x > y;
}
bool cmp2(P a, P b) {
    if (a.x == b.x) return a.y < b.y;
    return a.x < b.x;
}
int main() {
    string s = "bcad";
    sort(s.begin(), s.end());
    int a[5] = {3, 2, 4, 5, 1};
    sort(a, a + 5, cmp);
    for (int i=0;i<5;i++) {
        cout << a[i] << ' ';
    }
    P p[4] = {{1, 2}, {1, 1}, {3, 1}, {2, 1}};
    sort(p, p + 4, cmp2);
    for (int i=0;i<4;i++) {
        cout << p[i].x << ',' <<  p[i].y << ' ';
    }
}

pair/tuple

#include <iostream>
#include <algorithm>
#include <utility>
#include <tuple>
using namespace std;
int main() {
    pair<string, int> p[3] = {{"b", 2}, {"a", 2} ,{"b", 0}};
    sort(p, p + 3);
    for (int i=0;i<3;i++) {
        cout << p[i].first << ',' << p[i].second << '\n';
    }
    p[2] = make_pair("d", 1);
    for (int i=0;i<3;i++) {
        cout << p[i].first << ',' << p[i].second << '\n';
    }
}
#include <iostream>
#include <algorithm>
#include <tuple>
using namespace std;
int main() {
    tuple<string, string, int> t1("hh", "nnb", 1);
    auto t2 = make_tuple(1, 2.3, "hello");
    cout << get<1>(t2);
}

priority_queue

#include <queue>
#include <iostream>
using namespace std;
bool cmp(int a, int b) {
    return a < b;
}
struct tmp {
    bool operator()(int a, int b) {
        return a > b;
    }
};
int main() {
    priority_queue<int, vector<int>, greater<int>> q;
    priority_queue<int, vector<int>, tmp> q2;
    q.push(2);q2.push(2);
    q.push(1);q2.push(1);
    q.push(3);q2.push(3);
    while (!q.empty()) {
        cout << q.top() << endl;
        q.pop();
    }
    while (!q2.empty()) {
        cout << q2.top() << endl;
        q2.pop();
    }
}

技巧

使用 stringstream 分割字符串

#include <iostream>
#include <sstream>
using namespace std;
int main() {
    string s = "ni hao a !!!";
    stringstream ss(s);
    string word;
    while (ss >> word) {
        cout << word << '\n';
    }
}
#include <iostream>
#include <sstream>
using namespace std;
int main() {
    stringstream ss("nihao-hello-nb");
    string s;
    while (getline(ss, s, '-')) {
        cout << s << endl;
    }
}

万能头文件

#include <bits/stdc++.h> //不包含<map>

输入输出重定向

#include <iostream>
using namespace std;
int main() {
    freopen("./input.txt", "r", stdin);
    freopen("./output.txt", "w", stdout);
    int a;
    while (cin >> a) {
        cout << a + 1 << '\n';
    }
}

命令行重定向

test < in.txt > out.txt

随机数

#include <iostream>
#include <cstdlib>
#include <ctime>
using namespace std;
int main() {
    srand(time(0));
    int cnt = 100;
    while (cnt--) {
        printf("%f ", 1.0 * rand() / RAND_MAX);
    }   
}

判断字符是否为数字、字母

#include <iostream>
#include <vector>
using namespace std;
int main() {
    cout << isalpha('B') << '\n';
    cout << isdigit('9') << '\n';
}

常用头文件

头文件名主要功能说明
<iostream>输入输出流,用于 cincout
<cstdio>C语言风格输入输出,用于 scanf, printf
<vector>动态数组容器 vector
<string>string 类型,字符串操作
<algorithm>常用算法函数,如 sort, reverse, min, max
<map> / <unordered_map>关联容器,键值对映射
<set> / <unordered_set>集合容器,自动去重
<queue>队列 queue、优先队列 priority_queue
<stack>栈容器
<deque>双端队列
<list>双向链表结构
<utility>提供 pairmake_pair
<tuple>元组类型
<functional>函数对象,如 greater<int>()
<cmath>数学函数,如 sqrt, abs, pow
<cstdlib>C标准库函数,如 rand(), srand()
<ctime>时间函数,常用于随机种子 srand(time(0))
<sstream>字符串流处理,如 stringstream
<bitset>位集合操作
<limits>获取数据类型的极限值(如 INT_MAX, INT_MIN
<fstream>文件读写(机试较少用)

速查表

函数/类所属头文件
cin, cout<iostream>
vector<vector>
string<string>
sort, reverse<algorithm>
map, set<map>, <set>
queue, stack<queue>, <stack>
sqrt, abs<cmath>
stringstream<sstream>
rand, srand<cstdlib>
time<ctime>
pair<utility>
priority_queue<queue>
unordered_map<unordered_map>

评论