• <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>
            Welcome to Leon's Blog  
            日歷
            <2025年5月>
            27282930123
            45678910
            11121314151617
            18192021222324
            25262728293031
            1234567
            統計
            • 隨筆 - 30
            • 文章 - 0
            • 評論 - 51
            • 引用 - 0

            導航

            常用鏈接

            留言簿(4)

            隨筆分類

            隨筆檔案

            ACM

            搜索

            •  

            最新評論

            閱讀排行榜

            評論排行榜

             

            置頂隨筆

                 摘要: 今天下午做了一道acm的題,提交了10次都是WA,所以想請大家幫我看看到底哪里不正確,程序哪里寫的不好!謝謝大家了!代碼:   1#include <stdlib.h>  2#include <stdio.h>  3typedef struct telNumber ...  閱讀全文
            posted @ 2008-05-15 19:29 Leon916 閱讀(229) | 評論 (0)編輯 收藏

            2009年9月13日

                  昨天在csdn上看到這個帖子,是關于歸并排序的,發現錯誤很隱蔽(在我看來),后來經過調試和跟蹤才找到了這個錯誤。結果是一個很簡單的邏輯問題,自己太笨了,花了3、4個小時。哎,還是看程序時粗枝大葉!
            posted @ 2009-09-13 10:28 Leon916 閱讀(264) | 評論 (0)編輯 收藏

            2009年9月11日

                   由于工作需要,這兩天看看了STL的源代碼,發現里面有很多的技巧和技術,以及C++語言的使用,有一種震撼的感覺。
                   簡單總結一下: 目前看到的技巧有:traite技術,仿函數(仿函數都是定義了()函數運算操作符的類)。關于仿函數可以參看http://blog.csdn.net/kingofbirdzjy/archive/2007/12/10/1926653.aspx
            posted @ 2009-09-11 22:04 Leon916 閱讀(299) | 評論 (0)編輯 收藏
             
                   好長時間沒有寫程序了,敲代碼+調試花了我2個小時時間,就為了一道特簡單題目(acm3299),以后要多多鍛煉才行。
             代碼如下:
             1#include <stdio.h>
             2#include <math.h>
             3
             4const float E = 2.718281828;
             5
             6int main(int argc, char *argv[])
             7{
             8    float t, d, h, e, hh;
             9    int index;
            10    char temp;
            11
            12    while( (scanf("%c"&temp) == 1&& (temp != 'E'))
            13    {
            14        index = 0;
            15        forint i = 0 ; i < 2;i++)
            16        {
            17            switch(temp)
            18            {
            19            case 'T':
            20                index |= 0x1;
            21                scanf("%f"&t);
            22                break;
            23            case 'D':
            24                index |= 0x2;
            25                scanf("%f"&d);
            26                break;
            27            case 'H':
            28                index |= 0x4;
            29                scanf("%f"&h);
            30                break;
            31            default:
            32                i--;
            33                break;
            34            }

            35            scanf("%c",&temp);
            36        }

            37        switch(index)
            38        {
            39            
            40        case 3:  // t d
            41            e = 6.11 * exp(5417.7530*((1/273.16-(1/(d+273.16))));
            42            hh = (0.5555* (e - 10.0);
            43            h = t + hh;
            44            break;
            45        case 6:        //d, h ----t
            46            e = 6.11 * exp(5417.7530*((1/273.16-(1/(d+273.16))));
            47            t = h - 0.5555 * (e - 10.0); 
            48            break;
            49        case 5:            //t h ---d
            50            double e;
            51            e = (h-t)/0.5555 + 10.0;
            52            d = 1/((1/273.16- (log(e/6.11))/5417.753- 273.16;
            53            break;
            54        }

            55        printf("T %.1f D %.1f H %.1f\n", t, d, h);
            56    }

            57    return 0;
            58}

            59
            posted @ 2009-09-11 17:08 Leon916 閱讀(197) | 評論 (0)編輯 收藏

            2009年4月10日

            今天寫了一個關于C語言讀寫的小程序,代碼如下:
            int main(int argc, char *argv[])
            {
                FILE 
            *file;
                FILE 
            *fw;
                
            if( (file = fopen("main.cpp""r")) == NULL)
                    
            return -1;
                
            if( (fw = fopen("text.txt""wt")) == NULL)
                    
            return -1;

                
            char ch;
                
            while( (ch=fgetc(file)) != EOF )
                {
                    fputc(ch, fw);
                    printf(
            "%c", ch);
                }

                fclose(file);
                fclose(fw);
                
            return 0;
            }

                
            //char ch[LEN] = {0};
                
            //while( !feof(file) )
                
            //{
                
            //    fread(ch, sizeof(char), LEN-1, file);
                
            //    printf("%s", ch);
                
            //    fwrite(ch, sizeof(char), LEN-1, fw);
                
            //    memset(ch, 0, sizeof(char)*LEN);
                
            //}
            這是用通過調用c語言的庫文件來實現。
            以下是用C++的中的stream來實現的,但是在處理中文的時候,會出現一些亂碼。
            //fstream fs;
            //    fs.open(_T("文本.txt"));
            //
            //    char arr[LEN] = {0};
            //    wchar_t temp[2*LEN+1] = {0};
            //    int length;
            //    if(!fs)
            //    {
            //        cout << "Error" << endl;
            //        return -1;
            //    }
                
            //while(!fs.eof())
                
            //{
                
            //    fs.read(arr, sizeof(arr)-1);
                
            //    length = MultiByteToWideChar(CP_ACP, 0, arr, -1, NULL, NULL);
                
            //    MultiByteToWideChar(CP_ACP, 0, arr, -1, temp, length);
                
            //    wcout.imbue(locale("chs"));
                
            //    wcout << temp;
                
            //}
            但是通過char類型進行讀寫卻不出現亂碼,感覺很奇怪。
            int main(int argc, char *argv[])
            {
                fstream fs;
                fs.open(_T(
            "文本.txt"));

                fstream fw;
                fw.open(
            "text.txt");
                
            char arr[LEN] = {0};
                wchar_t temp[
            2*LEN+1= {0};
                
            int length;
                
            if(!fs || !fw)
                {
                    cout 
            << "Error" << endl;
                    
            return -1;
                }
                
            while(!fs.eof())
                {
                    fs.read(arr, 
            sizeof(arr)-1);
                    cout 
            << arr;
                    fw.write(arr, 
            sizeof(arr)-1);
                    memset(arr, 
            0sizeof(arr));

                }
                cout 
            << endl;
                fs.close();
                fw.close();
                
            return 0;
            }


            posted @ 2009-04-10 11:02 Leon916 閱讀(1591) | 評論 (1)編輯 收藏

            2009年3月29日

                這是我在看一個Java開源軟件的時候,發現一個人寫的代碼,里面有三個函數到現在還沒有看懂,先放在blog里面。
            posted @ 2009-03-29 15:54 Leon916 閱讀(1299) | 評論 (3)編輯 收藏

            2009年3月9日

                這兩天又開始做ACM的題,但是從簡單的題目開始。發現自己有一個毛病,就是不認真審題,不動腦筋,這是個不好的兆頭。希望自己能夠克服,認真思考,這樣才能夠有進步!!
            posted @ 2009-03-09 22:04 Leon916 閱讀(121) | 評論 (0)編輯 收藏

            2008年12月26日

               這個星期開始學習編譯原理,找了本書,是關于lcc編譯器的書,已經看到第五章了,但是這五章的內容對我來說簡直跟地獄一樣,都不好理解。
               最容易的是第二章,是關于內存管理,到完之后感覺跟STL的內存管理有點相似,都是把自己管理分配的內存,當需要的內存不夠用的時候,在調用malloc重新申請。
               對于后面幾章,還沒有徹底理解,還需要花一些時間,邊讀代碼邊看書,一定要理解它是如何管理符號、標識符和連接前端和后端的接口。

               鼓勵一下自己,第一次讀這么復雜的東西,但是這是成長的必由之路,加油!

            ps:又要從頭開始看了,有些東西還是不懂!加油!!!

            posted @ 2008-12-26 14:37 Leon916 閱讀(248) | 評論 (0)編輯 收藏

            2008年12月5日

            之所以拋棄char*的字符串而選用C++標準程序庫中的string類,是因為他和前者比較起來,不必擔心內存是否足夠、字符串長度等等,而且作 為一個類出現,他集成的操作函數足以完成我們大多數情況下(甚至是100%)的需要。我們可以用 = 進行賦值操作,== 進行比較,+ 做串聯(是不是很簡單?)。我們盡可以把它看成是C++的基本數據類型。
               好了,進入正題………
            首先,為了在我們的程序中使用string類型,我們必須包含頭文件 。如下:
               #include //注意這里不是string.h string.h是C字符串頭文件

            1.聲明一個C++字符串
            聲明一個字符串變量很簡單:
               string Str;
            這樣我們就聲明了一個字符串變量,但既然是一個類,就有構造函數和析構函數。上面的聲明沒有傳入參數,所以就直接使用了string的默認的構造函數,這個函數所作的就是把Str初始化為一個空字符串。String類的構造函數和析構函數如下:
            a)    string s;  //生成一個空字符串s
            b)    string s(str) //拷貝構造函數 生成str的復制品
            c)    string s(str,stridx) //將字符串str內“始于位置stridx”的部分當作字符串的初值
            d)    string s(str,stridx,strlen) //將字符串str內“始于stridx且長度頂多strlen”的部分作為字符串的初值
            e)    string s(cstr) //將C字符串作為s的初值
            f)    string s(chars,chars_len) //將C字符串前chars_len個字符作為字符串s的初值。
            g)    string s(num,c) //生成一個字符串,包含num個c字符
            h)    string s(beg,end) //以區間beg;end(不包含end)內的字符作為字符串s的初值
            i)    s.~string() //銷毀所有字符,釋放內存
            都很簡單,我就不解釋了。
            2.字符串操作函數
               這里是C++字符串的重點,我先把各種操作函數羅列出來,不喜歡把所有函數都看完的人可以在這里找自己喜歡的函數,再到后面看他的詳細解釋。
            a) =,assign()   //賦以新值
            b) swap()   //交換兩個字符串的內容
            c) +=,append(),push_back() //在尾部添加字符
            d) insert() //插入字符
            e) erase() //刪除字符
            f) clear() //刪除全部字符
            g) replace() //替換字符
            h) + //串聯字符串
            i) ==,!=,<,<=,>,>=,compare()  //比較字符串
            j) size(),length()  //返回字符數量
            k) max_size() //返回字符的可能最大個數
            l) empty()  //判斷字符串是否為空
            m) capacity() //返回重新分配之前的字符容量
            n) reserve() //保留一定量內存以容納一定數量的字符
            o) [ ], at() //存取單一字符
            p) >>,getline() //從stream讀取某值
            q) <<  //將謀值寫入stream
            r) copy() //將某值賦值為一個C_string
            s) c_str() //將內容以C_string返回
            t) data() //將內容以字符數組形式返回
            u) substr() //返回某個子字符串
            v)查找函數
            w)begin() end() //提供類似STL的迭代器支持
            x) rbegin() rend() //逆向迭代器
            y) get_allocator() //返回配置器
            下面詳細介紹:
            2.1 C++字符串和C字符串的轉換
               C++提供的由C++字符串得到對應的C_string的方法是使用data()、c_str()和copy(),其中,data()以字符數組的形式 返回字符串內容,但并不添加’\0’。c_str()返回一個以‘\0’結尾的字符數組,而copy()則把字符串的內容復制或寫入既有的 c_string或字符數組內。C++字符串并不以’\0’結尾。我的建議是在程序中能使用C++字符串就使用,除非萬不得已不選用c_string。由 于只是簡單介紹,詳細介紹掠過,誰想進一步了解使用中的注意事項可以給我留言(到我的收件箱)。我詳細解釋。
            2.2 大小和容量函數
               一個C++字符串存在三種大小:a)現有的字符數,函數是size()和length(),他們等效。Empty()用來檢查字符串是否為空。 b)max_size() 這個大小是指當前C++字符串最多能包含的字符數,很可能和機器本身的限制或者字符串所在位置連續內存的大小有關系。我們一般情況下不用關心他,應該大小 足夠我們用的。但是不夠用的話,會拋出length_error異常c)capacity()重新分配內存之前 string所能包含的最大字符數。這里另一個需要指出的是reserve()函數,這個函數為string重新分配內存。重新分配的大小由其參數決定, 默認參數為0,這時候會對string進行非強制性縮減。

            還有必要再重復一下C++字符串和C字符串轉換的問題,許多人會遇到這樣的問 題,自己做的程序要調用別人的函數、類什么的(比如數據庫連接函數Connect(char*,char*)),但別人的函數參數用的是char*形式 的,而我們知道,c_str()、data()返回的字符數組由該字符串擁有,所以是一種const char*,要想作為上面提及的函數的參數,還必須拷貝到一個char*,而我們的原則是能不使用C字符串就不使用。那么,這時候我們的處理方式是:如果 此函數對參數(也就是char*)的內容不修改的話,我們可以這樣Connect((char*)UserID.c_str(), (char*)PassWD.c_str()),但是這時候是存在危險的,因為這樣轉換后的字符串其實是可以修改的(有興趣地可以自己試一試),所以我強 調除非函數調用的時候不對參數進行修改,否則必須拷貝到一個char*上去。當然,更穩妥的辦法是無論什么情況都拷貝到一個char*上去。同時我們也祈 禱現在仍然使用C字符串進行編程的高手們(說他們是高手一點兒也不為過,也許在我們還穿開襠褲的時候他們就開始編程了,哈哈…)寫的函數都比較規范,那樣 我們就不必進行強制轉換了。

            2.3元素存取
               我們可以使用下標操作符[]和函數at()對元素包含的字符進行訪問。但是應該注意的是操作符[]并不檢查索引是否有效(有效索引 0~str.length()),如果索引失效,會引起未定義的行為。而at()會檢查,如果使用at()的時候索引無效,會拋出 out_of_range異常。
               有一個例外不得不說,const string a;的操作符[]對索引值是a.length()仍然有效,其返回值是’\0’。其他的各種情況,a.length()索引都是無效的。舉例如下:
            const string Cstr(“const string”);
            string Str(“string”);

            Str[3];    //ok
            Str.at(3);  //ok

            Str[100]; //未定義的行為
            Str.at(100);  //throw out_of_range

            Str[Str.length()]  //未定義行為
            Cstr[Cstr.length()] //返回 ‘\0’
            Str.at(Str.length());//throw out_of_range
            Cstr.at(Cstr.length()) ////throw out_of_range

            我不贊成類似于下面的引用或指針賦值:
            char& r=s[2];
            char* p= &s[3];
            因為一旦發生重新分配,r,p立即失效。避免的方法就是不使用。

            2.4比較函數
               C++字符串支持常見的比較操作符(>,>=,<,<=,==,!=),甚至支持string與C-string的比較(如 str<”hello”)。在使用>,>=,<,<=這些操作符的時候是根據“當前字符特性”將字符按字典順序進行逐一得 比較。字典排序靠前的字符小,比較的順序是從前向后比較,遇到不相等的字符就按這個位置上的兩個字符的比較結果確定兩個字符串的大小。同 時,string(“aaaa”)    另一個功能強大的比較函數是成員函數compare()。他支持多參數處理,支持用索引值和長度定位子串來進行比較。他返回一個整數來表示比較結果,返 回值意義如下:0-相等 〉0-大于 <0-小于。舉例如下:
               string s(“abcd”);
               
               s.compare(“abcd”); //返回0
               s.compare(“dcba”); //返回一個小于0的值
               s.compare(“ab”); //返回大于0的值
               
            s.compare(s); //相等
               s.compare(0,2,s,2,2); //用”ab”和”cd”進行比較 小于零
               s.compare(1,2,”bcx”,2); //用”bc”和”bc”比較。
            怎么樣?功能夠全的吧!什么?還不能滿足你的胃口?好吧,那等著,后面有更個性化的比較算法。先給個提示,使用的是STL的比較算法。什么?對STL一竅不通?靠,你重修吧!

            2.5 更改內容
            這在字符串的操作中占了很大一部分。
            首先講賦值,第一個賦值方法當然是使用操作符=,新值可以是string(如:s=ns) 、c_string(如:s=”gaint”)甚至單一字符(如:s=’j’)。還可以使用成員函數assign(),這個成員函數可以使你更靈活的對字符串賦值。還是舉例說明吧:
            s.assign(str); //不說
            s.assign(str,1,3);//如果str是”iamangel” 就是把”ama”賦給字符串
            s.assign(str,2,string::npos);//把字符串str從索引值2開始到結尾賦給s
            s.assign(“gaint”); //不說
            s.assign(“nico”,5);//把’n’ ‘I’ ‘c’ ‘o’ ‘\0’賦給字符串
            s.assign(5,’x’);//把五個x賦給字符串
            把字符串清空的方法有三個:s=””;s.clear();s.erase();(我越來越覺得舉例比說話讓別人容易懂!)。
            string提供了很多函數用于插入(insert)、刪除(erase)、替換(replace)、增加字符。
            先說增加字符(這里說的增加是在尾巴上),函數有 +=、append()、push_back()。舉例如下:
            s+=str;//加個字符串
            s+=”my name is jiayp”;//加個C字符串
            s+=’a’;//加個字符

            s.append(str);
            s.append(str,1,3);//不解釋了 同前面的函數參數assign的解釋
            s.append(str,2,string::npos)//不解釋了

            s.append(“my name is jiayp”);
            s.append(“nico”,5);
            s.append(5,’x’);

             

                字符串操作是一個不小的主題,在標準C++中,string字符串類成為一個標準,之所以拋棄char*的字符串而選用C++標準程序庫中的string類,是因為他和前者比較起來,不必擔心內存是否足夠、字符串長度等等,而且作為一個類出現,他集成的操作函數足以完成我們大多數情況下的需要.
                下面我們首先從一些示例開始學習下string類的使用.
            1)
            #include <string>
            #include <iostream>
            using namespace std;

            void main()
            {
                string s("hehe");
                cout<<s<<endl;
                cin.get();
            }
            2)
            #include <string>
            #include <iostream>
            using namespace std;

            void main()
            {
                char chs[] = "hehe";
                string s(chs);
                cout<<s<<endl;
                cin.get();
            }
            3)
            #include <string>
            #include <iostream>
            using namespace std;

            void main()
            {
                char chs[] = "hehe";
                string s(chs,1,3);    //指定從chs的索引1開始,最后復制3個字節
                cout<<s<<endl;
                cin.get();
            }
            4)
            #include <string>
            #include <iostream>
            using namespace std;

            void main()
            {
                string s1("hehe");
                string s2(s1);   
                cout<<s2<<endl;
                cin.get();
            }
            5)
            #include <string>
            #include <iostream>
            using namespace std;

            void main()
            {
                string s1("hehe",2,3);
                string s2(s1);   
                cout<<s2<<endl;
                cin.get();
            }
            6)
            #include <string>
            #include <iostream>
            using namespace std;

            void main()
            {
                char chs[] = "hehe";
                string s(chs,3);    //將chs前3個字符作為初值構造
                cout<<s<<endl;
                cin.get();
            }
            7)
            #include <string>
            #include <iostream>
            using namespace std;

            void main()
            {
                string s(10,'k');    //分配10個字符,初值都是'k'
                cout<<s<<endl;
                cin.get();
            }
            //以上是string類實例的構造手段,都很簡單.

            9)
            //賦新值
            #include <string>
            #include <iostream>
            using namespace std;

            void main()
            {
                string s(10,'k');    //分配10個字符,初值都是'k'
                cout<<s<<endl;
                s = "hehehehe";
                cout<<s<<endl;
                s.assign("kdje");
                cout<<s<<endl;
                s.assign("fkdhfkdfd",5);    //重新分配指定字符串的前5的元素內容
                cout<<s<<endl;       
                cin.get();
            }
            10)
            //swap方法交換
            #include <string>
            #include <iostream>
            using namespace std;

            void main()
            {
                string s1 = "hehe";
                string s2 = "gagaga";
                cout<<"s1 : "<<s1<<endl;
                cout<<"s2 : "<<s2<<endl;
                s1.swap(s2);
                cout<<"s1 : "<<s1<<endl;
                cout<<"s2 : "<<s2<<endl;
                cin.get();
            }
            11)
            //+=,append(),push_back()在尾部添加字符
            #include <string>
            #include <iostream>
            using namespace std;

            void main()
            {
                string s = "hehe";
                s += "gaga";
                cout<<s<<endl;
                s.append("嘿嘿");    //append()方法可以添加字符串
                cout<<s<<endl;
                s.push_back('k');    //push_back()方法只能添加一個字符...
                cout<<s<<endl;
                cin.get();
            }
            12)
            //insert() 插入字符.其實,insert運用好,與其他的插入操作是一樣的.
            #include <string>
            #include <iostream>
            using namespace std;

            void main()
            {
                string s = "hehe";
                s.insert(0,"頭部");            //在頭部插入
                s.insert(s.size(),"尾部");    //在尾部插入
                s.insert(s.size()/2,"中間");//在中間插入
                cout<<s<<endl;
                cin.get();
            }
            13)
            #include <string>
            #include <iostream>
            using namespace std;

            void main()
            {
                string s = "abcdefg";
                s.erase(0,1);    //從索引0到索引1,即刪除掉了'a'
                cout<<s<<endl;
                //其實,還可以使用replace方法來執行刪除操作
                s.replace(2,3,"");//即將指定范圍內的字符替換成"",即變相刪除了
                cout<<s<<endl;
                cin.get();
            }

            14)
            //clear() 刪除全部字符
            #include <string>
            #include <iostream>
            using namespace std;

            void main()
            {
                string s = "abcdefg";
                cout<<s.length()<<endl;
                s.clear();
                cout<<s.length()<<endl;
                //使用earse方法變相全刪除
                s = "dkjfd";
                cout<<s.length()<<endl;
                s.erase(0,s.length());
                cout<<s.length()<<endl;

                cin.get();
            }
            15)
            //replace() 替換字符
            #include <string>
            #include <iostream>
            using namespace std;

            void main()
            {
                string s = "abcdefg";
                s.replace(2,3,"!!!!!");//從索引2開始3個字節的字符全替換成"!!!!!"
                cout<<s<<endl;
                cin.get();
            }
            16)
            //==,!=,<,<=,>,>=,compare()  比較字符串
            #include <string>
            #include <iostream>
            using namespace std;

            void main()
            {
                string s1 = "abcdefg";
                string s2 = "abcdefg";   
                if (s1==s2)cout<<"s1 == s2"<<endl;
                else cout<<"s1 != s2"<<endl;
               
                if (s1!=s2)cout<<"s1 != s2"<<endl;
                else cout<<"s1 == s2"<<endl;
               
                if (s1>s2)cout<<"s1 > s2"<<endl;
                else cout<<"s1 <= s2"<<endl;
               
                if (s1<=s2)cout<<"s1 <= s2"<<endl;
                else cout<<"s1 > s2"<<endl;

                cin.get();
            }
            17)
            //size(),length()  返回字符數量
            #include <string>
            #include <iostream>
            using namespace std;

            void main()
            {
                string s = "abcdefg";
                cout<<s.size()<<endl;
                cout<<s.length()<<endl;

                cin.get();
            }
            18)
            //max_size() 返回字符的可能最大個數
            #include <string>
            #include <iostream>
            using namespace std;

            void main()
            {
                string s = "abcdefg";
                cout<<s.max_size()<<endl;

                cin.get();
            }
            19)
            //empty()  判斷字符串是否為空
            #include <string>
            #include <iostream>
            using namespace std;

            void main()
            {
                string s ;
                if (s.empty())
                    cout<<"s 為空."<<endl;
                else
                    cout<<"s 不為空."<<endl;

                s = s + "abcdefg";
                if (s.empty())
                    cout<<"s 為空."<<endl;
                else
                    cout<<"s 不為空."<<endl;

                cin.get();
            }
            20)
            // [ ], at() 存取單一字符
            #include <string>
            #include <iostream>
            using namespace std;

            void main()
            {
                string s = "abcdefg1111";
               
                cout<<"use []:"<<endl;
                for(int i=0; i<s.length(); i++)
                {
                    cout<<s[i]<<endl;
                }
                cout<<endl;

                cout<<"use at():"<<endl;
                for(int i=0; i<s.length(); i++)
                {
                    cout<<s.at(i)<<endl;
                }
                cout<<endl;
               
                cin.get();
            }
            21)
            #include <string>
            #include <iostream>
            using namespace std;

            void main()
            {
                string s = "abcdefg1111";
               
                const char * chs1 = s.c_str();
                const char * chs2 = s.data();

                cout<<"use at():"<<endl;
                int i;
                for(i=0; i<s.length(); i++)
                {
                    cout<<"c_str() : "<<chs1[i]<<endl;
                    cout<<"data() : "<<chs2[i]<<endl;
                }
                cout<<"c_str() : "<<chs1<<endl;
                cout<<"data() : "<<chs2<<endl;
                cout<<endl;
               
                cin.get();
            }
            22)
            // substr() 返回某個子字符串
            #include <string>
            #include <iostream>
            using namespace std;

            void main()
            {
                string s = "abcdefg1111";
               
                string str = s.substr(5,3);//從索引5開始3個字節
                cout<<str<<endl;
               
                cin.get();
            }
            23)
            // find 查找函數
            #include <string>
            #include <iostream>
            using namespace std;

            void main()
            {
                string s = "abcdefg1111";
                string pattern = "fg";
                string::size_type pos;
                pos = s.find(pattern,0);        //從索引0開始,查找符合字符串"f"的頭索引
                cout<<pos<<endl;
                string str = s.substr(pos,pattern.size());
                cout<<str<<endl;
                cin.get();
            }
            24)
            // begin() end() 提供類似STL的迭代器支持
            #include <string>
            #include <iostream>
            using namespace std;

            void main()
            {
                string s = "abcdefg1111";
                for(string::iterator iter = s.begin(); iter!=s.end(); iter++)
                {
                    cout<<*iter<<endl;
                }
                cout<<endl;

                cin.get();
            }
                    一個C++字符串存在三種大小:a)現有的字符數,函數是size()和length(),他們等效。 Empty()用來檢查字符串是否為空。b)max_size() 這個大小是指當前C++字符串最多能包含的字符數,很可能和機器本身的限制或者字符串所在位置連續內存的大小有關系。我們一般情況下不用關心他,應該大小 足夠我們用的。但是不夠用的話,會拋出length_error異常c)capacity()重新分配內存之前 string所能包含的最大字符數。這里另一個需要指出的是reserve()函數,這個函數為string重新分配內存。重新分配的大小由其參數決定, 默認參數為0,這時候會對string進行非強制性縮減
            posted @ 2008-12-05 22:33 Leon916 閱讀(11559) | 評論 (3)編輯 收藏
             
            vector <int> vNew(v);(v是一個vector <int>)
            按上面的語法,創建一個vector,是按照v的實際元素個數創建新vector的。就是說,如果v中元素是1,2,3,那么,vNew中的元素是1,2,3,并且,vNew的總容量也是3.
            也就是:vNew的總容量取決于v的實際元素個數。

            pVec->clear();
            clear()只是刪除[first,last)區間中的所有元素,但并不釋放其內存。

            (std::vector  <int >)(*pVec)
            也就是創建一個臨時對象vector <int>,顯然,這個臨時對象的總容量是取決于*pVec的實際元素個數的,而經過
            pVec->clear();的pVect的實際元素個數是0,因此,pVec- >swap( (std::vector  <int >)(*pVec) ); 將這個臨時對象與pVec交換后,它的總容量變成了0。
            (vector中的swap()實現的是:將first、last,end_of_storage(總容量)依次交換)。

            因此,實現了*pVec沒使用內存的釋放。
            posted @ 2008-12-05 13:21 Leon916 閱讀(506) | 評論 (0)編輯 收藏

            2008年10月28日

            char *p;
            cout << sizeof*(p) << endl;
            請問這個的輸出?

            經過調試,我發現sizeof*(p)的值是p類型所占的字節數。
            char輸出為1,double:8.


            char x[0];
            x是什么類型?
            我在VS2005下調試,結果是錯誤  cannot allocate an array of constant size 0 。
            posted @ 2008-10-28 08:54 Leon916 閱讀(190) | 評論 (0)編輯 收藏
             
            Copyright © Leon916 Powered by: 博客園 模板提供:滬江博客
            久久久这里有精品中文字幕| 亚洲伊人久久精品影院| 无码久久精品国产亚洲Av影片| 88久久精品无码一区二区毛片| 精品人妻伦九区久久AAA片69| 精品久久久久久无码人妻热| 久久久久女人精品毛片| 久久精品一本到99热免费| 一本久久知道综合久久| 思思久久99热只有频精品66| 久久亚洲中文字幕精品一区四| 久久国产精品-国产精品| 久久99热精品| 亚洲一本综合久久| 99久久亚洲综合精品网站| 91秦先生久久久久久久| 国产AⅤ精品一区二区三区久久| 久久香综合精品久久伊人| 久久久久亚洲AV无码网站| 91精品国产色综合久久| AV无码久久久久不卡蜜桃| 狠狠色丁香婷婷久久综合不卡 | 午夜肉伦伦影院久久精品免费看国产一区二区三区 | 国产一区二区精品久久凹凸| 激情五月综合综合久久69| 四虎亚洲国产成人久久精品| 久久亚洲精品国产亚洲老地址| 久久久这里有精品| 熟妇人妻久久中文字幕| 97久久天天综合色天天综合色hd | 久久九九久精品国产免费直播| 亚洲国产另类久久久精品黑人 | 久久综合九色综合网站| 嫩草影院久久国产精品| 久久无码精品一区二区三区| 亚洲日韩中文无码久久| www.久久热.com| 亚洲人成无码网站久久99热国产| 伊人久久综合精品无码AV专区| 91精品国产91久久综合| 大香伊人久久精品一区二区|