实战c++中的string系列--string的连接(+= or append or push_back)
来源:互联网 发布:淘宝开通花呗怎么收费 编辑:程序博客网 时间:2024/05/22 05:02
string的连接也是经常用到的,string重载了一些运算符:
首先看一看重载+运算符,用于串联两个字符串对象:
源码:
template<class CharType, class Traits, class Allocator> basic_string<CharType, Traits, Allocator> operator+( const basic_string<CharType, Traits, Allocator>& _Left, const basic_string<CharType, Traits, Allocator>& _Right );template<class CharType, class Traits, class Allocator> basic_string<CharType, Traits, Allocator> operator+( const basic_string<CharType, Traits, Allocator>& _Left, const CharType* _Right );template<class CharType, class Traits, class Allocator> basic_string<CharType, Traits, Allocator> operator+( const basic_string<CharType, Traits, Allocator>& _Left, const CharType _Right );template<class CharType, class Traits, class Allocator> basic_string<CharType, Traits, Allocator> operator+( const CharType* _Left, const basic_string<CharType, Traits, Allocator>& _Right );template<class CharType, class Traits, class Allocator> basic_string<CharType, Traits, Allocator> operator+( const CharType _Left, const basic_string<CharType, Traits, Allocator>& _Right );template<class CharType, class Traits, class Allocator> basic_string<CharType, Traits, Allocator>&& operator+( const basic_string<CharType, Traits, Allocator>& _Left, const basic_string<CharType, Traits, Allocator>&& _Right);template<class CharType, class Traits, class Allocator> basic_string<CharType, Traits, Allocator>&& operator+( const basic_string<CharType, Traits, Allocator>&& _Left, const basic_string<CharType, Traits, Allocator>& _Right);template<class CharType, class Traits, class Allocator> basic_string<CharType, Traits, Allocator>&& operator+( const basic_string<CharType, Traits, Allocator>&& _Left, const basic_string<CharType, Traits, Allocator>&& _Right);template<class CharType, class Traits, class Allocator> basic_string<CharType, Traits, Allocator>&& operator+( const basic_string<CharType, Traits, Allocator>&& _Left, const CharType *_Right);template<class CharType, class Traits, class Allocator> basic_string<CharType, Traits, Allocator>&& operator+( const basic_string<CharType, Traits, Allocator>&& _Left, CharType _Right);template<class CharType, class Traits, class Allocator> basic_string<CharType, Traits, Allocator>&& operator+( const CharType *_Left, const basic_string<CharType, Traits, Allocator>&& _Right);template<class CharType, class Traits, class Allocator> basic_string<CharType, Traits, Allocator>&& operator+( CharType _Left, const basic_string<CharType, Traits, Allocator>&& _Rig
所以使用时,注意事项:
#include<iostream>#include<string>int main(){ std::string my_str = "holiday"; std::string my_str_add = "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;}
下面开始正题!
+=
*将字符追加到字符串*
basic_string<CharType, Traits, Allocator>& operator+=( value_type _Ch);basic_string<CharType, Traits, Allocator>& operator+=( const value_type* _Ptr);basic_string<CharType, Traits, Allocator>& operator+=( const basic_string<CharType, Traits, Allocator>& _Right);
append
*添加字符为字符串的末尾*
basic_string<CharType, Traits, Allocator>& append( const value_type* _Ptr);basic_string<CharType, Traits, Allocator>& append( const value_type* _Ptr, size_type _Count);basic_string<CharType, Traits, Allocator>& append( const basic_string<CharType, Traits, Allocator>& _Str, size_type _Off, size_type _Count);basic_string<CharType, Traits, Allocator>& append( const basic_string<CharType, Traits, Allocator>& _Str);basic_string<CharType, Traits, Allocator>& append( size_type _Count, value_type _Ch);template<class InputIterator> basic_string<CharType, Traits, Allocator>& 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);
有多个重载函数,因此多种使用方法:
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
*将元素添加到该字符串的末尾*
void push_back( value_type _Ch);
这里需要注意的是,以下代码是错误的:
my_str.push_back("123");//错误my_str.push_back('1');//ok
1 0
- 实战c++中的string系列--string的连接(+= or append or push_back)
- 实战c++中的vector系列--assign() push_back() or insert()
- 实战c++中的string系列--string与char*、const char *的转换(data() or c_str())
- C++基础---string类的operator+=/append/push_back
- String in C or C++
- 实战c++中的string系列--string到LPCWSTR的转换
- String, StringBuffer or StringBuilder?
- string+append的用法!!!
- 实战c++中的string系列--string的分割、替换(类似string.split或是explode())
- TypeError: expected string or buffer的解决方法
- c++string类 append用法
- QList 中的 append 和 push_back
- 实战c++中的string系列--函数返回局部变量string(引用局部string,局部string的.c_str()函数)
- 实战c++中的string系列--CDuiString和string的转换(duilib中的cduistring)
- StringBuffer的append拼接和String的+连接效率对比
- StringBuffer的append拼接和String的+连接效率对比
- C++ string::push_back
- string::append
- 2个不错的介绍前端网站
- Unity3D协程介绍 以及 使用
- SIMOTION D415-2DP控制SIEMENS V70伺服
- 手动创建的数据库,engine恢复时提示修改最大连接数的解决方法
- RTOS 基础入门与UCOSIII移植
- 实战c++中的string系列--string的连接(+= or append or push_back)
- NULL 0 和 nullptr
- 跨浏览器添加事件+跨浏览器移除事件+获取浏览器目标对象+跨浏览器输入文本的光标自动跳转+跨浏览器输入验证+跨浏览器屏蔽
- uva 10163——Storage Keepers
- 交友的基本配置
- inputstream outputstream 区别
- 跨浏览器兼容选择部分文本+跨浏览器获取字符编码
- Android手机平板两不误,使用Fragment实现兼容手机和平板的程序
- 基于rails的schedule网站开发(11):记住我