什么导致以下错误?

时间:2017-10-03 04:16:25

标签: 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]); 

我想知道为什么会这样。 对于此程序,我们不允许使用类或结构。只是给我们的程序。这是为了看看类和结构如何简单地编程以及没有它们实际编程有多难。

1 个答案:

答案 0 :(得分:0)

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

  

你编错了。在线编译器的优点在于,您可以确切地了解如何编译/使用它(在这种情况下,c++11 was enough并进行两次微调,使其成为c++03 compatible) - sehe 2 hours ago