• <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  評(píng)論-40  文章-117  trackbacks-0
            1.字符串字面值
              字符串字面值是一串常量字符,字符串字面值常量用雙引號(hào)括起來(lái)的零個(gè)或多個(gè)字符表示,為兼容C語(yǔ)言,C++中所有的字符串字面值都由編譯器自動(dòng)在末尾添加一個(gè)空字符
              "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風(fēng)格字符串
               字符串字面值的類型實(shí)質(zhì)是const char類型的數(shù)組。C++從C語(yǔ)言繼承下來(lái)的一種通用結(jié)構(gòu)是C風(fēng)格字符串,而字符串字面值就是該類型的實(shí)例。C風(fēng)格字符串是以空字符null結(jié)束的字符數(shù)組:
               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風(fēng)格字符串:ca1是一個(gè)不帶結(jié)束符null的字符數(shù)組,而指針cp1指向ca1,因此,它指向的并不是以null結(jié)束的數(shù)組。
               2.1 C風(fēng)格字符串的使用
                   C++語(yǔ)言通過(guò)(const) char *類型的指針來(lái)操縱C風(fēng)格字符串。
                   const char *cp = "some value";  // 一個(gè)C風(fēng)格字符串
                   while(*cp) //判斷cp當(dāng)前指向的字符是true還是false,true表明這是除null外的任意字符
                   {
                        // do something to *cp
                        ++cp;
                   }
               2.2 C風(fēng)格字符串的標(biāo)準(zhǔn)庫(kù)函數(shù)
                   #include <cstring>  // cstring是string.h頭文件中的C++版本,而string.h是C語(yǔ)言提供的標(biāo)準(zhǔn)庫(kù)
                   操縱C風(fēng)格字符串的標(biāo)準(zhǔn)庫(kù)函數(shù):
                   strlen(s)           // 返回s的長(zhǎng)度,不包括字符串結(jié)束符null
                   strcmp(s1, s2)
                   strcat(s1, s2)      // 將字符串s2連接到s1后,并返回s1 
                   strcpy(s1, s2)      // 將s2復(fù)制給s1,并返回s1
                   strncat(s1, s2, n)  // 將s2的前n個(gè)字符連接到s1后面,并返回s1
                   strncpy(s1, s2, n)  // 將s2的前n個(gè)字符復(fù)制給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 永遠(yuǎn)不要忘記字符串結(jié)束符null
                   char ca[]={'C', '+', '+'};  // not null-terminated
                   cout << strlen(ca) << endl; // disaster: ca isn't null-terminated
               2.4 調(diào)用者必須確保目標(biāo)字符串具有足夠的大小
                   // 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函數(shù)處理C風(fēng)格字符串
                   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 盡可能使用標(biāo)準(zhǔn)庫(kù)類型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
                   此時(shí),標(biāo)準(zhǔn)庫(kù)負(fù)責(zé)處理所有的內(nèi)在管理問(wèn)題。
             

            posted on 2010-05-05 13:07 李陽(yáng) 閱讀(606) 評(píng)論(0)  編輯 收藏 引用 所屬分類: C++
            久久精品国产免费| 国产亚洲美女精品久久久2020| 久久亚洲国产成人影院| 无码任你躁久久久久久久| 丁香色欲久久久久久综合网| 婷婷久久久亚洲欧洲日产国码AV| 99久久无色码中文字幕人妻| 色综合久久精品中文字幕首页 | 久久亚洲精品无码AV红樱桃| 国产婷婷成人久久Av免费高清| 久久国产乱子伦精品免费午夜| 久久福利资源国产精品999| 国内精品久久久人妻中文字幕 | 亚洲国产精品无码久久一线| 久久国产精品久久久| 伊人情人综合成人久久网小说| 99久久99久久| 久久中文字幕人妻熟av女| 99久久免费只有精品国产| av色综合久久天堂av色综合在| 久久国产免费直播| 色综合久久88色综合天天| 亚洲国产精品无码久久久不卡 | avtt天堂网久久精品| 欧美日韩精品久久久久| 久久久精品久久久久特色影视| 国产精品一久久香蕉国产线看 | 日韩久久无码免费毛片软件| 国产韩国精品一区二区三区久久| 精品久久久久久无码不卡| 精品综合久久久久久88小说| 999久久久国产精品| 国产精品无码久久久久久| 伊人色综合久久天天人手人婷| 午夜肉伦伦影院久久精品免费看国产一区二区三区 | 久久久久久毛片免费看| 亚洲精品乱码久久久久66| 一级做a爰片久久毛片人呢| 国产精品久久久久久久久免费| 久久免费的精品国产V∧| 浪潮AV色综合久久天堂|