This commit is contained in:
programmercarl
2023-08-15 10:09:47 +08:00
parent 4f632c8c41
commit 6a2f01055c
3 changed files with 92 additions and 6 deletions

View File

@@ -237,13 +237,10 @@ public:
```cpp
for (pair<const string, int>& target : targets[result[result.size() - 1]])
```
pair里要有const因为map中的key是不可修改的所以是`pair<const string, int>`
如果不加const也可以复制一份pair例如这么写
一定要加上引用即 `& target`,因为后面有对 target.second 做减减操作,如果没有引用,单纯复制,这个结果就没记录下来,那最后的结果就不对了。
```cpp
for (pair<string, int>target : targets[result[result.size() - 1]])
```
加上引用之后,就必须在 string 前面加上 const因为map中的key 是不可修改了,这就是语法规定了。
## 总结