我的代码返回零,而不是实际计算数字中实际数字的实例数。我对自己的错误感到困惑。
int number_times_appear(int digit, int number)
{
int total = 0;
string stringNumber = to_string(number);
char charDigit = digit;
total = count(stringNumber.begin(), stringNumber.end(), charDigit);
cout << total;
return total;
}
答案 0 :(得分:4)
你应该做的转换错误
item.replace(/&trade/g,'™')
item.replace(/®/g,'®')
有关详细说明,请参阅this post
我还创建了 Ideone 代码段here
答案 1 :(得分:1)
char charDigit = digit;
没有做你认为它做的事情。这将获取digit
中的值并将其转换为基于您的字符集的字符(例如,对于美国ASCII,this table)。例如,digit
的{{1}}实际上可以在您的字符串中搜索标签!
您想要9
,因为要再次使用ASCII示例,char charDigit = '0'+digit;
根据链接表评估为48,数字'0'
- '1'
按照49-57进行操作。