C++中string的拼接

字符拼接可以采用的方法:

1、多个字串拼接时用+操作符

1)

代码:

如果不加红色部分的代码,则需要采用_sntprintf代替sntprintf。

#include <iostream>
#include <string>
<span style="background-color: rgb(255, 255, 255);"><span style="color:#FF0000;">#if _MSC_VER
#define snprintf _snprintf
#endif</span></span>
using namespace std;

string intToString(int v)
{
	char buf[32] = {0};
	snprintf(buf, sizeof(buf), "%u", v);

	string str = buf;
	return str;
}
int main()
{
	string data;
	int myid=7;
	string data1=intToString(myid) ;
	string data2;
	data = "{\"status\":200, \"id\":\"" +intToString(myid) + "\"}";
	//为实现字符的相加而实现拼接,必须#include string,否则string的运算符操作无法使用。不包含该头文件下,string是可以定义使用的。这是运算操作上面不行。
	cout<<data.c_str()<<endl;
	
	return 0;
}

2)引申使用str += "a", str =str+ "a" 效率差距:

str =str+ "a"加的运算产生的是一个新的对象,再把结果返回,而str += "a" 涉及到的应该是对象的引用,操作之后直接返回引用,避免了产生新的对象。因此,两者的性能有一定的差距。

int main()
{
	static int num = 1000000;  
	time_t timeBegin, timeEnd;  
	timeBegin = time(NULL);  
	
	
	string str = "";  
	for(int i =0; i<num; i++)
	{  
		
		//      str = "";         //多一条,时间花费一些  
		str =str + "a";  
	}
	timeEnd = time(NULL);  
	cout<<"str=str +a所耗费的时间:"<<timeEnd - timeBegin<<" ms"<<endl;  

	//num = 100W ,使用str += "a"表达, 花费18ms  
	timeBegin = time(NULL);  
	string str1 = "";  
	for(int i =0; i<num; i++)
	{  
		str1 += "a";  
	}
	timeEnd = time(NULL);  
	cout<<"str+=a所耗费的时间:"<<timeEnd - timeBegin<<" ms"<<endl;  	
	return 0;
}
所耗费的时间差距如下图所示:差得真不是一丢丢。。。(此处用的是debug版本)

2、使用append。

        string s1 = "Hello ";
	string s2 = "World! ";
	string s3 = " China";
	string s4;
	s4.append(s1);
	cout<<s4.c_str()<<endl;
	s4.append(s2);
	cout<<s4.c_str()<<endl;
	s4.append(s3);
	cout<<s4.c_str()<<endl;
将其与str+=a进行对比:

#include <iostream>
#include <string>
#include <time.h>  

using namespace std;
//获得当前的系统时间,返回一个long类型的数据 

int main()
{
	static int num = 100000000;//这里的时间是上面的100倍
	time_t timeBegin, timeEnd;  
	
	timeBegin = time(NULL);  
	string str1 = "";  
	for(int i =0; i<num; i++)
	{  
		str1 += "a";  
	}
	timeEnd = time(NULL);  
	cout<<"str+=a 所耗费的时间:"<<timeEnd - timeBegin<<" ms"<<endl; 

	timeBegin = time(NULL);  
	string str2 = "";  
	for(int i =0; i<num; i++)
	{  
		str2.append("a");  
	}
	timeEnd = time(NULL);  
	cout<<"str.append(a)所耗费的时间:"<<timeEnd - timeBegin<<" ms"<<endl; 

	return 0;

}

总体运行效率差不多:



3、stringstream

结合这两种方法与上述方法进行对比:

#include <iostream>
#include <map>
#include <string>
#include <time.h>  
#include <sstream>
using namespace std;
//获得当前的系统时间,返回一个long类型的数据 

