C++11-----Basic2

來源:互聯網
上載者:User

標籤:

1.使用using聲明

  .using namespace spacename

  .using namespace::name

2.標準庫類型string

  Initialization

    .string s     // default null string

    .string s(s1)   // copy initialization

    .string s = s1  // 同上

    .string s ("xxxx")

    .string s = "xxxxx"

    .string s(n, ‘c‘)  // s初始化為連續n個字元c組成的串

    .string s = {‘h‘, ‘e‘, ‘l‘, ‘l‘, ‘0‘};

  Manipulate

    Iterator:

      begin()

      end()

    Capacity:

      size()

      length()

      capacity()

      resize()

      reserve()

      clear()

      empty()

    Access:

      []

      at()

      front()

      back()

    Modifiers:

      operator+=

      append()

      push_back()

      insert()

      erase()

      swap()

    String operations:

      c_str()

      data()

      copy()

      find()

      substr()

      compare()

    Compare:

      ==

      != 

      >  >=

      < <=

    Other:

      is >> s

      os << s

      getline(is, s)

Note:

  •   length() & size(): length() 為C風格,size()為STL引入
  •       [] & at():  [] 當訪問的下標超出時,不進行範圍檢查,其結果是未定義的;at()會進行範圍檢查,下標超出,拋出out_of_range異常
  •       c_str() & data(): c_str()返回一個null結尾的內容的指標;data()僅僅返回內容不包括null的指標,僅僅提供有效字元資料
  •       string對象中不能靠‘\0‘而截斷字串,只有在C風格字串中‘\0‘才具有實際意義

如下,我們編寫了一個teststring.cpp的程式用於測試

編譯運行:

    #g++ -Wall -g -std=c++0x teststring.cpp      //預設產生可執行程式a.out

    #./a.out

說明:

  1.最開始s初始化為空白字串, 列印長度為0

  2.s新增內容,即使有null字元,個數也計算在內, 故size為9;C風格字串,始終以\0最為結束符,故size為5

  3.使用C風格的字串初始化或者添加到string對象中時,會在\0出截斷,\0後的內容被丟棄;使用帶\0的string對象初始化或者添加到新string對象中時,新string對象不會再\0出截斷,如S2列印所示

附錄:

  

                        圖1

                              圖2

Note:

  從圖1總可以開出s += ‘\0‘字元時,調用了push_back()函數,s的長度計數加1

  由此推導,當 += c風格字串時,追加的僅僅是strlen(cstr)的長度; 當+= string對象時,追加的是string.size()的長度

                                      c_str() & data()

Note:

  string對象不以Null 字元(null, ‘\0‘)標識字串結尾

 

3. 範圍for(range for)    // c++11 add

  for(range_declaration:range_expresssion)

    loop_statement;

  range_declaration:  一個變數聲明,其為range_expression的序列的一個元素或引用;通常使用auto來檢測其類型

  range_expression:  能夠表示為序列的運算式或花括弧初始化列表

4.vector

  Initialization

    vector<T>  v

    vector<T>  v1(v)

    vector<T>  v1 = v

    vector<T>  v {.....}      // initialize-list

    vector<T>  v = {......}

    vector<T>  v(n, val)

    vector<T>  v(n)

   Other

    v.empty()

    v.size()

    v.push_back()

    v[n]

     == != > >= < <=

 

5.迭代器(iterator)

 

C++11-----Basic2

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.