• <ins id="pjuwb"></ins>
    <blockquote id="pjuwb"><pre id="pjuwb"></pre></blockquote>
    <noscript id="pjuwb"></noscript>
          <sup id="pjuwb"><pre id="pjuwb"></pre></sup>
            <dd id="pjuwb"></dd>
            <abbr id="pjuwb"></abbr>
            隨筆-4  評論-40  文章-117  trackbacks-0
            1.字符串字面值
              字符串字面值是一串常量字符,字符串字面值常量用雙引號括起來的零個或多個字符表示,為兼容C語言,C++中所有的字符串字面值都由編譯器自動在末尾添加一個空字符
              "Hello World!" //simple string literal
              ""             //empty string literal
              "\nCC\toptions\tfile.[cC]\n"  //string literal using newlines and tabs

              字符字面值: 'A'  //single quoto:character literal
              字符串字面值: "A"  //double quote:character string literal.包含字母A和空字符的字符串

              字符串字面值的連接:
              std::out << "a multi-line "
                          "string literal"
                          " using concatenation"
                       << std::endl;
              輸出:a multi-line string literal using concatenation

              多行字面值:
              std::out << "a multi-line  \
               string literal\
            using a backslash"
                       << std::endl;
              輸出:a multi-line       string literalusing a backslash


            2.C風格字符串
               字符串字面值的類型實質是const char類型的數組。C++從C語言繼承下來的一種通用結構是C風格字符串,而字符串字面值就是該類型的實例。C風格字符串是以空字符null結束的字符數組:
               char ca1[]={'C', '+', '+'};         // no null, not C-style string
               char ca2[]={'C', '+', '+', '\0'};   // explicit null
               char ca3[]="C++";       // null terminator added automatically
               const char *cp="C++";   // null terminator added automatically
               char *cp1=ca1;      // points to first element of a array, but not C-style string
               char *cp2=ca2;      // points to first element of a null-terminated char array
               ca1和cp1都不是C風格字符串:ca1是一個不帶結束符null的字符數組,而指針cp1指向ca1,因此,它指向的并不是以null結束的數組。
               2.1 C風格字符串的使用
                   C++語言通過(const) char *類型的指針來操縱C風格字符串。
                   const char *cp = "some value";  // 一個C風格字符串
                   while(*cp) //判斷cp當前指向的字符是true還是false,true表明這是除null外的任意字符
                   {
                        // do something to *cp
                        ++cp;
                   }
               2.2 C風格字符串的標準庫函數
                   #include <cstring>  // cstring是string.h頭文件中的C++版本,而string.h是C語言提供的標準庫
                   操縱C風格字符串的標準庫函數:
                   strlen(s)           // 返回s的長度,不包括字符串結束符null
                   strcmp(s1, s2)
                   strcat(s1, s2)      // 將字符串s2連接到s1后,并返回s1 
                   strcpy(s1, s2)      // 將s2復制給s1,并返回s1
                   strncat(s1, s2, n)  // 將s2的前n個字符連接到s1后面,并返回s1
                   strncpy(s1, s2, n)  // 將s2的前n個字符復制給s1,并返回s1
                   if(cp1 < cp2)   // compares address, not the values pointed to
                   const char *cp1 = "A string example";
                   const char *cp2 = "A different string";
                   int i=strcmp(cp1, cp2);   // i is positive
                   i=strcmp(cp2, cp1);       // i is negative
                   i=strcmp(cp1, cp1);       // i is zero
               2.3 永遠不要忘記字符串結束符null
                   char ca[]={'C', '+', '+'};  // not null-terminated
                   cout << strlen(ca) << endl; // disaster: ca isn't null-terminated
               2.4 調用者必須確保目標字符串具有足夠的大小
                   // Dangerous:What happens if we miscalculate the size of largeStr?
                   char largeStr[16+18+2];  // will hold cp1 a space and cp2
                   strcpy(largeStr, cp1);   // copies cp1 into largeStr
                   strcat(largeStr, " ");   // adds a space at end of largeStr
                   strcat(largeStr, cp2);   // concatenates cp2 to largeStr
                   // prints A string example A different string
                   cout << largeStr << endl;
               2.5 使用strn函數處理C風格字符串
                   char largeStr[16+18+2]   // to hold cp1 a space and cp2
                   strncpy(largeStr, cp1, 17);  // size to copy includes the null
                   strncat(largeStr, " ", 2);   // pedantic, but a good habit
                   strncat(largeStr, cp2, 19);  // adds at most 18 characters, plus a null
               2.6 盡可能使用標準庫類型string
                   string largeStr = cp1;   // initialize largeStr as a copy of cp1
                   largeStr += " ";   // add space at end of largeStr
                   largeStr += cp2;   // concatenate cp2 onto end of largeStr
                   此時,標準庫負責處理所有的內在管理問題。
             

            posted on 2010-05-05 13:07 李陽 閱讀(608) 評論(0)  編輯 收藏 引用 所屬分類: C++
            2020久久精品国产免费| 久久综合五月丁香久久激情| 少妇人妻综合久久中文字幕| 7777精品伊人久久久大香线蕉| 综合久久国产九一剧情麻豆| 久久91精品国产91久久麻豆| 国产高潮国产高潮久久久91| 国内精品久久久久久久涩爱 | 国产精品日韩欧美久久综合| 亚洲欧美国产精品专区久久| 日韩人妻无码一区二区三区久久| 一本大道加勒比久久综合| 一97日本道伊人久久综合影院| 麻豆一区二区99久久久久| 欧美日韩成人精品久久久免费看| 久久久久人妻精品一区 | 久久久精品免费国产四虎| 日韩中文久久| 国产精品日韩欧美久久综合| 久久婷婷五月综合97色一本一本| 久久久WWW成人| 久久美女人爽女人爽| 久久国产精品77777| 国产成人综合久久精品红| 97超级碰碰碰碰久久久久| 狠狠色丁香久久婷婷综合五月| 性做久久久久久免费观看| 很黄很污的网站久久mimi色 | 久久婷婷成人综合色综合| 欧美粉嫩小泬久久久久久久| 精品人妻伦一二三区久久| 久久99国产精品久久| 国产精品岛国久久久久| 久久国产亚洲精品无码| 国产一区二区三区久久| 久久精品国产99国产精偷| 狼狼综合久久久久综合网| 久久精品国产亚洲AV麻豆网站| 97精品伊人久久久大香线蕉| 欧洲人妻丰满av无码久久不卡| 色8久久人人97超碰香蕉987|