STL中count函数统计string字符串中某个字符的个数报错:无法将参数 1 从“char”转换为“con

C++语言 码拜 7年前 (2017-04-19) 1435次浏览
#include <iostream>
#include <vector>
#include <string>
#include <algorithm>
using namespace std;
bool Mycomp(const string &s1, const string &s2)
{
int c1 = count(s1.begin(), s1.end(), “1”);
int c2 = count(s2.begin(), s2.end(), “1”);
return c1 != c2 ? c1 < c2 : c1 < c2;
}
int main()
{
vector<string> result;
string str;
while (cin >> str)
{
result.push_back(str);
}
stable_sort(str.begin(), str.end(),Mycomp);
for (vector<string>::iterator it = result.begin(); it != result.end();it++)
{
cout << *it << endl;
}
return 0;
}
解决方案

7

stable_sort(result.begin(), result.end(),Mycomp);

6

stable_sort()
这个函数调用错了,  你是想    stable_sort(result.begin(), result.end(),Mycomp);吧?

14

#include <iostream>
#include <vector>
#include <string>
#include <algorithm>
using namespace std;
bool Mycomp(const string &s1, const string &s2)
{
	int c1 = count(s1.begin(), s1.end(), "1");
	int c2 = count(s2.begin(), s2.end(), "1");
	return c1 != c2 ? c1 < c2 : c1 < c2;
}
int main()
{
	vector<string> result;
	string str;
	while (cin >> str)
	{
		result.push_back(str);
	}
	stable_sort(result.begin(), result.end(),Mycomp);
	for (vector<string>::iterator it = result.begin(); it != result.end();it++)
	{
		cout << *it << endl;
	}
	return 0;
} 

CodeBye 版权所有丨如未注明 , 均为原创丨本网站采用BY-NC-SA协议进行授权 , 转载请注明STL中count函数统计string字符串中某个字符的个数报错:无法将参数 1 从“char”转换为“con
喜欢 (0)
[1034331897@qq.com]
分享 (0)