In practice, the string series in c ++ -- string connection (+ = or append or push_back)

Source: Internet
Author: User

In practice, the string series in c ++ -- string connection (+ = or append or push_back)

String connections are also frequently used. string reloads some operators:
First, let's take a look at the overload + operator, which is used to concatenate two string objects:
Source code:

template
  
      basic_string
   
     operator+(      const basic_string
    
     & _Left,      const basic_string
     
      & _Right   );template
      
        basic_string
       
         operator+( const basic_string
        
         & _Left, const CharType* _Right );template
         
           basic_string
          
            operator+( const basic_string
           
            & _Left, const CharType _Right );template
            
              basic_string
             
               operator+( const CharType* _Left, const basic_string
              
               & _Right );template
               
                 basic_string
                
                  operator+( const CharType _Left, const basic_string
                 
                  & _Right );template
                  
                    basic_string
                   
                    && operator+( const basic_string
                    
                     & _Left, const basic_string
                     
                      && _Right);template
                      
                        basic_string
                       
                        && operator+( const basic_string
                        
                         && _Left, const basic_string
                         
                          & _Right);template
                          
                            basic_string
                           
                            && operator+( const basic_string
                            
                             && _Left, const basic_string
                             
                              && _Right);template
                              
                                basic_string
                               
                                && operator+( const basic_string
                                
                                 && _Left, const CharType *_Right);template
                                 
                                   basic_string
                                  
                                   && operator+( const basic_string
                                   
                                    && _Left, CharType _Right);template
                                    
                                      basic_string
                                     
                                      && operator+( const CharType *_Left, const basic_string
                                      
                                       && _Right);template
                                       
                                         basic_string
                                        
                                         && operator+( CharType _Left, const basic_string
                                         
                                          && _Rig
                                         
                                        
                                       
                                      
                                     
                                    
                                   
                                  
                                 
                                
                               
                              
                             
                            
                           
                          
                         
                        
                       
                      
                     
                    
                   
                  
                 
                
               
              
             
            
           
          
         
        
       
      
     
    
   
  

Note:

# Include
  
   
# Include
   
    
Int main () {std: string my_str = "holiday"; std: string my_str_add = "error" + "error"; // error std :: string my_str_add2 = my_str + "right"; std: string my_str_add3 = my_str + "right" + "right"; std: string my_str_add4 = "right" + my_str; std :: string my_str_add5 = "error" + "error" + my_str; // return 0 ;}
   
  

Start with the question below!
+ =
* Append a character to a string *

basic_string
  
   & operator+=(   value_type _Ch);basic_string
   
    & operator+=(   const value_type* _Ptr);basic_string
    
     & operator+=(   const basic_string
     
      & _Right);
     
    
   
  

Append
* Add the character to the end of the string *

basic_string
  
   & append(    const value_type* _Ptr);basic_string
   
    & append(    const value_type* _Ptr,    size_type _Count);basic_string
    
     & append(    const basic_string
     
      & _Str,    size_type _Off,    size_type _Count);basic_string
      
       & append( const basic_string
       
        & _Str);basic_string
        
         & append( size_type _Count, value_type _Ch);template
         
           basic_string
          
           & append( InputIterator _First, InputIterator _Last );basic_string
           
            & append( const_pointer _First, const_pointer _Last);basic_string
            
             & append( const_iterator _First, const_iterator _Last);
            
           
          
         
        
       
      
     
    
   
  

There are multiple overload functions, so there are multiple usage methods:

   string str1a ( "Hello " );   const char *cstr1a = "Out There ";   str1a.append ( cstr1a );   string str1b ( "Hello " );   const char *cstr1b = "Out There ";   str1b.append ( cstr1b , 3 );   string str1c ( "Hello " ), str2c ( "Wide World " );      str1c.append ( str2c , 5 , 5 );   string str1d ( "Hello " ), str2d ( "Wide " ), str3d ( "World " );   str1d.append ( str2d );   str1d += str3d;   string str1e ( "Hello " );   str1e.append ( 4 , '!' );   string str1f ( "Hello " ), str2f ( "Wide World " );   str1f.append ( str2f.begin ( ) + 5 , str2f.end ( ) - 1 );

Push_back
* Add the element to the end of the string *

void push_back(    value_type _Ch);

Note that the following code is incorrect:

My_str.push_back ("123"); // error my_str.push_back ('1'); // OK

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.