1. 程式人生 > 其它 >理解:std

理解:std

技術標籤:C/C++程式設計c++

文章目錄

一、理解

std::是個名稱空間識別符號(standard),C++標準庫中的函式或者物件都是在名稱空間std中定義的,所以我們要使用標準庫中的函式或者物件都要用std來限定。

1.std::move

#include //包含在這裡

功能:將左值強制轉化為右值避免不必要的拷貝操作,為效能而生,被轉換的就為空了!,等同於被移動),等同於一個型別轉換:static_cast<T&&>(lvalue);

#include <iostream>
#include <utility>
#include <vector> 
#include <string>
int main()
{
    std::string str = "Hello";
    std::vector<std::string> v;//呼叫常規的拷貝建構函式,新建字元陣列,拷貝資料
    v.push_back(str);
    std::cout << "After copy, str is \"" << str << "\"\n";//呼叫移動建構函式,掏空str,掏空後,最好不要使用str
    v.push_back(std::move(str));
    std::cout << "After move, str is \"" << str << "\"\n";
    std::cout << "The contents of the vector are \"" << v[0]
                                         << "\", \"" << v[1] << "\"\n";
}

其他std(已經熟知或者已經在其它地方講過)

#include

  • std::cin;
  • std::cout;
  • std::endl;
  • std::boolalpha:如果不加這句,bool將會輸出0或1,加了之後,就為true或false
  • std::noboolalpha相反

#include

  • std::thread;

#include #include #include

  • std::vector;
  • std::map;
  • std::list;