c++ - 是什么导致了以下错误?

标签 c++

这段代码:

#include <algorithm>
#include <fstream>
#include <iostream>
#include <vector>
#include <cassert>
using namespace std;

const int max_applications_num = 1000;

enum { AUTHOR, TITLE, VENUE, YEAR, PRESENTATION };

vector<vector<string> > database;

void Tokenize(string line, vector<string> &tokens, string delimiters = "\t ") {
string token = "";
string OneCharString = " ";
for (size_t i = 0; i < line.size(); i++)
    if (find(delimiters.begin(), delimiters.end(), line[i]) !=
        delimiters.end()) // line[i] is one of the delimiter characters
    {
        if (token != "")
            tokens.push_back(token);
        token = "";
    } else {
        OneCharString[0] = line[i];
        token += OneCharString;
    }

if (token != "")
    tokens.push_back(token);
}

void SaveApplication(const vector<string> &tokens) {
    database.emplace_back(tokens.begin()+1, tokens.end());
}

void remove_application(size_t pos) {
    assert(pos < database.size());
    database.erase(database.begin()+pos);
}

int year_of(vector<string> const &record) { return stoi(record[YEAR]); }
int year_of(int i) { return year_of(database.at(i)); }

void sort() {
    for (size_t j = 0; j <= database.size() - 1; j++) {

    vector<string> tmp = database.at(j);

    int tmp_year = year_of(tmp);

    int i = j - 1;
    while (i > -1 and year_of(i) > tmp_year) {
        database.at(i + 1) = database.at(i);
        i = i - 1;
    }

    database.at(i + 1) = tmp;
    }
}

void print() {
    for (size_t i = 0; i < database.size(); i++) {
    cout 
        << database.at(i)[AUTHOR] << "\t"
        << database.at(i)[TITLE]  << "\t"
        << database.at(i)[VENUE]  << "\t"
        << database.at(i)[YEAR]   << "\t"
        << database.at(i)[PRESENTATION] 
        << endl;
}
cout << "\n" << endl;
}

void ExecuteCommands(const char *fname) {
ifstream inf;
inf.open(fname);

string line;
while (getline(inf, line).good()) {
    vector<string> tokens;
    Tokenize(line, tokens, "\t ");
    if (tokens.size() == 0)
        continue;

    if (tokens[0].compare("save_application") == 0)
        SaveApplication(tokens);

    else if (tokens[0].compare("remove_application") == 0)
        remove_application(atoi(tokens[1].c_str()));

    else if (tokens[0].compare("sort") == 0)
        sort();

    else if (tokens[0].compare("print") == 0)
        print();
}

inf.close();
}

int main(int argc, char **argv) {
if (argc != 2) {
    cout << "usage: executable.o command.txt\n";
    return 1;
}

ExecuteCommands(argv[1]);
}

有了这个输入:

save_application "authors_list1"    "title1" "conference1"  2016    "poster"

save_application "authors_list3"    "title3" "conference2"  2010    "oral"

save_application "authors_list2"    "title2" "journal1" 2015    "none"
print
sort
print
remove_application 0
print

应该打印:

"authors_list1" "title1"    "conference1"   2016    "poster"
"authors_list3" "title3"    "conference2"   2010    "oral"
"authors_list2" "title2"    "journal1"  2015    "none"


"authors_list3" "title3"    "conference2"   2010    "oral"
"authors_list2" "title2"    "journal1"  2015    "none"
"authors_list1" "title1"    "conference1"   2016    "poster"


"authors_list2" "title2"    "journal1"  2015    "none"
"authors_list1" "title1"    "conference1"   2016    "poster"

然而,它在编译时给出了这些错误:

error: ‘class std::vector<std::vector<std::__cxx11::basic_string<char> > >’ has no member named ‘emplace_back’ database.emplace_back(tokens.begin()+1, tokens.end());

error: ‘stoi’ was not declared in this scope int year_of(vector<string> const &record) { return stoi(record[YEAR]); 

我想知道为什么会这样。 对于这个程序,我们不允许使用类或结构。只是给我们的程序。这是为了了解类和结构使编程变得多么容易,以及没有它们实际编程有多么困难。

最佳答案

确实,那是因为您没有启用 c++11 模式(或者您的编译器没有)。幸运的是,我已经在评论中发布了 c++03 版本。

You compiled it wrong. The beauty of online compilers is that you can see exactly how to compile/use it (in this case, c++11 was enough and two tiny tweaks make it c++03 compatible) – sehe 2 hours ago

关于c++ - 是什么导致了以下错误?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46537131/

相关文章:

c++ - 为什么会有定义_tmain 的宏?

c++ - 编写 STL 兼容的迭代器

c++ - C++ 中的 UPnP 发现?

c++ - 在构造函数中调用方法

c++ - 如何让 C++ 程序在用户输入时计算字数?

C++ - WebSockets 是否强制加密,如何获取 "plaintext"?

C++11 用于每个具有多个变量的循环

c++ - 为什么我的全局 new() 覆盖被绕过?

c++ - 错误: no matching function for call C++

c++ - 如何使 wxScrolledWindow 居中对齐?