本文介绍了stoi和stoll在c ++的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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.



    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中是新的,GCC只有在你指定版本的语言使用命令行选项 -std = c ++ 11 (或 -std = c ++ 0x

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).

如果你不能使用C ++ 11某些原因,你可以使用字符串流进行转换:

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;
}

或者,如果你感觉自闭症,C函数在< cstring> 中声明的code> strtoll 。

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

这篇关于stoi和stoll在c ++的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-23 03:32