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

  1. #include <iostream>
  2. #include <cstdio>
  3. using namespace std;
  4. int main(){
  5. int a1, a2, b1, b2;
  6. double a3, b3;
  7. scanf("%d%d%lf", &a1, &a2, &a3);
  8. scanf("%d%d%lf", &b1, &b2, &b3);
  9. printf("VALOR A PAGAR: R$ %.2f\n", a2*a3 + b2*b3);
  10. }
  11. #include <cmath>
  12. using namespace std;
  13. double myround(double a, int precision){
  14. double b = a;
  15. int i=0;
  16. while (i++ < precision) b *= 10;
  17. b = round(b);
  18. i=0;
  19. while (i++ < precision) b /= 10.0;
  20. return b;
  21. }

c++ 每秒最多计算 10e8 (一亿次)。

字符串

输入输出

  1. char *str;
  2. cin >> str; // 遇到空格、回车会停止
  3. cout << str; // 遇到 \0 停止
  4. fgets(str, 100, stdin);
  5. string s;
  6. getline(cin, s);

string:
http://www.cplusplus.com/reference/string/string/

  1. string s1 , s2;
  2. cin >> s1 >> s2;
  3. cout <<s1;
  4. printf("%s", s1.c_str());
  5. getline(cin, s1);
  6. // 遍历
  7. for (char &c : s1)
  8. c += 1;
  9. for (char c : s1)
  10. cout << c << endl;
  11. // 拼接
  12. std::string s1, s2;
  13. s2 += s1;
  14. s2 += "hello";
  15. // 初始化
  16. string s1(10, 'c');
  17. // 比较
  18. // 支持 > < >= <= == !=, 按照字典序比较。
  19. // 常用成员函数
  20. string s1;
  21. s1.size();
  22. s1.empty();
  23. 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