从 txt 文件读取到矢量中,然后按数字C++排序
Reading from txt file into vector then sorting numerically C++
>03 Ribena 7 2.20
17 咖啡 76 1.50
24 茶 21 1.10
39 精灵3 1.70
56 可乐 18 1.70
68橙色106 2.20
77 青柠 11 2.10
86 葡萄 34 2.30
55 巧克力 15 2.70
16 霜冻 20 2.20
65柠檬水21 1.90
55酸奶99 3.40
05 冲床 3 1.70
这些记录需要根据第一列上的项目编号进行排序。
void record_initialize()
{
vector<string> record;
cout << "n";
record.push_back("03""tt""Ribena""tt""7""tt""2.20");
record.push_back("17" "tt""Coffee" "tt""76" "tt""1.50");
record.push_back("24" "tt""Tea" "tt""21" "tt""1.10");
record.push_back("39" "tt""Sprite" "tt""3" "tt""1.70");
record.push_back("56" "tt""Cola" "tt""18" "tt""1.70");
record.push_back("68" "tt""Orange" "tt""106""tt""2.20");
record.push_back("77" "tt""Lime" "tt""11" "tt""2.10");
record.push_back("86" "tt""Grape" "tt""34" "tt""2.30");
record.push_back("55" "tt" "Chocolate" "t""15" "tt""2.70");
record.push_back("16" "tt""Frosty" "tt""20" "tt""2.20");
record.push_back("55" "tt" "Lemonade" "t""21" "tt""1.90");
record.push_back("55" "tt""Yoghurt" "tt""99" "tt""3.40");
record.push_back("05" "tt""Punch" "tt""3" "tt""1.70");
cout << "n";
//sort(record.begin(), record.end());
ofstream output_file("Drinks.txt");
ostream_iterator<string> output_iterator(output_file, "n");
copy(record.begin(), record.end(), output_iterator);
}
void record_add()
{
DrinkRecord d;
cout << "n";
cout << "Please enter the item no.: ";
cin >> d.no;
cout << "n";
cout << "Please enter name of drink: ";
cin >> d.name;
cout << "n";
cout << "Please enter the quantity: ";
cin >> d.quantity;
cout << "n";
cout << "Please enter the unit price: ";
cin >> d.price;
cout << "n";
ofstream myfile;
myfile.open("Drinks.txt", ios::app | ios::out);
cout << "n";
myfile << d.no << "tt" << d.name << "tt" << d.quantity << "tt" << d.price << endl;
myfile.close();
}
void record_view()
{
cout << "n";
cout << "ItemNo" << "tt" << "ItemName" << "t" << "Quantity" << "t" << "Unit Price" << endl;
cout << "n";
string getcontent;
ifstream openfile("Drinks.txt");
if (openfile.is_open())
{
while (!openfile.eof())
{
getline(openfile, getcontent);
cout << getcontent << endl;
}
}
}
我已经设法做到了这一部分。但是在将新项目添加到饮料库存后,我现在遇到了排序问题。我的讲师告诉我将 txt 文件读入向量,然后对向量进行排序,然后显示结果。我似乎无法采取正确的步骤。任何帮助将不胜感激。我尝试过这样的事情。
void read_File() //Read the file into the vector function definition
{
vector<string> logs;
string line;
cout << "Testing loading of file." << endl;
ifstream myfile("Drinks.txt");
if (myfile.is_open())
{
while (!myfile.eof())
{
getline(myfile, line);
logs.push_back(line);
sort(line.begin(), line.end());
}
myfile.close();
}
else{
cout << "Unable to open file." << endl;
}
}
似乎您在阅读每一行后正在排序。请注意,对sort
的调用在循环内。这意味着在每次迭代(在您阅读的每一行之后)您都会调用sort
.如果我正确理解了您的作业,您需要对文件中的所有行(logs
)进行排序。如果是这样,那么这意味着您只需调用sort
一次。而不是将行作为参数传递,您可能需要将logs
向量作为参数传递给它,因为您想要对日志进行排序。
您必须在读取整个文件后对其进行一次排序。
此外,sort(line.begin(), line.end()); 将根据字符串比较规则对向量中的字符串进行排序,但是,您需要根据第一列进行排序。
您需要编写自己的自定义比较器,该比较器从要比较的两个字符串中读取第一列。
由于这看起来像一个类问题,我将让您编写下面的比较器函数。
请参阅下面的链接以获取更多帮助。
sort(line.begin(), line.end(), mycolumncomparator);
// Fill the below function correctly
bool mycolumncomparator(const std::string& i, const std:string& j)
{
// Read the first column from both strings
// covert them into int using atoi
// return (int1 < int2);
}
请参考这里: http://www.cplusplus.com/reference/algorithm/sort/
- 首先按给定顺序打印所有数字,然后使用 Array 打印所有字符和其他符号
- 在C++中编程,将 3 个数字发送到一个函数,然后计算这 3 个数字的平均函数
- 编写一个程序,提示用户输入一个整数,然后输出数字的单个数字和数字的总和
- 修改的选择排序,选择最大的数字,然后交换到最后
- 检查字符串是否是数字,然后将该数字转换为 int?
- 如果用户想要输入 x 数量的数字,我如何设法跟踪它然后结束程序?
- 请说明这种怪异的行为C 乘法数字,然后说明mod
- 从for循环中给出最小数字,然后使开关无限
- C++ 将文件中的数字读取到数组中,然后将其打印到其他数组中
- c++ 输入一个数字来确定其他数字,然后比较它们
- 如何从用户那里获得输入字符串而不是示例,然后计算空格、标点符号、数字和字母.C++
- 如何在数组中的范围内添加所有数字?然后找到该范围的平均值
- 使用C - CIN获取几个整数数字,然后将它们放入数组中
- 用getline在字符串中从文件中读取,然后将数字从int数组中的字符串划分
- 从txt文件中读取数字,添加一些数字,然后在c ++中将其重写为其他文件
- 随机选择一个 0 到 8 之间的数字,直到没有数字可以选择,然后重新开始
- 将一个数字的一个数字与其当前位置相乘,然后使用递归将其和其他数字相加
- QString::compare() vs 将 QString 转换为数字然后进行比较
- 在C++中,我需要创建一个程序,该程序在输入某个数字时循环并停止,然后显示最大值和最小值
- 在C++的文本文件中读取数字,然后读取字母