Comment detail

タブ区切りデータの処理 (Nested Flatten)

This comment is reply for 7741 Y: 1. ファイルの終わりまで繰り返す 1...(タブ区切りデータの処理). Go to thread root.

「第4カラムの値にそれぞれ1を加える」が抜けてますよー

ご指摘ありがとうございます。修正しました。

1. ファイルの終わりまで繰り返す
1.1. 1行を読む
1.2. \tでsplitして*r=vector<string>に格納
1.3. swapで入れ替え
1.4. 1を加える
1.5. rをvector< vector<string> >に格納
2.1. 並び替え
2.2. \tをはさみながらjoinして表示
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
#include <boost/algorithm/string.hpp>
#include <boost/algorithm/string/join.hpp>
#include <boost/foreach.hpp>
#include <boost/lexical_cast.hpp>
using namespace std;
using namespace boost;
using namespace boost::algorithm;
typedef vector<string> row;

bool comp(row* lhs, row* rhs) {
    return lexical_cast<int>((*lhs)[0]) < lexical_cast<int>((*rhs)[0]);
}

int main()
{
    vector<row*> all;
    string line;
    while (getline(cin,line)) {
        row* r=new row;
        split(*r,line,is_any_of("\t"));
        swap((*r)[1],(*r)[2]);
        try { (*r)[3]=lexical_cast<string>(lexical_cast<int>((*r)[3])+1); }
        catch (const bad_lexical_cast&) {}
        all.push_back(r);
    }
    sort(all.begin()+1,all.end(),comp);
    BOOST_FOREACH(row* r, all) cout<<join(*r,"\t")<<endl;
}

Index

Feed

Other

Link

Pathtraq

loading...