"stoi"和"to_string"不是"std"的成员

'stoi' and 'to_string' is not a member of 'std'

本文关键字:std 成员 不是 stoi to string      更新时间:2023-10-16

我有一些代码,它很大,所以我在这里创建一个快照:

int l = 3;
vector<int> weights;   
void changeWeights(int out){
    for (int i = 0; i < weights.size(); i++){
        int w = std::stoi(std::to_string(weights[i])) -
        out*std::stoi(std::to_string(weights[i]));
        if (w < -l){
          w = -l;
        } else if(w > l){
            w = l;
        }
        weights.assign(i, w);
    }
}

我在"stoi"和"to_string"函数调用中都收到错误,形式为

Main.cpp:35:21: error: ‘stoi’ is not a member of ‘std’
         int w = std::stoi(std::to_string(weights[i])) -
                 ^
Main.cpp:35:31: error: ‘to_string’ is not a member of ‘std’
         int w = std::stoi(std::to_string(weights[i])) -
                           ^
Main.cpp:36:17: error: ‘stoi’ is not a member of ‘std’
         out*std::stoi(std::to_string(weights[i]));
             ^
Main.cpp:36:27: error: ‘to_string’ is not a member of ‘std’
         out*std::stoi(std::to_string(weights[i]));

我读过一些类似的查询,答案是在编译时添加 -std=c++11 或 -std=c++0x - 这两种解决方案都不起作用。另一个解决方案建议编译器版本中存在错误,但我认为这不是我正在使用的编译器。我在 64x Apple Macbook Pro 上使用 g++ (GCC( 5.0.0 20141005(实验性(版本。

这部分代码中使用stoi()to_string()是非常奇怪的,而且完全没有必要。你可以简单地写

int w = weights[i] - out * weights[i];

要使用std::stoi()std::to_string()您需要有一个适当的

#include <string>

语句和 C++11 语言选项集(请参阅指向上述文档参考的链接(。