容器:
std::array – 定长数组,替代特定场景的std::vector使用
std::array<int, 10> arr; 长度为10的数组,不可扩容
std::foword_list – 单向链表,性能比std::list高,但是使用限制多,特定情形可使用
std::unorderd_set/multiset/map/multimap – hash容器,需要提供hash函数和等于判断函数,hash函数用于桶映射,等于函数用于解决hash碰撞问题
emplace系列函数 – 性能优化
Type_traits:
std::is_same<>/std::is_abstract<>/std::is_unsigned<>/std::is_constructible<>…
std::tuple:
是std::pair的扩展,适用范围更广
std::make_tuple/ std::forward_as_tuple
std::tie/std::get<>/std::ignore
std::chrono:
时间相关
std::chrono::system_clock/std::chrono::steady_clock
std::chrono::duration/std::chrono::time_point
std::chrono::second/millisecond…
std::chrono::duration_cast
Function:
std::function
std::bind
std::placeholders::_1/std::_Ph<1>{}…
void f1(int p1, int p2, int p3);
auto f2 = std::bind(f1,std::_Ph<3>{},std::_Ph<2>{},std::_Ph<1>{});
Move语义:
move构造函数/拷贝函数,c11的单例需要多禁用这些
std::swap – 特别是stl容器:
vct2 = vct1;
vct1.clear();
->
vct2.clear();
vct2.swap(vct1);
std::forward – 完美转发,模板当中模板参数T,函数参数T&& t,使用参数std::forward<T>(t)
异步操作:
std::async
std::launch::async/defer
std::future<>/get/wait/future_status(defered/timeout/ready)…
std::promise<>/get_future/set_value… - 可以自己指定设置值的时机,可用于线程同步,代替win32 Event
std::packaged_task<>/get_future/operator() – 将一个函数(可执行对象)包装成一个异步执行对象,并不立即执行
线程同步:
std::mutex
std::lock_guard<> - raii
std::unique_lock<> -raii
std::condition_variable/std::condition_variable_any
wait/notify_one…
thread_local – 当前线程作用域的静态变量
std::call_once/std::once_flag – 多线程中保证值执行一次,c11原生支持静态局部变量构造线程安全,避免双检锁
std::thread
std::this_thread
sleep_for/sleep_until/yield
原子操作
std::atomic<>
std::atomic_int/std::atomic_bool…
compare_exchange_strong/ compare_exchange_weak
简单的++/--/赋值/拷贝 都是原子的
以下代码存在线程安全问题,行2和行3之间cpu切换则程序可能出错
std::atomic_bool b;
if(!b)
b = true;
else
…;
改成:
bool exp = false;
b. compare_exchange_strong(exp,true);
if(b)
…;
智能指针:
std::shared_ptr<>/ std::weak_ptr<>
std::unique_ptr – 替代std::auto_ptr,只能move,不能copy
变长参数模板:
typename…/Args…/args…
template<typename Func, typename... Args>
constexpr auto funccall(Func&& func, Args&&... args)
// noexcept(noexcept(std::forward<Func>(func)(std::forward<Args>(args)...)))
-> typename std::result_of<Func(Args...)>::type
{
return std::forward<Func>(func)(std::forward<Args>(args)...);
}
其他:
lambda表达式 – [捕获] (参数) 异常声明 ->返回类型声明 {语句块};
auto
nullptr
for(const ElementType& e : arr){…;}
正则表达式 std::regex
override/final/delete
初始化列表
就地初始化
代理构造函数 - CLSA(int a):CLSA(a, 1){} CLSA(int a, int b):m_a(a),m_b(b){}
继承构造函数 - class CLSB : public CLSA{ using CLSA::CLSA; }
constexpr - 常量表达式
static_assert – 编译期断言
enable_if – SFINAE(匹配失败不是错误)
后置返回声明 - ->
enum class
更通用的迭代器模式 – std::begin()/std::end()