gpt4 book ai didi

C++ map迭代器问题

转载 作者:太空宇宙 更新时间:2023-11-04 14:35:05 24 4
gpt4 key购买 nike

为什么这段代码有问题(在 visual studio 2010 中)?

#include <iostream>
#include <fstream>
#include <string>
#include <map>

using namespace std;

int main() {

map<string,int> map;
map<string,int>::iterator iter = map.begin();
}

它只是告诉我迭代器定义有问题(类模板“std::iterator”的参数列表丢失),但我看到了这样写的示例。

最佳答案

您调用变量 map你用了using namespace std;这将导致名称查找问题,因为您正在使用一个变量和一个名为同一事物的库容器。大卫在评论和下面解释了这部分。

您可以做几件事(除此之外您还可以做更多):

  1. std:: 限定你的变量声明

    std::map<string, int> map;

    std::map<string, int>::iterator iter = map.begin();

  2. 放下using namespace std;并用 std:: 限定所有内容

  3. 重命名您的 map多变的。实际上,您应该避免使用库定义的名称作为变量名称。

编辑:Marc's answer也应该对你有用。

关于C++ map迭代器问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5159091/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com