Cpp 算法题语法速查表

头文件

#include <iostream>
#include <$container>
#include <algorithm>
#include <cmath>
#include <random>

输入输出

std::cin >> num;
std::getline(std::cin, str);
std::cout << num << std::endl;

STL 容器

vector
deque
list
stack
queue
priority_queue
set
unordered_set
map
unordered_map

容器创建方法

std::$container<$type...> name;

以下侧重形式的一致性,一个容器根据其意义决定是否拥有某个函数

(几乎)所有容器都有的函数

size()
empty()
clear()
erase()

长度(元素个数)

普通数组

sizeof(arr) / sizeof(arr[0]);

STL 容器

size()

std::string

作为 STL 容器,可以使用 size()
作为字符串,可以使用 length()

容器系列函数

vector, deque, list

push_back(value)
pop_back()
push_front(value)
pop_front()

stack, queue, priority_queue

push()
pop()
top()
front()
back()

set, unordered_set, map, unordered_map

insert()
find()

注意 find 函数返回迭代器

[] 的使用

vector, deque, string:

container[index] = e

map, unordered_map:

container[key] = value

迭代器的使用

for (auto it = container.begin(); it != container.end(); ++it) {
    // *it / it->first, it->second
}

算法函数

以 vector 为例:

std::sort(vec.begin(), vec.end());
auto it = std::find(vec.begin(), vec.end(), target);
std::copy(vec1.begin(), vec1.end(), vec2.begin());
int count = std::count(vec.begin(), vec.end(), target);
std::reverse(vec.begin(), vec.end());
int sum = std::accumulate(vec.begin(), vec.end(), 0);
auto it = std::lower_bound(vec.begin(), vec.end(), value);
auto it = std::upper_bound(vec.begin(), vec.end(), value);

原博客地址:https://alioth4j.github.io/new-blog/p/cpp-algorithm/

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值