stoi and stoll in c++

别等时光非礼了梦想. 提交于 2019-12-10 09:37:41

问题


I have #include(string) in my declaratives at the top of the program but when I try to run stoi(string) or stoll(string) i get the following error. I am running Cygwin g++ v4.5.3.

Z:\G\CSCE 437>g++ convert.cpp -o conv convert.cpp: In function void transfer(std::string*)': convert.cpp:103:36: error:stoll' was not declared in this scope convert.cpp:116:35: error: `stoi' was not declared in this scope

    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]);`

Where result is an array of strings.


回答1:


These functions are new in C++11, and GCC only makes it available if you specify that version of the language using the command-line option -std=c++11 (or -std=c++0x on some older versions; I think you'll need that for version 4.5).

If you can't use C++11 for some reason, you could convert using string streams:

#include <sstream>

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

or, if you're feeling masochistic, the C functions in such as strtoll declared in <cstring>.



来源:https://stackoverflow.com/questions/14590410/stoi-and-stoll-in-c

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!