C++ 中的 std::map 键

发布于 2024-08-28 05:10:43 字数 952 浏览 3 评论 0原文

我需要在 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

烈酒灼喉 2024-09-04 05:10:43

我的理解是,CHAR* 的等于运算符只是等于指针的内存地址。

你的理解是正确的。

最简单的方法是使用 std::string 作为键。这样,您就可以轻松比较实际的字符串值:

std::map<std::string, PSTRUCTTYPE> m;
PSTRUCTTYPE s = bar();
m.insert(std::make_pair("foo", s));

if(m.find("foo") != m.end()) {
    // works now
}

请注意,如果您不总是手动删除结构,则可能会泄漏结构的内存。如果无法按值存储,请考虑使用智能指针。

根据您的用例,您不必存储指向结构的指针:

std::map<std::string, STRUCTTYPE> m;
m.insert(std::make_pair("foo", STRUCTTYPE(whatever)));

最后一点:typedef您正在执行的方式定义结构是 C-ism,在 C++ 中,以下内容就足够了:

typedef struct STRUCTTYPE {
    // ...
} *PSTRUCTTYPE;

My understanding is that the equals operator for CHAR* just equates the memory address of the pointers.

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:

std::map<std::string, PSTRUCTTYPE> m;
PSTRUCTTYPE s = bar();
m.insert(std::make_pair("foo", s));

if(m.find("foo") != m.end()) {
    // works now
}

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:

std::map<std::string, STRUCTTYPE> m;
m.insert(std::make_pair("foo", STRUCTTYPE(whatever)));

A final note: typedefing structs the way you are doing it is a C-ism, in C++ the following is sufficient:

typedef struct STRUCTTYPE {
    // ...
} *PSTRUCTTYPE;
慕烟庭风 2024-09-04 05:10:43

如果您使用 std::string 代替 char * ,您可以使用更方便的比较函数。另外,您可以使用 STL set_intersection 算法(请参阅 此处了解更多详细信息)查找两个排序容器中的共享元素(std::map 当然是排序的)。这是一个例子

typedef map<std::string, STRUCTTYPE *> ExampleMap;  
ExampleMap inputMap1, inputMap2, matchedMap;

// Insert elements to input maps
inputMap1.insert(...);

// Put common elements of inputMap1 and inputMap2 into matchedMap
std::set_intersection(inputMap1.begin(), inputMap1.end(), inputMap2.begin(), inputMap2.end(), matchedMap.begin());

for(ExampleMap::iterator iter = matchedMap.begin(); iter != matchedMap.end(); ++iter)
{
    // Do things with matched elements
    std::cout << iter->first << endl;
}

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 STL set_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

typedef map<std::string, STRUCTTYPE *> ExampleMap;  
ExampleMap inputMap1, inputMap2, matchedMap;

// Insert elements to input maps
inputMap1.insert(...);

// Put common elements of inputMap1 and inputMap2 into matchedMap
std::set_intersection(inputMap1.begin(), inputMap1.end(), inputMap2.begin(), inputMap2.end(), matchedMap.begin());

for(ExampleMap::iterator iter = matchedMap.begin(); iter != matchedMap.end(); ++iter)
{
    // Do things with matched elements
    std::cout << iter->first << endl;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文