The connection to string is also frequently used, and string overloads a number of operators:
First look at the overloaded + operator, used to concatenate two string objects:
Source:
Template<classCharType,classTraits,classAllocator> Basic_string<chartype, Traits, allocator>operator+(ConstBasic_string<chartype, Traits, allocator>& _left,ConstBasic_string<chartype, Traits, allocator>& _right);Template<classCharType,classTraits,classAllocator> Basic_string<chartype, Traits, allocator>operator+(ConstBasic_string<chartype, Traits, allocator>& _left,Constchartype* _right);Template<classCharType,classTraits,classAllocator> Basic_string<chartype, Traits, allocator>operator+(ConstBasic_string<chartype, Traits, allocator>& _left,ConstCharType _right);Template<classCharType,classTraits,classAllocator> Basic_string<chartype, Traits, allocator>operator+(ConstChartype* _left,ConstBasic_string<chartype, Traits, allocator>& _right);Template<classCharType,classTraits,classAllocator> Basic_string<chartype, Traits, allocator>operator+(ConstCharType _left,ConstBasic_string<chartype, Traits, allocator>& _right);Template<classCharType,classTraits,classAllocator> Basic_string<chartype, Traits, allocator>&&operator+(ConstBasic_string<chartype, Traits, allocator>& _left,ConstBasic_string<chartype, Traits, allocator>&& _right);Template<classCharType,classTraits,classAllocator> Basic_string<chartype, Traits, allocator>&&operator+(ConstBasic_string<chartype, Traits, allocator>&& _left,ConstBasic_string<chartype, Traits, allocator>& _right);Template<classCharType,classTraits,classAllocator> Basic_string<chartype, Traits, allocator>&&operator+(ConstBasic_string<chartype, Traits, allocator>&& _left,ConstBasic_string<chartype, Traits, allocator>&& _right);Template<classCharType,classTraits,classAllocator> Basic_string<chartype, Traits, allocator>&&operator+(ConstBasic_string<chartype, Traits, allocator>&& _left,ConstCharType *_right);Template<classCharType,classTraits,classAllocator> Basic_string<chartype, Traits, allocator>&&operator+(ConstBasic_string<chartype, Traits, allocator>&& _left, CharType _right);Template<classCharType,classTraits,classAllocator> Basic_string<chartype, Traits, allocator>&&operator+(ConstCharType *_left,ConstBasic_string<chartype, Traits, allocator>&& _right);Template<classCharType,classTraits,classAllocator> Basic_string<chartype, Traits, allocator>&&operator+ (CharType _left,ConstBasic_string<chartype, Traits, allocator>&& _rig
So when using, the precautions:
#include <iostream>#include <string>intMain () {STD::stringMy_str ="Holiday";STD::stringMy_str_add ="Error"+"Error";//Error STD::stringMY_STR_ADD2 = My_str +"Right";STD::stringMY_STR_ADD3 = My_str +"Right"+"Right";STD::stringMY_STR_ADD4 ="Right"+ my_str;STD::stringMY_STR_ADD5 ="Error"+"Error"+ my_str;//Error return 0;}
Let's get down to the chase!
+=
* Append characters to the string *
operator+=( operator+=( constoperator+=( const basic_string<CharType, Traits, Allocator>& _Right);
Append
* Add character to end of String *
Basic_string<chartype, Traits, allocator>&Append(Constvalue_type* _ptr); Basic_string<chartype, Traits, allocator>&Append(Constvalue_type* _ptr, Size_type _count); Basic_string<chartype, Traits, allocator>&Append(ConstBasic_string<chartype, Traits, allocator>& _str, Size_type _off, Size_type _count); Basic_string<chartyp E, Traits, allocator>&Append(ConstBasic_string<chartype, Traits, allocator>& _str); Basic_string<chartype, Traits, allocator>&Append(Size_type _count, Value_type _ch); Template<class inputiterator> Basic_string<chartype, Traits, Alloca tor>&Append(Inputiterator _first, Inputiterator _last); Basic_string<chartype, Traits, allocator>&Append(Const_pointer _first, Const_pointer _last); Basic_string<chartype, Traits, allocator>&Append(Const_iterator _first, const_iterator _last);
There are several overloaded functions, so there are several ways to use them:
stringStr1a ("Hello");ConstChar *cstr1a ="Out There"; Str1a.Append(CSTR1A);stringSTR1B ("Hello");ConstChar *cstr1b ="Out There"; Str1b.Append(CSTR1B,3);stringSTR1C ("Hello"), STR2C ("Wide World"); Str1c.Append(STR2C,5,5);stringSTR1D ("Hello"), Str2d ("Wide"), Str3d ("World"); str1d.Append(STR2D); str1d + = Str3d;stringSTR1E ("Hello"); str1e.Append(4,'! ');stringSTR1F ("Hello"), str2f ("Wide World"); str1f.Append(Str2f.begin () +5, Str2f.end ()-1);
push_back
* Add an element to the end of the string *
void push_back( value_type _Ch);
It is important to note that the following code is wrong:
my_str.push_back("123");//错误my_str.push_back(‘1‘);//ok
Connection to the string series--string in combat C + + (+ = or append or push_back)