• <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++博客 :: 首頁 :: 聯(lián)系 :: 聚合  :: 管理
              117 Posts :: 2 Stories :: 61 Comments :: 0 Trackbacks

            常用鏈接

            留言簿(8)

            搜索

            •  

            最新評(píng)論

            閱讀排行榜

            評(píng)論排行榜


            第一個(gè)程序:
                  C-styel string與常規(guī)char數(shù)組之間的一個(gè)重要區(qū)別是,字符串有內(nèi)置的結(jié)束字符(前面講過,包含字符,但不以空值,而不是字符串)。這意味著不必將字符串長(zhǎng)度作為參數(shù)傳遞給函數(shù),而函數(shù)可以使用循環(huán)依次檢查字符串中的每個(gè)字符,直到遇到結(jié)尾的空值字符為止。下面演示這種方法,使用一個(gè)函數(shù)來計(jì)算特定的字符在字符串中出現(xiàn)的次數(shù)。
            #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()函數(shù)不應(yīng)修改原始字符串,因此它在聲明形參str時(shí)使用了限定符const
            //這樣,如果錯(cuò)誤地址函數(shù)修改了字符串的內(nèi)容,編譯器將捕獲這種錯(cuò)誤。
            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;
            }

            第二個(gè)程序:返回C-style string的函數(shù)
                  函數(shù)無法返回一個(gè)字符串,但可以返回字符串的地址,這樣做的效率更高。下面程序定義了一個(gè)名為buildstr()的函數(shù),該函數(shù)返回一個(gè)指針。該函數(shù)接受兩個(gè)參數(shù):一個(gè)字符和一個(gè)數(shù)字。函數(shù)使用new創(chuàng)建一個(gè)長(zhǎng)度與數(shù)字參數(shù)相等的字符串,然后將每個(gè)元素都初始化為該字符。然后,返回指向新字符串的指針。   
            #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 煙皚 閱讀(601) 評(píng)論(0)  編輯 收藏 引用 所屬分類: C++ primer plus學(xué)習(xí)筆記
            亚洲欧美日韩中文久久| 久久久高清免费视频| 久久无码中文字幕东京热| 国内精品久久久久久久亚洲| 久久久无码精品亚洲日韩京东传媒| 久久久久成人精品无码| 久久久久久国产精品免费免费| 99久久精品免费看国产一区二区三区| 欧洲精品久久久av无码电影 | 久久97久久97精品免视看秋霞| 99久久婷婷免费国产综合精品| 精品久久久久久亚洲精品 | 手机看片久久高清国产日韩 | 久久大香香蕉国产| 韩国三级大全久久网站| 热99re久久国超精品首页| 99久久婷婷国产综合精品草原| 亚洲国产精久久久久久久| 久久久久无码专区亚洲av| 亚洲国产精品无码久久久久久曰| 精品久久久久久无码不卡| 精品综合久久久久久98| 精品永久久福利一区二区| 国产69精品久久久久99尤物| 亚洲精品美女久久久久99小说 | 久久99精品久久只有精品| 国产91色综合久久免费| 国产高清国内精品福利99久久| 亚洲国产一成久久精品国产成人综合| 久久久久久久精品成人热色戒| 久久精品欧美日韩精品| 亚洲一区中文字幕久久| 尹人香蕉久久99天天拍| 精品久久久噜噜噜久久久| 久久中文字幕视频、最近更新| 国产aⅴ激情无码久久| 久久香蕉一级毛片| 天天爽天天狠久久久综合麻豆| 麻豆精品久久久一区二区| 久久这里只有精品首页| 久久亚洲精品中文字幕三区|