• <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 小烏龜 閱讀(962) 評論(0)  編輯 收藏 引用 所屬分類: C&C++
            欧美精品福利视频一区二区三区久久久精品 | 国产成人久久AV免费| 亚洲精品tv久久久久久久久| 亚洲精品乱码久久久久久按摩| 久久国产精品无码HDAV| 日韩精品久久久久久| 久久亚洲中文字幕精品一区四| 久久久久久曰本AV免费免费| 97久久国产亚洲精品超碰热| 国产精品免费看久久久香蕉| 少妇高潮惨叫久久久久久| 国内精品久久久久影院网站 | 99国产精品久久| 老司机午夜网站国内精品久久久久久久久| 久久这里只精品99re66| 99久久er这里只有精品18| 久久国产AVJUST麻豆| 久久se这里只有精品| 国产成人久久精品一区二区三区| 四虎国产精品成人免费久久| 久久国产精品-久久精品| 国产精品99久久久精品无码| 久久最新免费视频| 久久久久黑人强伦姧人妻| 88久久精品无码一区二区毛片| 少妇高潮惨叫久久久久久| 欧美日韩精品久久免费| 日本精品一区二区久久久| 国内精品久久久久久久影视麻豆| av国内精品久久久久影院| 久久国产亚洲高清观看| 无码精品久久久久久人妻中字| 99久久免费国产精品特黄| 一极黄色视频久久网站| 久久人人爽人人澡人人高潮AV | 国产一区二区精品久久岳| 国产精品久久久久天天影视| 国产成人综合久久综合| 狠狠色丁香久久综合五月| 丁香狠狠色婷婷久久综合| 成人国内精品久久久久一区|