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("pause");
return 0;
}
//========================================
运行结果为
I like C++,I like the world.
Hello World.
Hi.....
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";
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());
}
return 0;
}
结果分别是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_sec-sTime.tv_sec)*1000000+(eTime.tv_usec-sTime.tv_usec); //exeTime 单位是微秒
gettimeofday(&sTime, NULL);
for(int i=0; i<OUT_IN_REPEATE_NUM; i++)
{
append="";
appendTest(append);
}
gettimeofday(&eTime, NULL);
long AppendTime = (eTime.tv_sec-sTime.tv_sec)*1000000+(eTime.tv_usec-sTime.tv_usec); //exeTime 单位是微秒
gettimeofday(&sTime, NULL);
for(int i=0; i<OUT_IN_REPEATE_NUM; i++)
{
ss="";
ssTest(ss);
}
gettimeofday(&eTime, NULL);
long SsTime = (eTime.tv_sec-sTime.tv_sec)*1000000+(eTime.tv_usec-sTime.tv_usec); //exeTime 单位是微秒
gettimeofday(&sTime, NULL);
for(int i=0; i<OUT_IN_REPEATE_NUM; i++)
{
plus="";
plusTest(plus);
}
gettimeofday(&eTime, NULL);
long PlusTime = (eTime.tv_sec-sTime.tv_sec)*1000000+(eTime.tv_usec-sTime.tv_usec); //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;
}
else
{
cout<<"Different!"<<endl;
cout<<"Sprintf: "<<sprintf<<endl;
cout<<"ss: "<<ss<<endl;
cout<<"Plus: "<<plus<<endl;
cout<<"Append:"<<append<<endl;
}
}
原文链接: