C++之string的拼接

来源:互联网 发布:网络 取代 书 编辑:程序博客网 时间:2024/04/28 16:10

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

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

1)

代码:

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

[cpp] view plain copy
  1. #include <iostream>  
  2. #include <string>  
  3. #define snprintf _snprintf  
  4. #endif
  5. using namespace std;  
  6.   
  7. string intToString(int v)  
  8. {  
  9.     char buf[32] = {0};  
  10.     snprintf(buf, sizeof(buf), "%u", v);  
  11.   
  12.     string str = buf;  
  13.     return str;  
  14. }  
  15. int main()  
  16. {  
  17.     string data;  
  18.     int myid=7;  
  19.     string data1=intToString(myid) ;  
  20.     string data2;  
  21.     data = "{\"status\":200, \"id\":\"" +intToString(myid) + "\"}";  
  22.     //为实现字符的相加而实现拼接,必须#include string,否则string的运算符操作无法使用。不包含该头文件下,string是可以定义使用的。这是运算操作上面不行。  
  23.     cout<<data.c_str()<<endl;  
  24.       
  25.     return 0;  
  26. }  

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

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

[cpp] view plain copy
  1. int main()  
  2. {  
  3.     static int num = 1000000;    
  4.     time_t timeBegin, timeEnd;    
  5.     timeBegin = time(NULL);    
  6.       
  7.       
  8.     string str = "";    
  9.     for(int i =0; i<num; i++)  
  10.     {    
  11.           
  12.         //      str = "";         //多一条,时间花费一些    
  13.         str =str + "a";    
  14.     }  
  15.     timeEnd = time(NULL);    
  16.     cout<<"str=str +a所耗费的时间:"<<timeEnd - timeBegin<<" ms"<<endl;    
  17.   
  18.     //num = 100W ,使用str += "a"表达, 花费18ms    
  19.     timeBegin = time(NULL);    
  20.     string str1 = "";    
  21.     for(int i =0; i<num; i++)  
  22.     {    
  23.         str1 += "a";    
  24.     }  
  25.     timeEnd = time(NULL);    
  26.     cout<<"str+=a所耗费的时间:"<<timeEnd - timeBegin<<" ms"<<endl;      
  27.     return 0;  
  28. }  
所耗费的时间差距如下图所示:差得真不是一丢丢。。。(此处用的是debug版本)

2、使用append。

[cpp] view plain copy
  1.        string s1 = "Hello ";  
  2. string s2 = "World! ";  
  3. string s3 = " China";  
  4. string s4;  
  5. s4.append(s1);  
  6. cout<<s4.c_str()<<endl;  
  7. s4.append(s2);  
  8. cout<<s4.c_str()<<endl;  
  9. s4.append(s3);  
  10. cout<<s4.c_str()<<endl;  
将其与str+=a进行对比:

[cpp] view plain copy
  1. #include <iostream>  
  2. #include <string>  
  3. #include <time.h>    
  4.   
  5. using namespace std;  
  6. //获得当前的系统时间,返回一个long类型的数据   
  7.   
  8. int main()  
  9. {  
  10.     static int num = 100000000;//这里的时间是上面的100倍  
  11.     time_t timeBegin, timeEnd;    
  12.       
  13.     timeBegin = time(NULL);    
  14.     string str1 = "";    
  15.     for(int i =0; i<num; i++)  
  16.     {    
  17.         str1 += "a";    
  18.     }  
  19.     timeEnd = time(NULL);    
  20.     cout<<"str+=a 所耗费的时间:"<<timeEnd - timeBegin<<" ms"<<endl;   
  21.   
  22.     timeBegin = time(NULL);    
  23.     string str2 = "";    
  24.     for(int i =0; i<num; i++)  
  25.     {    
  26.         str2.append("a");    
  27.     }  
  28.     timeEnd = time(NULL);    
  29.     cout<<"str.append(a)所耗费的时间:"<<timeEnd - timeBegin<<" ms"<<endl;   
  30.   
  31.     return 0;  
  32.   
  33. }  

总体运行效率差不多:



3、stringstream

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

