【发布时间】:2012-04-05 20:41:13
【问题描述】:
我正在使用 C++ 映射在我的程序中实现字典。我的函数获取一个结构作为参数,并且应该返回基于structure.name 成员的关联值,即char named[32]。以下代码演示了我的问题:
map <const char *, const char *> myMap;
myMap.insert(pair<const char *, const char *>("test", "myTest"));
char *p = "test";
char buf[5] = {'\0'};
strcpy(buf, "test");
cout << myMap.find(p)->second << endl; // WORKS
cout << myMap.find("test")->second << endl; // WORKS
cout << myMap.find(buf)->second << endl; // DOES NOT WORK
我不确定为什么第三种情况不起作用,我应该怎么做才能使它起作用。 我调试了上面的代码来观察传递的值,我仍然无法解决问题。
谢谢!
【问题讨论】: