c++判断字符串是否包含指定字符串/判断字符串是否相等/保留小数点后几位

判断字符串是否包含指定字符串

//判断第一个参数 是否 包含 第二个参数  1:包含   0:不包含
bool is_contain(std::string input_str, std::string input_find_str)
{
    
    
	const char* s = input_str.c_str();
	int count = input_find_str.size();
	const char* s1 = input_find_str.c_str();
	int i, n = 0;
	for (int i = 0; i < input_str.size(); i++)
		if (s[i] == s1[0])
		{
    
    
			int del = 0;
			for (int j = 1; j < count; j++)
			{
    
    
				if (s[i + j] == s1[j])
				{
    
    
					del++;
				}
			}
			if (del == count - 1)
			{
    
    
				n++;
				break;
			}
		}
	return n;
}

判断字符串是否相等

//判断两个字符串相等
bool is_equal(std::string input_str1, std::string input_str2)
{
    
    
	const char* s1 = input_str1.c_str();
	const char* s2 = input_str2.c_str();
	int count1 = input_str1.size();
	int count2 = input_str2.size();
	int i, n = 0;
	if (count1 == count2)
	{
    
    
		for (int i = 0; i < count1; i++)
		{
    
    
			if (s1[i] != s2[i])
			{
    
    
				n = 0;
				break;
			}
			n = 1;
		}
	}
	return n;
}

保留小数点后几位

//保留几位小数
std::string save_double_Several(double d, int num)
{
    
    
	char char_s[20];
	string str = "%." + to_string(num) + "f";
	sprintf(char_s, str.c_str(), d);
	str = char_s;
	return str;
}
std::cout<<save_double_Several(3.1415926,2)<<std::endl;
double out = atof(save_double_Several(3.1415926,2));
std::cout<<out<<std::endl;

猜你喜欢

转载自blog.csdn.net/qq_41648925/article/details/127569595