为什么不g++即使使用 C++11 也能识别 stoi 吗?
我在 C++11 中有这个函数:
bool ccc(const string cc) {
vector<string> digits;
int aux;
for(int n = 0; n < cc.length(); ++n) {
digits.push_back(to_string(cc[n])); }
for(int s = 1; s < digits.size(); s += 2) {
aux = stoi(digits[s]);
aux *= 2;
digits[s] = to_string(aux);
aux = 0;
for(int f = 0; f < digits[s].length(); ++f) {
aux += stoi(digits[s][f]); }
digits[s] = to_string(aux);
aux = 0; }
for(int b = 0; b < digits.size(); ++b) {
aux += stoi(digits[b]); }
aux *= 9;
aux %= 10;
return (aux == 0); }
当使用带有 -std=c++11
标志的 g++ 进行编译时出现此错误:
crecarche.cpp: In function ‘bool ccc(std::string)’:
crecarche.cpp:18:12: error: no matching function for call to ‘stoi(__gnu_cxx::__alloc_traits<std::allocator<char>, char>::value_type&)’
18 | aux += stoi(digits[s][f]); }
| ~~~~^~~~~~~~~~~~~~
但我使用了 stoi
函数之后我没有收到该行的任何错误。
为什么编译器会向我抛出此错误以及如何修复它?
I have this function in C++11:
bool ccc(const string cc) {
vector<string> digits;
int aux;
for(int n = 0; n < cc.length(); ++n) {
digits.push_back(to_string(cc[n])); }
for(int s = 1; s < digits.size(); s += 2) {
aux = stoi(digits[s]);
aux *= 2;
digits[s] = to_string(aux);
aux = 0;
for(int f = 0; f < digits[s].length(); ++f) {
aux += stoi(digits[s][f]); }
digits[s] = to_string(aux);
aux = 0; }
for(int b = 0; b < digits.size(); ++b) {
aux += stoi(digits[b]); }
aux *= 9;
aux %= 10;
return (aux == 0); }
And I get this error when compiling with g++ with the -std=c++11
flag:
crecarche.cpp: In function ‘bool ccc(std::string)’:
crecarche.cpp:18:12: error: no matching function for call to ‘stoi(__gnu_cxx::__alloc_traits<std::allocator<char>, char>::value_type&)’
18 | aux += stoi(digits[s][f]); }
| ~~~~^~~~~~~~~~~~~~
But I used the stoi
function after and I did not get any error with that line.
Why is the compiler throwing me this error and how can I fix it?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
错误消息告诉您传递给
stoi
的参数的类型是char&
的一种奇特表达方式。发生这种情况是因为digits[s]
已经是string&
类型,进一步订阅它会给您一个char&
。我不清楚你想实现什么目标。也许您需要删除多余的下标,或者使用
digits[s][f] - '0'
来计算数字值。 C++ 要求十进制数字由后续代码点表示,因此即使在不基于 Unicode 的 ISO 646 子集的理论实现中也是如此。The error message is telling you that the argument you pass to
stoi
is of the typewhich is a fancy way of saying
char&
. This happens becausedigits[s]
is already of typestring&
, and subscribing it further gives you achar&
.It's not clear to me what you are trying to accomplish. Maybe you need to remove the extra subscript, or use
digits[s][f] - '0'
to compute the digit value. C++ requires that the decimal digits are represented by subsequent code points, so this works even in theoretical implementations which are not based on the ISO 646 subset of Unicode.