频道栏目
首页 > 资讯 > C++ > 正文

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

18-06-13        来源:[db:作者]  
收藏   我要投稿

工程中用到了不少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;
相关TAG标签
上一篇:百度网站打不开了,只有百度首页跟搜索页打不开其它能正常打开怎么解决?
下一篇:如何获取伪装ip下的真实ip地址
相关文章
图文推荐

关于我们 | 联系我们 | 广告服务 | 投资合作 | 版权申明 | 在线帮助 | 网站地图 | 作品发布 | Vip技术培训 | 举报中心

版权所有: 红黑联盟--致力于做实用的IT技术学习网站