欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

C++ string类型的几个典型应用

程序员文章站 2022-05-02 23:33:30
工程中用到了不少string的操作,总结了一下,分享学习成果。 1.替换函数,将str中的old_value替换为new_value string& replace_all_distin...

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

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

string& replace_all_distinct(string& str, const string& old_value, const string& new_value)
{
	for (string::size_type pos(0); pos != string::npos; pos += new_value.length()) {
		if ((pos = str.find(old_value, pos)) != string::npos)
			str.replace(pos, old_value.length(), new_value);
		else break;
	}
	return str;
}

测试过程:

	string str = "abc_m_dde_xxf_**plot_n_pq_df_cx-*9900))";
	cout << str << endl;
	string str2 = replace_all_distinct(str, "*", "");
	cout << str2 << endl;

输出结果:

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 << str << endl;
vector s_vector = split(str, "_");
for (int i = 0; i < s_vector.size(); i++)
{
	cout << tolower(s_vector[i]) << endl;
}

测试结果:

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 < s_vector.size(); i++)
	{	
		cout << (s_vector[i]) << endl;
		cout << toupper(s_vector[i]) << endl;
	}

测试结果:

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) < srcstr.length())
	{
		return true;
	}
	else
	{
		return false;
	}
}

测试过程:

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

输出:

1

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

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

测试过程:

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

输出:

2.544

6.获取随机字符串

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

	if (length>0)
	{
		char* szstr = new char[length + 1];
		szstr[length] = '\0';
		//srand((unsigned)gettickcount());
		int irand = 0;
		for (int i = 0; i < length; ++i)
		{
			irand = rand() % len;
			szstr[i] = g_arrcharelem[irand];
		}
		string result = szstr;
		delete[] szstr;
		return result;
	}
	return "";
}

测试过程:

	string ss2 = getrandomstring(9);
	cout << ss2 << endl;

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

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


	vector retultvector;
	if (length>0)
	{
		//srand((unsigned)gettickcount());
		for (int i = 0; i

测试:

	vector s2_vector = getrandomstringvector(5, 5);
	for (int i = 0; i < s2_vector.size(); i++)
	{
		cout << s2_vector[i] << endl;
	}

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 < p_vector.size();i++)
	{
		cout << p_vector[i].first <<" "<

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 < p_vector2.size(); i++)
	{
		cout << p_vector2[i].first << " " << p_vector2[i].second << endl;
	}

输出:

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=" << min << " " << "max=" << max << endl;
[i]>();>
,>,>
;>
;>