stoi and stoll in c++

stoi and stoll in c++

本文关键字:c++ in stoll and stoi      更新时间:2023-10-16

我在程序顶部的声明中有 #include(字符串),但是当我尝试运行stoi(string)或stoll(string)时,我收到以下错误。我正在运行Cygwin g ++ v4.5.3。

Z:\G\CSCE 437>g++ convert.cpp -o conv 转换.cpp:在此范围内未声明void transfer(std::string*)': convert.cpp:103:36: error: stoll' 的功能 转换.cpp:116:35:错误:"stoi"未在此范围内声明

    fileTime[numRec] = stoll(result[0]);    //converts string to Long Long
    if(numRec = 0){
       beginningTime = fileTime[0];
    }
    fileTime[numRec] = timeDiff;
    hostName[numRec] = result[1];
    diskNum[numRec] = stoi(result[2]);
    type[numRec] = result[3];
    offset[numRec] = stoi(result[4]);
    fileSize[numRec] = stoi(result[5]);
    responseTime[numRec] = stoi(result[6]);`

其中结果是一个字符串数组。

这些功能是 C++11 中的新功能,仅当您使用命令行选项 -std=c++11 指定该版本的语言(或在某些旧版本上-std=c++0x时,GCC 才使其可用;我认为您在 4.5 版中需要它)。

如果由于某种原因无法使用 C++11,则可以使用字符串流进行转换:

#include <sstream>
template <typename T> from_string(std::string const & s) {
    std::stringstream ss(s);
    T result;
    ss >> result;    // TODO handle errors
    return result;
}

或者,如果你感到受虐狂,C 在 <cstring> 中声明strtoll 中的 C 函数。