C++ 中写算法题常用的语法
10 次课,每次 2 小时。
第一章:C++ 入门及简单顺序结构
头文件:
- cstdio:
- printf
- scanf
- iostream:
- cin
- cout
- cmath
- fmod
- cstring : c 中函数
- strcpy
- strcmp
- strlen
- string : 可变长的字符序列,比字符数组更加好用
- string s;
- algorithm
- swap
using namespace std;
snippets
#include <iostream>#include <cstdio>using namespace std;int main(){int a1, a2, b1, b2;double a3, b3;scanf("%d%d%lf", &a1, &a2, &a3);scanf("%d%d%lf", &b1, &b2, &b3);printf("VALOR A PAGAR: R$ %.2f\n", a2*a3 + b2*b3);}#include <cmath>using namespace std;double myround(double a, int precision){double b = a;int i=0;while (i++ < precision) b *= 10;b = round(b);i=0;while (i++ < precision) b /= 10.0;return b;}
c++ 每秒最多计算 10e8 (一亿次)。
字符串
输入输出
char *str;cin >> str; // 遇到空格、回车会停止cout << str; // 遇到 \0 停止fgets(str, 100, stdin);string s;getline(cin, s);
string:
http://www.cplusplus.com/reference/string/string/
string s1 , s2;cin >> s1 >> s2;cout <<s1;printf("%s", s1.c_str());getline(cin, s1);// 遍历for (char &c : s1)c += 1;for (char c : s1)cout << c << endl;// 拼接std::string s1, s2;s2 += s1;s2 += "hello";// 初始化string s1(10, 'c');// 比较// 支持 > < >= <= == !=, 按照字典序比较。// 常用成员函数string s1;s1.size();s1.empty();s1.clear();
常用库函数
- reverse:
- 翻转 vector : reverse(v.begin(), v.end())
- 翻转 数组 : int arr[3]; reverse(arr, arr+4);
- unique:
- 需要保证相同元素放在一起
- v.erase(unique(v.begin(), v.end()), v.end());
- random_shuffle :
- 打乱顺序,一般用于生成数据
- sort :
- 快速排序
- cmp 函数:a 是否应该排在 b 前面
- lower_bound / uppeer_bound :
- lower_bound(v.begin(), v.end(), x);
- 返回指针 or 迭代器
- lower_bound:下界,大于等于。
- upper_bound:上界,小于等于。
stl
https://www.geeksforgeeks.org/stack-in-cpp-stl/
- stack
- empty() – Returns whether the stack is empty – Time Complexity : O(1)
- size() – Returns the size of the stack – Time Complexity : O(1)
- top() – Returns a reference to the top most element of the stack – Time Complexity : O(1)
- push(g) – Adds the element ‘g’ at the top of the stack – Time Complexity : O(1)
- pop() – Deletes the top most element of the stack – Time Complexity : O(1)
- vector
- begin() – Returns an iterator pointing to the first element in the vector
- end() – Returns an iterator pointing to the theoretical element that follows the last element in the vector
- size()
- capacity()
- resize()
- empty()
- at(g) – Returns a reference to the element at position ‘g’ in the vector
- front() – Returns a reference to the first element in the vector
- back() – Returns a reference to the last element in the vector
- data() – Returns a direct pointer to the memory array used internally by the vector to store its owned elements.
- push_back() – It push the elements into a vector from the back
- pop_back() – It is used to pop or remove elements from a vector from the back.
- insert() – It inserts new elements before the element at the specified position
- erase() – It is used to remove elements from a container from the specified position or range.
- clear() – It is used to remove all the elements of the vector container
- queue
- dequeue
- set / multiset
- map
next_permutation
