C++中stringappend函数的使⽤与字符串拼接1. append函数
常⽤的函数原型:
basic_string &append(const basic_string &str );
basic_string &append(const char*str );
basic_string &append(const basic_string &str, size_type index, size_type len );
basic_string &append(const char*str, size_type num );
basic_string &append( size_type num,char ch );
basic_string &append( input_iterator start, input_iterator end );
append() 函数可以完成以下⼯作:
在字符串的末尾添加str,
在字符串的末尾添加str的⼦串,⼦串以index索引开始,长度为len
在字符串的末尾添加str中的num个字符,
在字符串的末尾添加num个字符ch,
在字符串的末尾添加以迭代器start和end表⽰的字符序列.
append函数常⽤的三个功能:
直接添加另⼀个完整的字符串:如str1.append(str2);
添加另⼀个字符串的某⼀段⼦串:如str1.append(str2, 11, 7);
添加⼏个相同的字符:如str1.append(5, ‘.’);注意,个数在前字符在后.上⾯的代码意思为在str1后⾯添加5个"."
//========================================
#include<iostream>
using namespace std;
//========================================
int main()
{
string str1="I like C++";
string str2=",I like the world.";
string str3="Hello";
string str4("Hi");
//====================================
str1.append(str2);
str3.append(str2,11,7);
str4.append(5,'.');
//====================================
cout<<str1<<endl;
cout<<str3<<endl;
cout<<str4<<endl;
system("pau");
return0;
}
//========================================
运⾏结果为
I like C++,I like the world.
Hello World.
<
2. push_back函数
函数原型:
void push_back(
value_type _Ch
);
功能:将字符添加到字符串的末尾,注意是字符⽽不是字符串。相当于basic_string &append( size_type num, char ch );,其中num=1。
my_str.push_back("123");//错误
my_str.push_back('1');//ok
3. 使⽤+=拼接
重载操作符原型:
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 );
使⽤⽰例:
std::string my_str ="holiday";
std::string my_str_add ="error"+"error";//错误
std::string my_str_add2 = my_str +"right";
15个吊桶打水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;//错误
+=操作符和append函数有什么区别呢,可以看下⾯的⼀个例⼦:
#include<iostream>
#include<string>
using namespace std;
int main()
{
{
char szTest[]="\0abc";
string s;
s += szTest;
printf("size is %u\n", s.size());
}
{
char szTest[]="\0abc";
string s;
s.append(szTest,sizeof(szTest)-1);
printf("size is %u\n", s.size());
}
return0;
}
结果分别是0和4, 不⾔⽽喻, string就是⼀个容器, ⽤来装字符, 对任何字符透明。
4. 使⽤sprintf拼接
使⽤⽰例:家庭成员英文
#include<iostream>
#include<string>
#include<sys/time.h>
#include<sstream>
#include<stdio.h>
using namespace std;
#define OUT_IN_REPEATE_NUM 10000
#define IN_REPEATE_NUM 60
疏星
string s1="abcedfg";
string s2="hijklmn";
string s3="opqrst";
莫文蔚的经典歌曲void plusTest(string& ret)
{
for(int i=0; i<IN_REPEATE_NUM; i++)
{
ret += s1;
ret += s2;
ret += s3;雨天用英语怎么说>抗日英雄人物事迹
}
}
我的钱我做主
void appendTest(string& ret)
{
for(int i=0; i<IN_REPEATE_NUM; i++)
{
ret.append(s1);
ret.append(s2);
ret.append(s3);
}
}
//测试sprintf函数
void sprintfTest(string& ret)
{
const size_t length=26*IN_REPEATE_NUM;
char tmp[length];
char* cp = tmp;
size_t strLength=s1.length()+s2.length()+s3.length();
for(int i=0; i<IN_REPEATE_NUM; i++)
{
sprintf(cp,"%s%s%s", s1.c_str(), s2.c_str(),s3.c_str());
cp+=strLength;
}
ret = tmp;
}
void ssTest(string& ret)
{
stringstream ss;
for(int i=0; i<IN_REPEATE_NUM; i++)
{
ss<<s1;
ss<<s2;
ss<<s3;
}
ret = ss.str();
}
int main(){
string ss, plus, append, sprintf;
struct timeval sTime, eTime;
gettimeofday(&sTime,NULL);
for(int i=0; i<OUT_IN_REPEATE_NUM; i++)
{
sprintf="";
sprintfTest(sprintf);
}
gettimeofday(&eTime,NULL);
long SprintfTime =(eTime.tv_c-sTime.tv_c)*1000000+(eTime.tv_uc-sTime.tv_uc);//exeTime 单位是微秒
gettimeofday(&sTime,NULL);
for(int i=0; i<OUT_IN_REPEATE_NUM; i++)
{
append="";
appendTest(append);
appendTest(append);
}
gettimeofday(&eTime,NULL);
long AppendTime =(eTime.tv_c-sTime.tv_c)*1000000+(eTime.tv_uc-sTime.tv_uc);//exeTime 单位是微秒
gettimeofday(&sTime,NULL);
for(int i=0; i<OUT_IN_REPEATE_NUM; i++)
{
ss="";
ssTest(ss);
}
gettimeofday(&eTime,NULL);
long SsTime =(eTime.tv_c-sTime.tv_c)*1000000+(eTime.tv_uc-sTime.tv_uc);//exeTime 单位是微秒
gettimeofday(&sTime,NULL);
for(int i=0; i<OUT_IN_REPEATE_NUM; i++)
{
plus="";道路交通事故处理办法
plusTest(plus);
}
gettimeofday(&eTime,NULL);
long PlusTime =(eTime.tv_c-sTime.tv_c)*1000000+(eTime.tv_uc-sTime.tv_uc);//exeTime 单位是微秒
cout<<"PlusTime is : "<<PlusTime<<endl;
cout<<"AppendTime is : "<<AppendTime<<endl;
cout<<"SsTime is : "<<SsTime<<endl;
cout<<"SprintfTime is :"<<SprintfTime<<endl;
if(ss==sprintf && append==plus && ss==plus)
{
cout<<"They are same"<<endl;
}
el
{
cout<<"Different!"<<endl;
cout<<"Sprintf: "<<sprintf<<endl;
cout<<"ss: "<<ss<<endl;
cout<<"Plus: "<<plus<<endl;
cout<<"Append:"<<append<<endl;
}
}
原⽂链接:
1.
2.
3.
4 .