START:

第一道不依靠题解写出来的题目。

题目

给出来 ID,进出时间让你计算谁最先来,最晚走

思路

把时间转化为一堆数字,因为 mapkey值有序,所以直接无脑map就够了。

代码

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
#include <iostream>
#include <map>
#include <string>


using namespace std;

map<int, string>in;
map<int, string>out;

int main()
{
int n;
cin >> n;
while (n--)
{
int in_h, in_m, in_s;
int out_h, out_m, out_s;
char d;
string id;
cin >> id;
cin >> in_h >> d >> in_m >>d>> in_s;
cin >> out_h >> d >> out_m >> d >> out_s;

int in1, out1;
in1 = in_h * 3600 + in_m * 60 + in_s;
out1 = out_h * 3600 + out_m * 60 + out_s;
in.insert(make_pair(in1,id));
out.insert(make_pair(out1,id));
}
map<int, string>::iterator iter = in.begin();

string str1 = (*(iter)).second;
map<int, string>::reverse_iterator iter2 = out.rbegin();
string str2 = (*(iter2)).second;
cout << str1 << " " << str2;
}

思考

  • Q:神奇的事情。

    我使用 c++语言定义了一个指向out的尾部迭代器:

    map<int, string>::iterator iter = out.end();

    并将这个迭代器的value值赋给 str1:

    string str1 = (*(iter)).second;

    但是报错:

    0x0F11E906 (ucrtbased.dll) (PATEST–A1006.exe 中)处有未经处理的异常: 将一个无效参数传递给了将无效参数视为严重错误的函数。

    进入调试界面,str1 报读取字符串字符时出错。展开看到每一位都是无法读取内存。


    换用反向迭代器没有问题:

    map<int, string>::reverse_iterator iter = out.rbegin();

    这是为什么呢?

    A:end是指向最后一个元素的后一个位置。

  • Q:不用#include <string> cin >> id;竟然报错,为何?

    id 为 string类型。

END

相关文章
评论
分享
Please check the post_relate setting in config.yml of hexo-theme-Annie! You should make sure 'postsEnable == true' and the number of site posts greater than 1.
Please check the comment setting in config.yml of hexo-theme-Annie!