C++11必會技能

 

容器:

      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 Functypename... Args

constexpr auto funccall(Func&& funcArgs&&... 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()

 

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章