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

工程中用到了不少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<std::string> split(std::string str, std::string pattern)
{
	std::string::size_type pos;
	vector<std::string> result;
	str += pattern;//扩展字符串以方便操作
	int size = str.size();

	for (int i = 0; i<size; i++)
	{
		pos = str.find(pattern, i);
		if (pos<size)
		{
			std::string s = str.substr(i, pos - i);
			result.push_back(s);
			i = pos + pattern.size() - 1;
		}
	}
	return result;
}

测试过程:

string str = "abc_M_DDE_XXf_**PLoT_n_pq_df_cx-*9900))";	
cout << str << endl;
vector<string> s_vector = split(str, "_");
for (int i = 0; i < s_vector.size(); i++)
{
	cout << 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<string>中。

3.转为大写

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

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

测试过程:

	vector<string> 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<string> 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<string> retultVector;
	if (length>0)
	{
		//srand((unsigned)GetTickCount());
		for (int i = 0; i<vectorSize; i++)
		{
			char* szStr = new char[length + 1];
			szStr[length] = '\0';
			int iRand = 0;
			for (int i = 0; i < length; ++i)
			{
				iRand = rand() % LEN;
				szStr[i] = g_arrCharElem[iRand];
			}
			string result = szStr;
			delete[] szStr;
			retultVector.push_back(result);
		}
	}
	return retultVector;
}

测试:

	vector<string> 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<pair<string, int>> distinctVectorDouble2PairVector(vector<double> inputVector)
{
	vector<pair<string, int>> resultVector;
	vector<string> tempVector;
	for (int i = 0; i<inputVector.size(); i++)
	{
		tempVector.push_back(getStringFromDouble(inputVector.at(i),1));
	}
	sort(tempVector.begin(), tempVector.end(), less<string>());
	string lastValue;
	for (int i = 0; i<tempVector.size(); i++)
	{
		if (i == 0)
			lastValue = tempVector.at(i);
		if (tempVector.at(i) == lastValue&&i != 0)
		{
			resultVector[resultVector.size() - 1].second++;

		}
		else
		{
			resultVector.push_back(pair<string, int>(tempVector.at(i), 1));
			lastValue = tempVector.at(i);
		}
	}
	return resultVector;
}

测试过程:

	vector<double> inputVector;
	inputVector.push_back(3);
	inputVector.push_back(4);
	inputVector.push_back(5);
	inputVector.push_back(6);
	inputVector.push_back(3);

	vector<pair<string, int>> p_vector = distinctVectorDouble2PairVector(inputVector);
	for (int i = 0; i < p_vector.size();i++)
	{
		cout << p_vector[i].first <<" "<<p_vector[i].second<< endl;
	}

9.类型为string时,统计每个的出现次数

vector<pair<string, int>> distinctVectorString2PairVector(vector<string> inputVector)
{
	vector<pair<string, int>> resultVector;
	vector<string> tempVector = inputVector;
	sort(tempVector.begin(), tempVector.end(), less<string>());
	string lastValue;

	for (int i = 0; i<tempVector.size(); i++)
	{
		if (i == 0)
			lastValue = tempVector.at(i);
		if (tempVector.at(i) == lastValue&&i != 0)
		{
			resultVector[resultVector.size() - 1].second++;

		}
		else
		{
			resultVector.push_back(pair<string, int>(tempVector.at(i), 1));
			lastValue = tempVector.at(i);
		}
	}
	return resultVector;
}

测试过程:

	vector<string> inputVector2;
	inputVector2.push_back("aa");
	inputVector2.push_back("ss");
	inputVector2.push_back("bb");
	inputVector2.push_back("aa");

	vector<pair<string, int>> 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<double> input, double& min, double& max)
{
	if (input.size() <= 0)
	{
		min = 9999;
		max = -9999;
	}
	for (int i = 0; i<input.size(); i++)
	{
		if (i == 0)
		{
			min = input[i];
			max = input[i];
		}
		else
		{
			min = min>input[i] ? input[i] : min;
			max = max<input[i] ? input[i] : max;
		}
	}
}

测试:

double min = 0, max = 0;
getMinMaxFromVector(inputVector, min, max);
cout << "min=" << min << " " << "max=" << max << endl;


几乎上了一天的课,早上机器学习,晚上遥感,下午做了回项目,回去睡觉了。代码重在积累,这几天把近期做过的项目好好总结一番,包括hdf影像的读写,pg数据库的入库等内容。

keep fighting!


猜你喜欢

转载自blog.csdn.net/baidu_31933141/article/details/80672224