根据没有类或结构的另一个向量的元素对一个向量进行分类
Sort One Vector Based on the Elements of Another Vector With no Class or Struct
#include <algorithm>
#include <fstream>
#include <iostream>
#include <vector>
using namespace std;
const int max_applications_num = 1000;
vector<string> vector_authors;
vector<string> vector_titles;
vector<string> vector_venue;
vector<int> vector_year;
vector<string> vector_presentation;
void Tokenize(string line, vector<string> &tokens, string delimiters = "t ") {
string token = "";
string OneCharString = " ";
for (int 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) {
string authors = tokens[1];
string title = tokens[2];
string venue = tokens[3];
int year = atoi(tokens[4].c_str());
string presentation = tokens[5];
vector_authors.push_back(authors);
vector_titles.push_back(title);
vector_venue.push_back(venue);
vector_year.push_back(year);
vector_presentation.push_back(presentation);
// cout << "in save" << endl;
}
void remove_application(int pos) {
vector_authors.erase(vector_authors.begin() + pos);
vector_titles.erase(vector_titles.begin() + pos);
vector_venue.erase(vector_venue.begin() + pos);
vector_year.erase(vector_year.begin() + pos);
vector_presentation.erase(vector_presentation.begin() + pos);
// cout << "in remove" << endl;
}
void sort() {
for (int j = 0; j <= vector_year.size() - 1; j++) {
int temp1 = vector_year.at(j);
int i = j - 1;
while (i > -1 and vector_year.at(i) > temp1) {
vector_year.at(i + 1) = vector_year.at(i);
i = i - 1;
}
vector_year.at(i + 1) = temp1;
}
for (int j = 0; j <= vector_authors.size() - 1; j++) {
string temp2 = vector_authors.at(j);
int i = j - 1;
while (i > -1 and vector_authors.at(i) > temp2) {
vector_authors.at(i + 1) = vector_authors.at(i);
i = i - 1;
}
vector_authors.at(i + 1) = temp2;
}
for (int j = 0; j <= vector_titles.size() - 1; j++) {
string temp3 = vector_titles.at(j);
int i = j - 1;
while (i > -1 and vector_titles.at(i) > temp3) {
vector_titles.at(i + 1) = vector_titles.at(i);
i = i - 1;
}
vector_titles.at(i + 1) = temp3;
}
for (int j = 0; j <= vector_venue.size() - 1; j++) {
string temp4 = vector_venue.at(j);
int i = j - 1;
while (i > -1 and vector_venue.at(i) > temp4) {
vector_venue.at(i + 1) = vector_venue.at(i);
i = i - 1;
}
vector_venue.at(i + 1) = temp4;
}
for (int j = 0; j <= vector_presentation.size() - 1; j++) {
string temp5 = vector_presentation.at(j);
int i = j - 1;
while (i > -1 and vector_presentation.at(i) > temp5) {
vector_presentation.at(i + 1) = vector_presentation.at(i);
i = i - 1;
}
vector_presentation.at(i + 1) = temp5;
}
// cout << "in sort" << endl;
}
void print() {
for (int i = 0; i < vector_authors.size(); i++) {
cout << vector_authors.at(i) << "t" << vector_titles.at(i) << "t"
<< "t" << vector_venue.at(i) << "t" << vector_year.at(i) << "t" << vector_presentation.at(i) << 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.txtn";
return 1;
}
ExecuteCommands(argv[1]);
return 0;
}
所以,这是我在学校做的实验室的代码。我们应该将某些元素放在向量中,打印那些向量,订购它们,再次打印它们,删除向量,最后一次打印它们。就我们的形式而言,我们需要根据出版年份订购它们。
"authors_list1" "title1" "conference1" 2016 "poster"
"authors_list3" "title3" "conference2" 2010 "oral"
"authors_list2" "title2" "journal1" 2015 "none"
所以,当我排序时,我得到了:
"authors_list1" "title1" "conference1" 2010 "none"
"authors_list2" "title2" "conference2" 2015 "oral"
"authors_list3" "title3" "journal1" 2016 "poster"
这是预期的输出:
"authors_list3" "title3" "conference2" 2010 "oral"
"authors_list2" "title2" "journal1" 2015 "none"
"authors_list1" "title1" "conference1" 2016 "poster"
多年的顺序是正确的,但是其他一切的顺序不是。我需要我的其他元素来效仿这一年份。有什么方法可以做到吗?
P.S。对于此实验室,我们不允许使用类或结构。这是我拥有的所有代码。
您可以用
伪造非浮肿的数据结构vector<vector<string> > database;
其中vector<string>
是单个记录。为了使其"可管理",我将使用一些别名,访问器功能和此枚举:
enum { AUTHOR, TITLE, VENUE, YEAR, PRESENTATION };
代码变短了很多:
活在coliru
#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.txtn";
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"
为什么不使用并行向量,当您订购年度向量时,如果将i
与位置交换j
,请在每个向量上进行。
相关文章:
- 函数向量_指针有不同的原型,我可以构建一个吗
- C++从另一个类访问公共静态向量的正确方法是什么
- 为什么我不能将一个对象push_back到属于另一个类的对象向量中?
- 给定一个向量,如何找到该向量的所有子集和的原始索引
- 获取向量C++中第一个值和最后一个值的和
- 从多个源构造一个对象,包括一个对象向量
- 为什么一个向量上的多线程操作很慢
- 基于范围的 for 循环:迭代使用一个元素扩展的向量
- C++ - 如何在结构向量中找到结构体一个成员的最大值?
- 如何在 C++11 中查找和更新向量中的一个嵌套结构
- Lower_bound不适用于具有 3 个元素的向量的最后一个元素
- 将一个向量插入另一个向量的某个位置
- std::find,返回所有找到的值的替代方法,而不仅仅是存在重复的向量的第一个值
- 将指向给定子类的指针从一个向量复制到另一个向量
- 如何创建一个类,以便向量工作 std::vector<MyClass<int>> v{ 1,2,3 };
- C++ STD 函数运算符:有没有一种方法可以通过函数将一个向量映射到另一个向量上?
- 将向量之间的数字放在另一个向量之间<vector>>如果两个数字的差值为 1
- 如何在不复制的情况下将一个向量移动到另一个向量中
- 创建一个函数的 Python 绑定,返回指向带有 boost 的向量的指针
- 紧凑向量一个小的内存足迹向量