0

我最近必须开始使用 c++ 并遇到很多问题。我的问题是我需要一些相同版本的 c++ 不支持的功能。例如,以下代码读取一个两列文件,对其进行标记,然后存储到字典中:

map<int,int>  ReadTables::lookupTable(string fpath) {
    map<int,int> lookup;    
    ifstream in;
    in.open(fpath.c_str());
    if ( !in.good()) {
        cout << "ERROR: Opening file failed.\n";
        exit (EXIT_FAILURE);
    }
    string line;
    const char delimiter[] = " ";
    vector<string> tokens;
    while (!in.eof()){
        getline(in,line);
        tokens = tokenize( line, delimiter );
        lookup[ atoi(tokens[0].c_str())] = atoi(tokens[1].c_str()); 
        //lookup[ stoi(tokens[0])] = stoi(tokens[1]);   
    } 

只要我不使用 -std=c++0x标志,它就可以正常工作。当我使用这个版本时,我得到了:

 *** Break *** segmentation violation


===========================================================
There was a crash.
This is the entire stack trace of all threads:
===========================================================
#0  0x00007fc4782270ee in waitpid () from /lib/x86_64-linux-gnu/libc.so.6
#1  0x00007fc4781b9e8e in ?? () from /lib/x86_64-linux-gnu/libc.so.6
#2  0x00007fc47cb0a98e in TUnixSystem::StackTrace() () from /usr/local/lib/root/libCore.so
#3  0x00007fc47cb0a223 in TUnixSystem::DispatchSignals(ESignals) () from /usr/local/lib/root/libCore.so
#4  <signal handler called>
#5  0x00007fc478c64050 in std::basic_string<char, std::char_traits<char>, std::allocator<char> >::c_str() const () from /usr/lib/x86_64-linux-gnu/libstdc++.so.6
#6  0x000000000040a83c in ReadTables::lookupTable (this=0x616500, fpath=...) at ../src/ReadTables.cpp:59
#7  0x000000000040ffd8 in main () at ../src/read.cpp:37
===========================================================


The lines below might hint at the cause of the crash.
If they do not help you then please submit a bug report at
http://root.cern.ch/bugs. Please post the ENTIRE stack trace
from above as an attachment in addition to anything else
that might help us fixing this issue.
===========================================================
#5  0x00007fc478c64050 in std::basic_string<char, std::char_traits<char>, std::allocator<char> >::c_str() const () from /usr/lib/x86_64-linux-gnu/libstdc++.so.6
#6  0x000000000040a83c in ReadTables::lookupTable (this=0x616500, fpath=...) at ../src/ReadTables.cpp:59
#7  0x000000000040ffd8 in main () at ../src/read.cpp:37
==========================================================

我还意识到c11我应该使用stoiwhich 再次给出此错误:

 *** Break *** segmentation violation



===========================================================
There was a crash.
This is the entire stack trace of all threads:
===========================================================
#0  0x00007f26561b50ee in waitpid () from /lib/x86_64-linux-gnu/libc.so.6
#1  0x00007f2656147e8e in ?? () from /lib/x86_64-linux-gnu/libc.so.6
#2  0x00007f265aa9898e in TUnixSystem::StackTrace() () from /usr/local/lib/root/libCore.so
#3  0x00007f265aa98223 in TUnixSystem::DispatchSignals(ESignals) () from /usr/local/lib/root/libCore.so
#4  <signal handler called>
#5  0x00007f2656bf2050 in std::basic_string<char, std::char_traits<char>, std::allocator<char> >::c_str() const () from /usr/lib/x86_64-linux-gnu/libstdc++.so.6
#6  0x000000000040ac44 in std::stoi (__str=..., __idx=0x0, __base=10) at /usr/include/c++/4.4/bits/basic_string.h:2567
#7  0x000000000040a8e6 in ReadTables::lookupTable (this=0x616500, fpath=...) at ../src/ReadTables.cpp:62
#8  0x0000000000410178 in main () at ../src/read.cpp:37
===========================================================


The lines below might hint at the cause of the crash.
If they do not help you then please submit a bug report at
http://root.cern.ch/bugs. Please post the ENTIRE stack trace
from above as an attachment in addition to anything else
that might help us fixing this issue.
===========================================================
#5  0x00007f2656bf2050 in std::basic_string<char, std::char_traits<char>, std::allocator<char> >::c_str() const () from /usr/lib/x86_64-linux-gnu/libstdc++.so.6
#6  0x000000000040ac44 in std::stoi (__str=..., __idx=0x0, __base=10) at /usr/include/c++/4.4/bits/basic_string.h:2567
#7  0x000000000040a8e6 in ReadTables::lookupTable (this=0x616500, fpath=...) at ../src/ReadTables.cpp:62
#8  0x0000000000410178 in main () at ../src/read.cpp:37
===========================================================

我在这里缺少什么?有没有关于使用哪个版本的建议?

4

2 回答 2

3

如果tokenize无法返回包含至少两个元素的向量,您的代码将访问向量末尾并遇到未定义的行为。一种这样的未定义行为是,您的程序似乎在 C++98 中运行,而在 C++11 编译中显着失败。

于 2013-08-22T13:39:14.817 回答
1

您可以大大简化您的代码:

std::map<int,int>  ReadTables::lookupTable(const std::string& fpath) 
{
    map<int,int> lookup;    
    std::ifstream fin(fpath.c_str());
    if (!fin.good()) 
    {
        cout << "ERROR: Opening file failed.\n";
        exit(EXIT_FAILURE); // should probably throw an exception here instead of exiting the program
    }

    // load all the integers from the file
    std::vector<int> v;
    std::copy(std::istream_iterator<int>(fin), std::istream_iterator<int>(), std::back_inserter<vector<int>>(v));
    assert(v.size() % 2 == 0); // assert that the file loaded key/value pairs
    for (i = 0; i < v.size(); i += 2)
    {
        lookup[v[i]] = v[i + 1];
    }

    fin.close();
    return lookup;
}

我必须进行一些挖掘,但您可以通过调整std::copy调用直接加载到std::map.

于 2013-08-22T13:52:49.193 回答