Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/160.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 即使我指定了一个值,映射值还是空的?_C++_Dictionary_Stl - Fatal编程技术网

C++ 即使我指定了一个值,映射值还是空的?

C++ 即使我指定了一个值,映射值还是空的?,c++,dictionary,stl,C++,Dictionary,Stl,我有一个std::map,如下所示: std::map<std::string, std::string> imagePaths; imagePaths.insert(std::make_pair("Square.bmp", "Square")); for (auto iterator = imagePaths.begin(); iterator != imagePaths.end(); iterator++) { std::cout << "Loaded &

我有一个std::map,如下所示:

std::map<std::string, std::string> imagePaths;
imagePaths.insert(std::make_pair("Square.bmp", "Square"));
for (auto iterator = imagePaths.begin(); iterator != imagePaths.end(); iterator++)
{
    std::cout << "Loaded > " << imagePaths[iterator->first] << " image path : " << imagePaths[iterator->second] << std::endl;
}
但是,当我在地图中循环并显示第一个和第二个值时,如下所示:

std::map<std::string, std::string> imagePaths;
imagePaths.insert(std::make_pair("Square.bmp", "Square"));
for (auto iterator = imagePaths.begin(); iterator != imagePaths.end(); iterator++)
{
    std::cout << "Loaded > " << imagePaths[iterator->first] << " image path : " << imagePaths[iterator->second] << std::endl;
}
for(自动迭代器=imagepath.begin();迭代器!=imagepath.end();迭代器++)
{

std::cout您使用了错误的方式来显示地图内容

试试这个:

for (auto iterator = imagePaths.begin(); iterator != imagePaths.end(); iterator++)
{
   std::cout << "Loaded > " << iterator->first << " image path : " << iterator->second << std::endl;
}

值将是“正方形”。

std::map
将成对的第一个元素映射到第二个元素,反之则不起作用


除此之外,在循环中应用映射根本没有意义,因为您已经可以通过迭代器访问这两个值。

这是可行的,但是如果我想打印出值“Square”?我已经尝试过:
std::cout我会在2分钟内,在接受之前必须等待一点。:p当您需要查找值(“Square”时在您的情况下)使用键(“Square.bmp”)。因此您使用了.find()方法。我在回答中添加了它。哦,我设法将键与值混淆。我想使用“Square”作为键,但我使用Square.bmp作为键:P谢谢您的帮助!