热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

C/C++编程实践:深入解析C++中string类型的关键应用场景与技术要点

工程中用到了不少string的操作,总结了一下,分享学习成果。1.替换函数,将str中的old_value替换为new_valuestring&replace_

工程中用到了不少string的操作,总结了一下,分享学习成果。

1.替换函数,将str中的old_value替换为new_value

%ignore_pre_1%

测试过程:

  	string str = "abc_m_dde_xxf_**plot_n_pq_df_cx-*9900))";  	cout <

输出结果:

  abc_m_dde_xxf_**plot_n_pq_df_cx-*9900))  abc_m_dde_xxf_plot_n_pq_df_cx-9900))

将*替换为空,该函数只能一个一个的替换,如果有多个想要替换的对象,请多次调用函数吧。

2.字符串分割函数

  vector split(std::string str, std::string pattern)  {  	std::string::size_type pos;  	vector result;  	str += pattern;//扩展字符串以方便操作  	int size = str.size();    	for (int i = 0; i

测试过程:

string str = "abc_m_dde_xxf_**plot_n_pq_df_cx-*9900))";	  cout <

测试结果:

  abc_m_dde_xxf_**plot_n_pq_df_cx-*9900))  abc  m  dde  xxf  **plot  n  pq  df  cx-*9900))

该函数是替换str中的pattern,存在一个vector中。

3.转为大写

  string toupper(string s)  {  	transform(s.begin(), s.end(), s.begin(), ::toupper);  	return s;  }

注意:需要添加头文件#include

测试过程:

  	vector s_vector = split(str, "_");  	for (int i = 0; i 

测试结果:

  abc  abc  m  m  dde  dde  xxf  xxf  **plot  **plot  n  n  pq  pq  df  df  cx-*9900))  cx-*9900))

transform是algorithm自带函数。

4.转为小写

  string tolower(string s)  {  	transform(s.begin(), s.end(), s.begin(), ::tolower);  	return s;  }

