C++ 中的 std::map 键
我需要在 C++ 中创建两个不同的地图。键的类型为 CHAR*,值是指向结构的指针。我在单独的迭代中用这些对填充 2 个地图。创建两个映射后,我需要找到所有此类实例,其中 CHAR* 引用的字符串值相同。
为此,我使用以下代码:
typedef struct _STRUCTTYPE
{
..
} STRUCTTYPE, *PSTRUCTTYPE;
typedef pair <CHAR *,PSTRUCTTYPE> kvpair;
..
CHAR *xyz;
PSTRUCTTYPE abc;
// after filling the information;
Map.insert (kvpair(xyz,abc));
// the above is repeated x times for the first map, and y times for the second map.
// after both are filled out;
std::map<CHAR *, PSTRUCTTYPE>::iterator Iter,findIter;
for (Iter=iteratedMap->begin();Iter!=iteratedMap->end();mapIterator++)
{
char *key = Iter->first;
printf("%s\n",key);
findIter=otherMap->find(key);
//printf("%u",findIter->second);
if (findIter!=otherMap->end())
{
printf("Match!\n");
}
}
上面的代码没有显示任何匹配,尽管两个映射中的键列表显示明显的匹配。我的理解是 CHAR * 的等于运算符只是等于指针的内存地址。
我的问题是,我应该做什么来改变这种类型的键的等于运算符,或者我可以为字符串使用不同的数据类型吗?
I have a requirement to create two different maps in C++. The Key is of type CHAR*
and the Value is a pointer to a struct. I am filling 2 maps with these pairs, in separate iterations. After creating both maps I need find all such instances in which the value of the string referenced by the CHAR*
are same.
For this I am using the following code :
typedef struct _STRUCTTYPE
{
..
} STRUCTTYPE, *PSTRUCTTYPE;
typedef pair <CHAR *,PSTRUCTTYPE> kvpair;
..
CHAR *xyz;
PSTRUCTTYPE abc;
// after filling the information;
Map.insert (kvpair(xyz,abc));
// the above is repeated x times for the first map, and y times for the second map.
// after both are filled out;
std::map<CHAR *, PSTRUCTTYPE>::iterator Iter,findIter;
for (Iter=iteratedMap->begin();Iter!=iteratedMap->end();mapIterator++)
{
char *key = Iter->first;
printf("%s\n",key);
findIter=otherMap->find(key);
//printf("%u",findIter->second);
if (findIter!=otherMap->end())
{
printf("Match!\n");
}
}
The above code does not show any match, although the list of keys in both maps show obvious matches. My understanding is that the equals operator for CHAR * just equates the memory address of the pointers.
My question is, what should i do to alter the equals operator for this type of key or could I use a different datatype for the string?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
你的理解是正确的。
最简单的方法是使用 std::string 作为键。这样,您就可以轻松比较实际的字符串值:
请注意,如果您不总是手动删除结构,则可能会泄漏结构的内存。如果无法按值存储,请考虑使用智能指针。
根据您的用例,您不必存储指向结构的指针:
最后一点:
typedef
您正在执行的方式定义结构是 C-ism,在 C++ 中,以下内容就足够了:Your understanding is correct.
The easiest thing to do would be to use
std::string
as the key. That way you get comparisons for the actual string value working without much effort:Note that you might leak memory for your structs if you don't always delete them manually. If you can't store by value, consider using smart pointers instead.
Depending on your usecase, you don't have to neccessarily store pointers to the structs:
A final note:
typedef
ing structs the way you are doing it is a C-ism, in C++ the following is sufficient:如果您使用 std::string 代替 char * ,您可以使用更方便的比较函数。另外,您可以使用 STL
set_intersection
算法(请参阅 此处了解更多详细信息)查找两个排序容器中的共享元素(std::map
当然是排序的)。这是一个例子If you use
std::string
instead of char * there are more convenient comparison functions you can use. Also, instead of writing your own key matching code, you can use the STLset_intersection
algorithm (see here for more details) to find the shared elements in two sorted containers (std::map
is of course sorted). Here is an example