C++ 无序地图查找常量wchar\t*
我使用以下代码从文件路径获取文件名:C++ 无序地图查找常量wchar\t*,c++,find,unordered-map,C++,Find,Unordered Map,我使用以下代码从文件路径获取文件名: const wchar_t* MyClass::PathFindFileNameW(const wchar_t* path) { const wchar_t* p1 = path ? wcsrchr(path, L'\\') : nullptr; const wchar_t* p2 = path ? wcsrchr(path, L'/') : nullptr; p1 = !p1 || (p2 && p2 > p1) ? p2
const wchar_t* MyClass::PathFindFileNameW(const wchar_t* path)
{
const wchar_t* p1 = path ? wcsrchr(path, L'\\') : nullptr;
const wchar_t* p2 = path ? wcsrchr(path, L'/') : nullptr;
p1 = !p1 || (p2 && p2 > p1) ? p2 : p1;
return (p1 ? p1 + 1 : path);
}
我还有以下无序地图定义:
std::unordered_map<const wchar_t*,std::string> mymap = {
{L"file1.doc","Author1"},
{L"file2.doc","Author2"},
{L"file3.doc","Author3"} };
std::无序映射mymap={
{L“file1.doc”,“Author1”},
{L“file2.doc”、“Author2”},
{L“file3.doc”,“Author3”};
使用以下代码,我希望通过文件名从映射中获取作者:
std::unordered_map<const wchar_t*,std::string>::const_iterator got = mymap.find(this->PathFindFileNameW(this->path));
if (got == mymap.end())
{
Log("No result");
}
std::无序映射::常量迭代器get=mymap.find(this->PathFindFileNameW(this->path));
if(get==mymap.end())
{
日志(“无结果”);
}
即使映射中存在文件名,此代码也会记录“无结果”。类似于:
std::unordered_map<const wchar_t*,std::string>::const_iterator got = mymap.find(L"file1.doc");
std::无序映射::常量迭代器get=mymap.find(L“file1.doc”);
给出一个结果。我这里缺少什么?您有一个以指针作为键的映射,因此只有当字符串存储在与键相同的地址时,您才能找到它 使用
std::wstring
作为键