2000字范文,分享全网优秀范文,学习好帮手!
2000字范文 > c++判断字符串是否包含指定字符串/判断字符串是否相等/保留小数点后几位

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

时间:2023-08-06 09:15:34

相关推荐

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;

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。