int main()
{
	static int num = 100000000;  
	time_t timeBegin, timeEnd;  
	
	timeBegin = time(NULL);  
	string str1 = "";  
	for(int i =0; i<num; i++)
	{  
		str1 += "a";  
	}
	timeEnd = time(NULL);  
	cout<<"str+=a 所耗费的时间:"<<timeEnd - timeBegin<<" ms"<<endl; 

	timeBegin = time(NULL);  
	string str2 = "";  
	for(int i =0; i<num; i++)
	{  
		str2.append("a");  
	}
	timeEnd = time(NULL);  
	cout<<"str.append(a)所耗费的时间:"<<timeEnd - timeBegin<<" ms"<<endl; 

	timeBegin = time(NULL);  
	string str3 = "";  
	stringstream ss;
	for(int i =0; i<num; i++)
	{  
		ss<<"a";
	}
	str3=ss.str();
	timeEnd = time(NULL);  
	cout<<"stringstream 方法所耗费的时间:"<<timeEnd - timeBegin<<" ms"<<endl; 

	return 0;
}

运行结果如下:可知stringstream方法是最快的!(这里的循环次数和上面是一样,对比运行时间也是可以看出)


4、sprintf进行字符的拼接

代码:

#include <iostream>
#include <map>
#include <string>
#include <time.h>  
#include <sstream>
using namespace std;
//获得当前的系统时间,返回一个long类型的数据 
static int num = 100000000;  


int main()
{
	
	time_t timeBegin, timeEnd;  

	timeBegin = time(NULL);  
	string str1 = "";  
	for(int i =0; i<num; i++)
	{  
		str1 += "a";  
	}
	timeEnd = time(NULL);  
	cout<<"str+=a 所耗费的时间:"<<timeEnd - timeBegin<<" ms"<<endl; 

	timeBegin = time(NULL);  
	string str2 = "";  
	for(int i =0; i<num; i++)
	{  
		str2.append("a");  
	}
	timeEnd = time(NULL);  
	cout<<"str.append(a)所耗费的时间:"<<timeEnd - timeBegin<<" ms"<<endl; 

	timeBegin = time(NULL);  
	string str3 = "";  
	stringstream ss;
	for(int i =0; i<num; i++)
	{  
		ss<<"a";
	}
	str3=ss.str();
	timeEnd = time(NULL);  
	cout<<"stringstream 方法所耗费的时间:"<<timeEnd - timeBegin<<" ms"<<endl; 

	timeBegin = time(NULL);  
	string s4 = "";
	//char tmp[5];//="abc";
	char* cp = new char [num];
	char *tt=cp;
	char *t1="a";
	size_t  strLength=sizeof(t1);
	for(int i=0; i<num; i++)
		{
			sprintf(cp,"%s",t1 );//t1所处的位置,必须是变量,不能是常理,如“a”这样的形式是不行的。
			//cout<<tt<<endl;
			cp++;
		}
	s4 = cp;
	timeEnd = time(NULL);  
	cout<<"sprintf 方法所耗费的时间:"<<timeEnd - timeBegin<<" ms"<<endl; 

	return 0;
}
运行结果如下:

从中可以知道,sprintf是目前这四者速度最快的。其次分别是stringstream、str.append和str+=a方法。

注意,sprintf是不安全的,该函数无法检查目的缓存区是否溢出,现在一般采用snprint对其进行替代使用。类似的函数还有gets,strcat和strcpy,建议分别用fgets,strncat和strncpy进行替代使用。



  • 32
    点赞
  • 95
    收藏
    觉得还不错? 一键收藏
  • 3
    评论
C++,可以使用+运算符或append()函数来拼接字符串。引用的代码展示了一些字符串拼接的示例。 使用+运算符: string str1 = "ls"; string str2 = "test.c"; string str3 = str1 + " /tmp/"; // str3的值为"ls /tmp/" string str5 = str1 + " append " + "haha"; // str5的值为"ls append haha" 使用append()函数: string tmp = str1 + " append"; string str5 = tmp.append("haha"); // str5的值为"ls append haha" 另外,引用提到了使用push_back()函数来拼接字符串,这是一种高效的方式。 所以,在C++,可以使用+运算符、append()函数或push_back()函数来进行字符串拼接。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* *2* [C++ String拼接](https://blog.csdn.net/u013105549/article/details/52854368)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v92^chatsearchT0_1"}}] [.reference_item style="max-width: 50%"] - *3* [C++ string类型字符串拼接](https://blog.csdn.net/neuzhangno/article/details/128686544)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v92^chatsearchT0_1"}}] [.reference_item style="max-width: 50%"] [ .reference_list ]

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值