[cpp] view plain copy
  1. #include <iostream>  
  2. #include <map>  
  3. #include <string>  
  4. #include <time.h>    
  5. #include <sstream>  
  6. using namespace std;  
  7. //获得当前的系统时间,返回一个long类型的数据   
  8.   
  9. int main()  
  10. {  
  11.     static int num = 100000000;    
  12.     time_t timeBegin, timeEnd;    
  13.       
  14.     timeBegin = time(NULL);    
  15.     string str1 = "";    
  16.     for(int i =0; i<num; i++)  
  17.     {    
  18.         str1 += "a";    
  19.     }  
  20.     timeEnd = time(NULL);    
  21.     cout<<"str+=a 所耗费的时间:"<<timeEnd - timeBegin<<" ms"<<endl;   
  22.   
  23.     timeBegin = time(NULL);    
  24.     string str2 = "";    
  25.     for(int i =0; i<num; i++)  
  26.     {    
  27.         str2.append("a");    
  28.     }  
  29.     timeEnd = time(NULL);    
  30.     cout<<"str.append(a)所耗费的时间:"<<timeEnd - timeBegin<<" ms"<<endl;   
  31.   
  32.     timeBegin = time(NULL);    
  33.     string str3 = "";    
  34.     stringstream ss;  
  35.     for(int i =0; i<num; i++)  
  36.     {    
  37.         ss<<"a";  
  38.     }  
  39.     str3=ss.str();  
  40.     timeEnd = time(NULL);    
  41.     cout<<"stringstream 方法所耗费的时间:"<<timeEnd - timeBegin<<" ms"<<endl;   
  42.   
  43.     return 0;  
  44. }  

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


4、sprintf进行字符的拼接

代码:

[cpp] view plain copy
  1. #include <iostream>  
  2. #include <map>  
  3. #include <string>  
  4. #include <time.h>    
  5. #include <sstream>  
  6. using namespace std;  
  7. //获得当前的系统时间,返回一个long类型的数据   
  8. static int num = 100000000;    
  9.   
  10.   
  11. int main()  
  12. {  
  13.       
  14.     time_t timeBegin, timeEnd;    
  15.   
  16.     timeBegin = time(NULL);    
  17.     string str1 = "";    
  18.     for(int i =0; i<num; i++)  
  19.     {    
  20.         str1 += "a";    
  21.     }  
  22.     timeEnd = time(NULL);    
  23.     cout<<"str+=a 所耗费的时间:"<<timeEnd - timeBegin<<" ms"<<endl;   
  24.   
  25.     timeBegin = time(NULL);    
  26.     string str2 = "";    
  27.     for(int i =0; i<num; i++)  
  28.     {    
  29.         str2.append("a");    
  30.     }  
  31.     timeEnd = time(NULL);    
  32.     cout<<"str.append(a)所耗费的时间:"<<timeEnd - timeBegin<<" ms"<<endl;   
  33.   
  34.     timeBegin = time(NULL);    
  35.     string str3 = "";    
  36.     stringstream ss;  
  37.     for(int i =0; i<num; i++)  
  38.     {    
  39.         ss<<"a";  
  40.     }  
  41.     str3=ss.str();  
  42.     timeEnd = time(NULL);    
  43.     cout<<"stringstream 方法所耗费的时间:"<<timeEnd - timeBegin<<" ms"<<endl;   
  44.   
  45.     timeBegin = time(NULL);    
  46.     string s4 = "";  
  47.     //char tmp[5];//="abc";  
  48.     char* cp = new char [num];  
  49.     char *tt=cp;  
  50.     char *t1="a";  
  51.     size_t  strLength=sizeof(t1);  
  52.     for(int i=0; i<num; i++)  
  53.         {  
  54.             sprintf(cp,"%s",t1 );//t1所处的位置,必须是变量,不能是常理,如“a”这样的形式是不行的。  
  55.             //cout<<tt<<endl;  
  56.             cp++;  
  57.         }  
  58.     s4 = cp;  
  59.     timeEnd = time(NULL);    
  60.     cout<<"sprintf 方法所耗费的时间:"<<timeEnd - timeBegin<<" ms"<<endl;   
  61.   
  62.     return 0;  
  63. }  
运行结果如下:

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

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

0 0
原创粉丝点击