This code:
#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]);
}
With this input:
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
Should 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"
However, it gives these errors when compiled:
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]);
I am wondering why this occurs. For this program, we are not allowed to use classes or structs. Just the program given to us. It was to see how easy classes and structs make programming and how hard it is to actually program without them.