5.判断字符串的包含关系

  bool contain(string srcstr, string containstr)  {  	if (srcstr.find(containstr) 

测试过程:

  bool isok = contain(str, "ab");  cout <

输出:

1

5.double转string,结果四舍五入

  string getstringfromdouble(double input, int savedigit)  {  	stringstream ss;  	ss <= 0)  	{  		ss.precision(savedigit);  	}  	ss <> output; ss.clear();  	return output;  }  

测试过程:

  string ss = getstringfromdouble(2.543621, 3);  cout <

输出:

2.544

6.获取随机字符串

  string getrandomstring(int length)  {  	const int len = 62; // 26 + 26 + 10  	char g_arrcharelem[len] = { &#39;0&#39;, &#39;1&#39;, &#39;2&#39;, &#39;3&#39;, &#39;4&#39;, &#39;5&#39;, &#39;6&#39;, &#39;7&#39;, &#39;8&#39;, &#39;9&#39;,  		&#39;a&#39;, &#39;b&#39;, &#39;c&#39;, &#39;d&#39;, &#39;e&#39;, &#39;f&#39;, &#39;g&#39;, &#39;h&#39;, &#39;i&#39;, &#39;j&#39;, &#39;k&#39;, &#39;l&#39;, &#39;m&#39;, &#39;n&#39;, &#39;o&#39;, &#39;p&#39;, &#39;q&#39;, &#39;r&#39;, &#39;s&#39;, &#39;t&#39;, &#39;u&#39;, &#39;v&#39;, &#39;w&#39;, &#39;x&#39;, &#39;y&#39;, &#39;z&#39;,  		&#39;a&#39;, &#39;b&#39;, &#39;c&#39;, &#39;d&#39;, &#39;e&#39;, &#39;f&#39;, &#39;g&#39;, &#39;h&#39;, &#39;i&#39;, &#39;j&#39;, &#39;k&#39;, &#39;l&#39;, &#39;m&#39;, &#39;n&#39;, &#39;o&#39;, &#39;p&#39;, &#39;q&#39;, &#39;r&#39;, &#39;s&#39;, &#39;t&#39;, &#39;u&#39;, &#39;v&#39;, &#39;w&#39;, &#39;x&#39;, &#39;y&#39;, &#39;z&#39; };    	if (length>0)  	{  		char* szstr = new char[length + 1];  		szstr[length] = &#39;&#39;;  		//srand((unsigned)gettickcount());  		int irand = 0;  		for (int i = 0; i 

测试过程:

  	string ss2 = getrandomstring(9);  	cout <

7.获取给定大小与数量的随机字符串,并且放到vector中

  vector getrandomstringvector(int vectorsize, int length)  {  	const int len = 62; // 26 + 26 + 10  	char g_arrcharelem[len] = { &#39;0&#39;, &#39;1&#39;, &#39;2&#39;, &#39;3&#39;, &#39;4&#39;, &#39;5&#39;, &#39;6&#39;, &#39;7&#39;, &#39;8&#39;, &#39;9&#39;,  		&#39;a&#39;, &#39;b&#39;, &#39;c&#39;, &#39;d&#39;, &#39;e&#39;, &#39;f&#39;, &#39;g&#39;, &#39;h&#39;, &#39;i&#39;, &#39;j&#39;, &#39;k&#39;, &#39;l&#39;, &#39;m&#39;, &#39;n&#39;, &#39;o&#39;, &#39;p&#39;, &#39;q&#39;, &#39;r&#39;, &#39;s&#39;, &#39;t&#39;, &#39;u&#39;, &#39;v&#39;, &#39;w&#39;, &#39;x&#39;, &#39;y&#39;, &#39;z&#39;,  		&#39;a&#39;, &#39;b&#39;, &#39;c&#39;, &#39;d&#39;, &#39;e&#39;, &#39;f&#39;, &#39;g&#39;, &#39;h&#39;, &#39;i&#39;, &#39;j&#39;, &#39;k&#39;, &#39;l&#39;, &#39;m&#39;, &#39;n&#39;, &#39;o&#39;, &#39;p&#39;, &#39;q&#39;, &#39;r&#39;, &#39;s&#39;, &#39;t&#39;, &#39;u&#39;, &#39;v&#39;, &#39;w&#39;, &#39;x&#39;, &#39;y&#39;, &#39;z&#39; };      	vector retultvector;  	if (length>0)  	{  		//srand((unsigned)gettickcount());  		for (int i = 0; i

测试:

	vector s2_vector = getrandomstringvector(5, 5);  	for (int i = 0; i 

8.去重一个vector,以map保存 key为vector内值,value为出现次数

  vector> distinctvectordouble2pairvector(vector inputvector)  {  	vector> resultvector;  	vector tempvector;  	for (int i = 0; i());  	string lastvalue;  	for (int i = 0; i(tempvector.at(i), 1));  			lastvalue = tempvector.at(i);  		}  	}  	return resultvector;  }

测试过程:

  	vector inputvector;  	inputvector.push_back(3);  	inputvector.push_back(4);  	inputvector.push_back(5);  	inputvector.push_back(6);  	inputvector.push_back(3);    	vector> p_vector = distinctvectordouble2pairvector(inputvector);  	for (int i = 0; i 9.类型为string时,统计每个的出现次数

vector> distinctvectorstring2pairvector(vector inputvector)  {  	vector> resultvector;  	vector tempvector = inputvector;  	sort(tempvector.begin(), tempvector.end(), less());  	string lastvalue;    	for (int i = 0; i(tempvector.at(i), 1));  			lastvalue = tempvector.at(i);  		}  	}  	return resultvector;  }

测试过程:

  	vector inputvector2;  	inputvector2.push_back("aa");  	inputvector2.push_back("ss");  	inputvector2.push_back("bb");  	inputvector2.push_back("aa");    	vector> p_vector2 = distinctvectorstring2pairvector(inputvector2);  	for (int i = 0; i 

输出:

  aa 2  bb 1  ss 1

10.vector中找极值

  void getminmaxfromvector(vector input, double& min, double& max)  {  	if (input.size() <= 0)  	{  		min = 9999;  		max = -9999;  	}  	for (int i = 0; iinput[i] ? input[i] : min;  			max = max

测试:

double min = 0, max = 0;  getminmaxfromvector(inputvector, min, max);  cout <<"min=" <
            var cpro_id = "u6885494";

        
        
    
推荐阅读
author-avatar
jiho_b
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有