????????往string字符串里插入单个字符时,有多种实现方式,+=、append()、push_back(),这些方式适用于不同的插入场景。
适用的插入场景
Append | += | push_back | |
整个字符串 | Ok | Ok | No |
字符串子串 | Ok | No | No |
字符数组 | Ok | Ok | No |
单个字符 | Ok | Ok | Ok |
迭代器 | Ok | No | No |
C string | Ok | Ok | No |
+=:OK; append:OK
//full-string
void fullStringAppend(string str1, string str2)
{
string str;
//append
str.append(str1);
//+=
str += str2;
return;
}
append:ok
//part-string
void partStringAppend(string str1)
{
string str;
str.append(str1, 0, 3);
return;
}
append:ok +=:ok
//character-array
void CharArrayAppend(string str1, string str2)
{
char ch[10] = {'h', 'e', 'l', 'l', 'o', '\0'};
str1.append(ch);
str2 += ch;
return;
}
append:ok +=:ok push_back:ok
//signal-character
void signalAppend(string str1, string str2, string str3)
{
str1.append("c"); //append方式不支持字符,必须转成单字符内容的string
str2 += 'c'; //+=支持单字符或者单字符内容的string
str3.push_back('c'); //push_back方式不支持单字符的string,必须为单字符形式
}
append:ok
//iterator-range
void iterRangeAppend(string str1)
{
string str;
str.append(str1.begin(), str1.begin() + 2);
return;
}
append:ok +=:ok
//CSting
void CstringAppend(string str1, string str2)
{
char *chstr = "hello";
str1.append(chstr);
str2 += chstr;
return;
}
?????????对比可以发现,append方式支持比较灵活,具体源码可以查看gcc或者msvc来查阅。这里针对三种方式都支持的单个字符的插入进行性能比较。分别在linux和windows下进行测试,为保证不同环境测试现象一致,且不同插入方式时间效率差距能明显比较, 测试插入字符次数为100W次,次数少差距不太明显。
测试代码如下:
#include <iostream>
#include <string>
#include <chrono>
using namespace std;
int main()
{
string str;
auto current_time = std::chrono::system_clock::now();
auto timestamp = std::chrono::duration_cast<std::chrono::milliseconds>(current_time.time_since_epoch()).count();
cout << "for += begin timestamp: " << timestamp <<endl;
for(int i = 0; i < 10000000; i++)
{
str += '1';
}
current_time = std::chrono::system_clock::now();
timestamp = std::chrono::duration_cast<std::chrono::milliseconds>(current_time.time_since_epoch()).count();
cout << "for += end timestamp: " << timestamp <<endl;
string str1;
current_time = std::chrono::system_clock::now();
timestamp = std::chrono::duration_cast<std::chrono::milliseconds>(current_time.time_since_epoch()).count();
cout << "for append begin timestamp: " << timestamp <<endl;
for(int j = 0; j < 10000000; j++)
{
str1.append("1");
}
current_time = std::chrono::system_clock::now();
timestamp = std::chrono::duration_cast<std::chrono::milliseconds>(current_time.time_since_epoch()).count();
cout << "for append end timestamp: " << timestamp <<endl;
string str2;
current_time = std::chrono::system_clock::now();
timestamp = std::chrono::duration_cast<std::chrono::milliseconds>(current_time.time_since_epoch()).count();
cout << "for push_back begin timestamp: " << timestamp <<endl;
for(int z = 0; z < 10000000; z++)
{
str2.push_back('1');
}
current_time = std::chrono::system_clock::now();
timestamp = std::chrono::duration_cast<std::chrono::milliseconds>(current_time.time_since_epoch()).count();
cout << "for push_back end timestamp: " << timestamp <<endl;
return 0;
}
Windows:
+=耗时:2316
append耗时:2658
push_back耗时:1752
Linux:
+=耗时:63
append()耗时:100
push_back()耗时:57
由此可见,两个平台的耗时现象一致,append > += > push_back
时间复杂度:
+=:O(n)
append:O(n)
push_back:O(1)
如果针对某个字符串进行频繁的单字符插入时,采用push_back方式最优。