std::map 具有 std::map 值的问题
Issue with std::map having an std::map value
我想创建一个 std::map 将 QString 映射到另一个 std::map。但是,当我尝试这样做时,它似乎将它们映射到 std::_Tree.
执行此操作的代码在这里
// *** Meta Data ****
quint8 numMetaDataItems = getBufferUint<quint8>(&frameData[numMetaDataOffset]);
size_t metaDataItemOffset = numMetaDataOffset + sizeof(numMetaDataItems);
std::map<int, QString> tempMap;
for (int i = 0; i < numMetaDataItems; i++)
{
int metadataId = getBufferUint<quint8>(&frameData[metaDataItemOffset]);
QString metadata = QString::fromUtf8((const char*) &frameData[metaDataItemOffset + 1]) ;
//tempMap.insert(std::pair<int, QString>(metadataId, metadata));
metaDataModel->metaDataMap[fileName][metadataId] = metadata;
metaDataItemOffset += headerLength - metaDataItemOffset;
}
当我使用调试器时,地图的键是正确的,但值应该是另一个地图,它却显示 class std::_Tree<>。谁能解释这是为什么?
编辑:
所以我意识到问题可能与地图无关。我尝试在此处访问地图:
QList<QString> strings;
for(std::map<int, QString>::iterator it = metaDataMap[frameID].begin(); it != metaDataMap[frameID].end(); it++)
{
QString tempString = idMap[it->first] + ": " + it->second;
strings.append(tempString);
}
但是由于地图中只有一项,因此 .begin() 和 .end() 似乎是相同的,并且 for 循环中的代码永远不会 运行。这听起来像是正在发生的事情吗?
如this 'doc'所述,
Maps are usually implemented as red-black trees.
这意味着,在您的编译器中 std::map<> 本质上是一个类型定义的 _Tree<> class,因此没有任何问题。
正如您在评论中提到的,问题是如何从 std::map
访问我的示例:
#include<map>
#include<string>
#include<iostream>
int main()
{
std::map<int,int> key = { {1,2}};
std::map< std::map<int,int>,std::string> myMap;
myMap[key]="hello";
std::string val= myMap[key];
std::cout<<val;
}
编辑
std::map<int,int> key = { {1,2}};
std::map< std::string, std::map<int,int>> myMap;
myMap["hello"]=key;
auto val= myMap["hello"];
for( const auto & p: val)
std::cout<<p.first<<" "<<p.second;
When I use the debugger, the key for the map is correct but where the
value should be another map, it instead says class std::_Tree<>. Can
anyone explain why this is?
这暗示了您的编译器如何实现 std::map
。
看起来是Visual C++。如果您查看计算机上的 header 文件(它应该类似于 C:\Program Files (x86)\Microsoft Visual Studio 12.0\VC\include\map
),那么您可能会看到 std::map
publicly 派生自 _Tree
.这没什么神奇的;这只是简单的 public 推导。
现在,名称 _Tree
已经表明它是一个实现细节。您自己的代码中不允许使用这样的名称(下划线 + 大写字母),但编译器本身可以在自己的 header 文件中自由使用它。那是因为对于C++语言而言,standard-library header files是不存在的。您可以在没有任何 header 文件的情况下完美地编写符合标准的 C++ 编译器,其中像 #include <map>
这样的用户代码将在不读取任何文件的情况下被解释。出于简单和实用的原因,我所知道的所有 C++ 编译器(可能所有曾经存在过的编译器)都附带 header files.
重点是编译器自己的 header 文件可能包含所有内容,包括 non-C++ 代码、专有构造和其他客户不需要知道的奇怪内容,但编译器需要以实现 C++ 标准库。
_Tree
就是这么回事。查看标准 header 文件或使用调试器会让您接触到它。
所有这一切都很好,只要您不尝试 fiddle 在您自己的代码中使用它。例如下面的evil一段代码:
#include <map>
int main()
{
// Just for demonstration purposes!
// Do not EVER try this in production code!
using namespace std;
_Tree<_Tmap_traits<int,
int,
less<int>,
allocator<int>,
false>
> m{ less<int>(), allocator<int>() };
}
它很可能会编译并在 MSVC 上正常工作,但是使你的程序 non-portable 甚至可能 non-compatible 与未来或过去版本的同一编译器,因为你已经使用 compiler-internal 组件在您自己的代码中。
简而言之:您自己的代码必须不知道 _Tree
存在的事实。
我想创建一个 std::map 将 QString 映射到另一个 std::map。但是,当我尝试这样做时,它似乎将它们映射到 std::_Tree.
执行此操作的代码在这里
// *** Meta Data ****
quint8 numMetaDataItems = getBufferUint<quint8>(&frameData[numMetaDataOffset]);
size_t metaDataItemOffset = numMetaDataOffset + sizeof(numMetaDataItems);
std::map<int, QString> tempMap;
for (int i = 0; i < numMetaDataItems; i++)
{
int metadataId = getBufferUint<quint8>(&frameData[metaDataItemOffset]);
QString metadata = QString::fromUtf8((const char*) &frameData[metaDataItemOffset + 1]) ;
//tempMap.insert(std::pair<int, QString>(metadataId, metadata));
metaDataModel->metaDataMap[fileName][metadataId] = metadata;
metaDataItemOffset += headerLength - metaDataItemOffset;
}
当我使用调试器时,地图的键是正确的,但值应该是另一个地图,它却显示 class std::_Tree<>。谁能解释这是为什么?
编辑:
所以我意识到问题可能与地图无关。我尝试在此处访问地图:
QList<QString> strings;
for(std::map<int, QString>::iterator it = metaDataMap[frameID].begin(); it != metaDataMap[frameID].end(); it++)
{
QString tempString = idMap[it->first] + ": " + it->second;
strings.append(tempString);
}
但是由于地图中只有一项,因此 .begin() 和 .end() 似乎是相同的,并且 for 循环中的代码永远不会 运行。这听起来像是正在发生的事情吗?
如this 'doc'所述,
Maps are usually implemented as red-black trees.
这意味着,在您的编译器中 std::map<> 本质上是一个类型定义的 _Tree<> class,因此没有任何问题。
正如您在评论中提到的,问题是如何从 std::map
访问我的示例:
#include<map>
#include<string>
#include<iostream>
int main()
{
std::map<int,int> key = { {1,2}};
std::map< std::map<int,int>,std::string> myMap;
myMap[key]="hello";
std::string val= myMap[key];
std::cout<<val;
}
编辑
std::map<int,int> key = { {1,2}};
std::map< std::string, std::map<int,int>> myMap;
myMap["hello"]=key;
auto val= myMap["hello"];
for( const auto & p: val)
std::cout<<p.first<<" "<<p.second;
When I use the debugger, the key for the map is correct but where the value should be another map, it instead says class std::_Tree<>. Can anyone explain why this is?
这暗示了您的编译器如何实现 std::map
。
看起来是Visual C++。如果您查看计算机上的 header 文件(它应该类似于 C:\Program Files (x86)\Microsoft Visual Studio 12.0\VC\include\map
),那么您可能会看到 std::map
publicly 派生自 _Tree
.这没什么神奇的;这只是简单的 public 推导。
现在,名称 _Tree
已经表明它是一个实现细节。您自己的代码中不允许使用这样的名称(下划线 + 大写字母),但编译器本身可以在自己的 header 文件中自由使用它。那是因为对于C++语言而言,standard-library header files是不存在的。您可以在没有任何 header 文件的情况下完美地编写符合标准的 C++ 编译器,其中像 #include <map>
这样的用户代码将在不读取任何文件的情况下被解释。出于简单和实用的原因,我所知道的所有 C++ 编译器(可能所有曾经存在过的编译器)都附带 header files.
重点是编译器自己的 header 文件可能包含所有内容,包括 non-C++ 代码、专有构造和其他客户不需要知道的奇怪内容,但编译器需要以实现 C++ 标准库。
_Tree
就是这么回事。查看标准 header 文件或使用调试器会让您接触到它。
所有这一切都很好,只要您不尝试 fiddle 在您自己的代码中使用它。例如下面的evil一段代码:
#include <map>
int main()
{
// Just for demonstration purposes!
// Do not EVER try this in production code!
using namespace std;
_Tree<_Tmap_traits<int,
int,
less<int>,
allocator<int>,
false>
> m{ less<int>(), allocator<int>() };
}
它很可能会编译并在 MSVC 上正常工作,但是使你的程序 non-portable 甚至可能 non-compatible 与未来或过去版本的同一编译器,因为你已经使用 compiler-internal 组件在您自己的代码中。
简而言之:您自己的代码必须不知道 _Tree
存在的事实。