/*
运维工程师采集到某产品线网运行一天产生的日志n条
现需根据日志时间先后顺序对日志进行排序
日志时间格式为H:M:S.N
H表示小时(0~23)
M表示分钟(0~59)
S表示秒(0~59)
N表示毫秒(0~999)
时间可能并没有补全
也就是说
01:01:01.001也可能表示为1:1:1.1
输入描述
第一行输入一个整数n表示日志条数
1<=n<=100000
接下来n行输入n个时间
输出描述
按时间升序排序之后的时间
如果有两个时间表示的时间相同
则保持输入顺序
示例:
输入:
2
01:41:8.9
1:1:09.211
输出
1:1:09.211
01:41:8.9
示例
输入
3
23:41:08.023
1:1:09.211
08:01:22.0
输出
1:1:09.211
08:01:22.0
23:41:08.023
示例
输入
2
22:41:08.023
22:41:08.23
输出
22:41:08.023
22:41:08.23
时间相同保持输入顺序
*/
using namespace std;
#include<string>
#include<vector>
#include<iostream>
#include<algorithm>
#include<map>
int main()
{
string str;
vector<string>base;
string base1;
vector<vector<int>>result;
map< vector<int>, string>hash_map;
vector<int>v1;
int num;
cin >> num;
int pos=0;
for (int i = 0; i < num; i++)
{
cin >> str;
for (int j = 0; j < str.size(); j++) {
if (str[j] == ':' || str[j] == '.') {
base1 = str;
string str1 = str.substr(pos, j - pos);
int digit = stoi(str1);
v1.push_back(digit);
str1.clear();
pos = j+1 ;
}
}
string str1 = str.substr(pos, str.size()-pos+1);
int digit = stoi(str1);
v1.push_back(digit);
result.push_back(v1);
hash_map.insert(pair< vector<int>, string>(v1,base1));
v1.clear();
pos = 0;
}
sort(result.begin(), result.end());
map< vector<int>, string>::iterator it = hash_map.begin();
for (int i = 0; i < result.size(); i++)
{
auto FIND = hash_map.find(result[i]);
cout << FIND->second<<endl;
}
return 0;
}
需要注意的问题
1.看题的输出是要什么形式,要输出所有输入的内容不变,就要考虑是否用哈希存储因为排序被分隔的字符串。因为字符串被分隔,要保留源字符串放起来。
2.局部变量在for循环的的使用,注意要考虑是否清空变量或者什么时候清空!!!
3.substr()的使用要注意。