/
main.cpp
51 lines (48 loc) · 1.24 KB
/
main.cpp
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
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
#include <iostream>
#include <fstream>
#include <sstream>
#include <vector>
#include <algorithm>
typedef std::pair<int, std::string> pair_t;
struct LessThan : std::binary_function<pair_t, pair_t, bool>
{
bool operator()(const pair_t& x, const pair_t& y) const
{
return x.first < y.first;
}
};
int main(int argc, char** argv)
{
if(argc != 2) {
std::cout << "expect 1 arg" << std::endl;
return 1;
}
std::cout << "input:" << std::endl;
std::ifstream myFile(argv[1]);
if(!myFile.is_open()) {
return 1;
}
std::vector<pair_t> myVec;
while(!myFile.eof()) {
char buf[256];
myFile.getline(buf, 256);
if(myFile.eof()) {
break;
}
std::cout << buf << std::endl;
std::stringstream ss;
ss << buf;
int key;
std::string value;
ss >> key >> value;
myVec.push_back(pair_t(key, value));
}
myFile.close();
std::cout << std::endl;
std::cout << "output:" << std::endl;
std::sort(myVec.begin(), myVec.end(), LessThan());
for(std::vector<pair_t>::iterator p = myVec.begin(); p != myVec.end(); p++) {
std::cout << (*p).first << ' ' << (*p).second << std::endl;
}
return 0;
}