• <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++博客 :: 首頁 :: 新隨筆 :: 聯系 :: 聚合  :: 管理 ::


            看一段簡單的代碼:

            int main()

                
            string s;
                s 
            += 'a';    
                s 
            += "bc"
                cout 
            << "s: " << s << endl;
                    
                cout 
            << endl;
                system(
            "Pause");
                
            return 0;
            }

            對不?當然對的!參看basic_string.h中的源碼:

                  basic_string&
                  
            operator+=(const basic_string& __str) { return this->append(__str); }

                  
            /**
                   *  @brief  Append a C string.
                   *  @param s  The C string to append.
                   *  @return  Reference to this string.
                   
            */
                  basic_string
            &
                  
            operator+=(const _CharT* __s) { return this->append(__s); }

                  
            /**
                   *  @brief  Append a character.
                   *  @param s  The character to append.
                   *  @return  Reference to this string.
                   
            */
                  basic_string
            &
                  
            operator+=(_CharT __c) { return this->append(size_type(1), __c); }

            下面的代碼對不?

                string s1;    
                s1 
            = 'a';    // = 也定義了右邊的值為字符的情況 
                cout << "s1: " << s1 << endl;
                s1 
            = "bc";
                cout 
            << "s1: " << s1 << endl;

            看源碼!

                  basic_string&
                  
            operator=(const basic_string& __str) 
                  { 
                
            this->assign(__str); 
                
            return *this;
                  }

                  
            /**
                   *  @brief  Copy contents of @a s into this string.
                   *  @param  s  Source null-terminated string.
                   
            */
                  basic_string
            &
                  
            operator=(const _CharT* __s) 
                  { 
                
            this->assign(__s); 
                
            return *this;
                  }

                  
            /**
                   *  @brief  Set value to string of length 1.
                   *  @param  c  Source character.
                   *
                   *  Assigning to a character makes this string length 1 and
                   *  (*this)[0] == @a c.
                   
            */
                  basic_string
            &
                  
            operator=(_CharT __c) 
                  { 
                
            this->assign(1, __c); 
                
            return *this;
                  }

            下面的呢

                string s2("u");
                s2 
            = 'j' + s2;        
                     cout 
            << "s2: " << s2 << endl;    
                s2 
            = s2 + 'n';
                cout 
            << "s2: " << s2 << endl;
                s2 
            = "jin " + s2;
                cout 
            << "s2: " << s2 << endl;
                s2 
            = s2 + " xia";
                cout 
            << "s2: " << s2 << endl;

            還是源碼!

            // operator+
              /**
               *  @brief  Concatenate two strings.
               *  @param lhs  First string.
               *  @param rhs  Last string.
               *  @return  New string with value of @a lhs followed by @a rhs.
               
            */
              template
            <typename _CharT, typename _Traits, typename _Alloc>
                basic_string
            <_CharT, _Traits, _Alloc>
                
            operator+(const basic_string<_CharT, _Traits, _Alloc>& __lhs,
                      
            const basic_string<_CharT, _Traits, _Alloc>& __rhs)
                {
                  basic_string
            <_CharT, _Traits, _Alloc> __str(__lhs);
                  __str.append(__rhs);
                  
            return __str;
                }

              
            /**
               *  @brief  Concatenate C string and string.
               *  @param lhs  First string.
               *  @param rhs  Last string.
               *  @return  New string with value of @a lhs followed by @a rhs.
               
            */
              template
            <typename _CharT, typename _Traits, typename _Alloc>
                basic_string
            <_CharT,_Traits,_Alloc>
                
            operator+(const _CharT* __lhs,
                      
            const basic_string<_CharT,_Traits,_Alloc>& __rhs);

              
            /**
               *  @brief  Concatenate character and string.
               *  @param lhs  First string.
               *  @param rhs  Last string.
               *  @return  New string with @a lhs followed by @a rhs.
               
            */
              template
            <typename _CharT, typename _Traits, typename _Alloc>
                basic_string
            <_CharT,_Traits,_Alloc>
                
            operator+(_CharT __lhs, const basic_string<_CharT,_Traits,_Alloc>& __rhs);

              
            /**
               *  @brief  Concatenate string and C string.
               *  @param lhs  First string.
               *  @param rhs  Last string.
               *  @return  New string with @a lhs followed by @a rhs.
               
            */
              template
            <typename _CharT, typename _Traits, typename _Alloc>
                inline basic_string
            <_CharT, _Traits, _Alloc>
                
            operator+(const basic_string<_CharT, _Traits, _Alloc>& __lhs,
                     
            const _CharT* __rhs)
                {
                  basic_string
            <_CharT, _Traits, _Alloc> __str(__lhs);
                  __str.append(__rhs);
                  
            return __str;
                }

              
            /**
               *  @brief  Concatenate string and character.
               *  @param lhs  First string.
               *  @param rhs  Last string.
               *  @return  New string with @a lhs followed by @a rhs.
               
            */
              template
            <typename _CharT, typename _Traits, typename _Alloc>
                inline basic_string
            <_CharT, _Traits, _Alloc>
                
            operator+(const basic_string<_CharT, _Traits, _Alloc>& __lhs, _CharT __rhs)
                {
                  typedef basic_string
            <_CharT, _Traits, _Alloc>    __string_type;
                  typedef typename __string_type::size_type        __size_type;
                  __string_type __str(__lhs);
                  __str.append(__size_type(
            1), __rhs);
                  
            return __str;
                }

            也許,最好的資料就是看看頭文件或者源碼!


            posted on 2008-11-23 16:14 小烏龜 閱讀(957) 評論(0)  編輯 收藏 引用 所屬分類: C&C++
            久久久人妻精品无码一区 | 色婷婷久久久SWAG精品| 久久亚洲AV成人出白浆无码国产| 久久影视综合亚洲| 日本亚洲色大成网站WWW久久| 久久国产成人午夜aⅴ影院| 久久综合综合久久狠狠狠97色88| 久久精品国产亚洲一区二区| 国产精品久久久天天影视| 99re久久精品国产首页2020| 久久99亚洲网美利坚合众国| 久久精品国产亚洲AV高清热| 国产V综合V亚洲欧美久久 | 香蕉久久av一区二区三区| 香蕉久久夜色精品国产尤物| 亚洲欧美久久久久9999| 久久久久亚洲av成人无码电影 | 国产91久久精品一区二区| 999久久久免费精品国产| 亚洲综合久久综合激情久久| 久久久久国产一区二区三区| 一本久久综合亚洲鲁鲁五月天| 久久丫忘忧草产品| 精品无码久久久久久午夜| 91久久香蕉国产熟女线看| 亚洲国产成人精品久久久国产成人一区二区三区综 | 国产精品久久久久久一区二区三区| 无码AV中文字幕久久专区| 一本色道久久88加勒比—综合| 久久最新免费视频| 久久99精品久久久久婷婷| 久久精品国产72国产精福利| 精品久久人人爽天天玩人人妻| 国产欧美久久一区二区| 中文字幕无码久久精品青草| 国产精品美女久久久m| 久久91精品国产91| 99久久www免费人成精品| 囯产精品久久久久久久久蜜桃| 91久久香蕉国产熟女线看| 久久久久久亚洲AV无码专区|