输出结果:
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))
trans form 是algorith m自带函数。
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=" <
<>