c ++中的stoi和stoll

the*_*3RV 3 c++ string compilation string.h

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

Z:\ G\CSCE 437> g ++ convert.cpp -o conv convert.cpp:函数void transfer(std::string*)': convert.cpp:103:36: error:stoll'未在此范围内声明convert.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]);`
Run Code Online (Sandbox Code Playgroud)

结果是一个字符串数组.

Mik*_*our 10

这些函数是C++ 11中的新功能,如果您使用命令行选项指定该语言版本-std=c++11(或者-std=c++0x在某些旧版本上,我认为您需要4.5版本),GCC才会使其可用.

如果由于某种原因无法使用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;
}
Run Code Online (Sandbox Code Playgroud)

或者,如果你感觉自虐,那么C函数就会在strtoll声明中声明<cstring>.