我有200行,看起来像这样的文本文件:重新排列线列在文本文件
1 4:48:08 Orvar Steingrimsson 1979 30 - 39 ara IS200
2 4:52:25 Gudni Pall Palsson 1987 18 - 29 ara IS870
我的代码排序这些行每个选手,而不是因为它在时间的出生年份上面的例子。所以,现在的行排序为这样的:(只多不少行)
2 4:52:25 Gudni Pall Palsson 1987 18 - 29 ara IS870
1 4:48:08 Orvar Steingrimsson 1979 30 - 39 ara IS200
我的问题是我怎么能现在做上市的三件事情:年 - 名字 - 时间......所以,这两个行会看像这样:
1987 Gudni Pall Palsson 4:52:25
1979 Orvar Steingrimsson 4:48:08
我的代码,到目前为止,这种种按照正确的顺序行:
#include <iostream> //for basic functions
#include <fstream> //for basic file operations
#include <string> //for string operations
#include <map> //for multimap functions
using namespace std;
int main()
{
ifstream in("laugavegurinn.txt", ios::in);
ofstream out("laugavegurinn2.txt");
string str;
multimap<int, string> datayear; //creates multimap linking each line to year
in.ignore(80);//ignores header of the file - 80 characters - to escape problems with while loop
// while (getline(in,str)) {
// string name = str.substr(18,30);
// string time = str.substr(8,7);
//}
while (getline(in,str)) {
int year = stoi(str.substr(54, 4));
datayear.insert(make_pair(year,str)); //insert function and pairs year and string
}
for (auto v : datayear)
out << v.second << "\n";
in.close();
out.close();
}
我不能创建变量下的名称和时间,然后将它们添加到线:出<< v.first <<名字<<时间<<“\ n \; –
什么应是第三个版本这个问题?首先是[这](http://stackoverflow.com/q/19055100/1870232)。我想知道这是一个[XY](http://meta.stackexchange.com/q/66377/ 232397)问题? – P0W
如果所有行按相同顺序排列,则可以使用#include stringstream获取每个字符串的单个信息使用某些std :: vector构造对所有信息进行排列,然后您只需要打印我对你的问题是否正确? – Martin