• <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>

              C++博客 :: 首頁 :: 聯系 :: 聚合  :: 管理
              117 Posts :: 2 Stories :: 61 Comments :: 0 Trackbacks

            常用鏈接

            留言簿(8)

            搜索

            •  

            最新評論

            閱讀排行榜

            評論排行榜


            第一個程序:
                  C-styel string與常規char數組之間的一個重要區別是,字符串有內置的結束字符(前面講過,包含字符,但不以空值,而不是字符串)。這意味著不必將字符串長度作為參數傳遞給函數,而函數可以使用循環依次檢查字符串中的每個字符,直到遇到結尾的空值字符為止。下面演示這種方法,使用一個函數來計算特定的字符在字符串中出現的次數。
            #include "stdafx.h"
            #include 
            <iostream>
            using namespace std;
            int c_in_str(const char * str,char ch);
            int main(int argc, char* argv[])
            {
                
            char mmm[15]="minimum";    //string in an array
              
            // some system require preceding char with static to 
              
            // enable array initialization
                char *wail="ululate";    //wail points to string

                
            int ms=c_in_str(mmm,'m');
                
            int us=c_in_str(wail,'u');
                cout
            <<ms<<" m characters in "<<mmm<<endl;
                cout
            <<us<<" u characters in "<<wail<<endl;
                
            return 0;
            }

            //this function counts the number of ch characters
            //in the string str
            //c_in_str()函數不應修改原始字符串,因此它在聲明形參str時使用了限定符const
            //這樣,如果錯誤地址函數修改了字符串的內容,編譯器將捕獲這種錯誤。
            int c_in_str(const char * str, char ch)
            {
                
            int count=0;
                
            while(*str)    //quit when *str is '\0'
                {
                    
            if(*str==ch)
                        count
            ++;
                    str
            ++;    //move pointer to next char
                }

                
            return count;
            }

            第二個程序:返回C-style string的函數
                  函數無法返回一個字符串,但可以返回字符串的地址,這樣做的效率更高。下面程序定義了一個名為buildstr()的函數,該函數返回一個指針。該函數接受兩個參數:一個字符和一個數字。函數使用new創建一個長度與數字參數相等的字符串,然后將每個元素都初始化為該字符。然后,返回指向新字符串的指針。   
            #include "stdafx.h"
            #include 
            <iostream>
            using namespace std;
            char * buildstr(char c, int n);    //prototype
            int main(int argc, char* argv[])
            {
                
            int times;
                
            char ch;

                cout
            <<"Enter a character: ";
                cin
            >>ch;
                cout
            <<"Enter an integer: ";
                cin
            >>times;
                
            char * ps =buildstr(ch, times);
                cout
            <<ps<<endl;
                delete [] ps;    
            //free memory
                ps=buildstr('+',20);//reuse pointer
                cout<<ps<<"-DONE-"<<ps<<endl;
                delete[] ps;
                
            return 0;
            }

            char * buildstr(char c,int n)
            {
                
            char * pstr =new char[n+1];
                pstr[n]
            ='\0';
                
            //之所以從后向前(而不是從前向后)填充字符串,是為了避免使用額外的變量。
                while((n--)>0)
                    pstr[n]
            =c;    //fill test of string
                return pstr;
            }
            posted on 2010-02-12 17:47 煙皚 閱讀(591) 評論(0)  編輯 收藏 引用 所屬分類: C++ primer plus學習筆記
            一97日本道伊人久久综合影院 | 伊人久久大香线蕉综合5g| 99久久精品免费看国产一区二区三区| AAA级久久久精品无码片| 国产免费福利体检区久久| 久久久久亚洲精品中文字幕| 久久婷婷色综合一区二区| 国产成年无码久久久久毛片| 久久播电影网| 色婷婷久久综合中文久久蜜桃av | 日韩AV毛片精品久久久| 亚洲精品无码久久千人斩| 色综合久久久久网| 亚洲国产精品无码久久一线| 国产成人香蕉久久久久| 人妻无码中文久久久久专区| 久久精品国产亚洲一区二区三区| 色狠狠久久AV五月综合| 中文国产成人精品久久亚洲精品AⅤ无码精品| 无码精品久久久天天影视| 精品国产婷婷久久久| 久久99热精品| 久久亚洲精品成人av无码网站| 合区精品久久久中文字幕一区| 国产亚洲欧美精品久久久| 一级a性色生活片久久无少妇一级婬片免费放| 久久久久久国产精品无码超碰| 热99RE久久精品这里都是精品免费 | 久久这里只有精品18| 久久久久久午夜精品| 久久久久亚洲AV成人网| 91精品国产乱码久久久久久 | 亚洲欧美国产日韩综合久久| 国产精品无码久久久久| 岛国搬运www久久| 精品久久久久久久久久中文字幕 | 国产精品美女久久久久AV福利| 久久久久久久久久久久中文字幕| 日韩精品久久久久久久电影蜜臀| 久久九九久精品国产免费直播| 国产欧美久久久精品影院|