关于地图 STL 的问题
problems about map STL
void huffmanDecode(string str){
string temp;
map<string, char>::iterator it;
//for(auto iter=myMap.begin();iter!=myMap.end();++iter)
//cout<<iter->first<<" "<<iter->second<<" "<<endl;
for (unsigned int i = 0; i < str.size(); i++)
{
temp += str[i];
it = myMap.find(temp);
if (it == myMap.end())
continue;
else
{
cout<<it->first<<" ";//crashed here, "Thread 1:EXC_BAD_ACCESS(code=1,address=0x0)
//cout << it->second << " ";
temp = nullptr;
}
}
}
我正在尝试通过地图解决霍夫曼解码问题,但它崩溃了~~~
std::string::operator=
有一个需要const char*
的重载。这是当你说时使用的重载
temp = nullptr;
现在,要求是const char*
指向以 null 结尾的字符串。因此,它不能是空指针。不允许传递空指针,并且允许实现在这种情况下引发异常。在任何情况下,尝试使用此类字符串都会导致未定义的行为。std::string
构造函数也有类似的情况。
如果您打算将temp
重新设置为空字符串,则有以下几种选择:
temp = "";
temp.clear();
temp = std::string();
您已将temp
定义为std::string
,而不是指针。因此,将其设置为 nullptr
是错误的!
如果你想清除它的内容,我假设你真的想要,试试这个:
temp.clear();
相关文章:
- 在C++中迭代 STL 集时出现奇怪的问题<CStudent>
- 为什么C++中的大括号初始化可以解决STL容器的初始化问题
- 使用队列和列表 STL 进行C++分配。在将数据填充到列表或队列中然后打印该数据时遇到问题
- 是包含容器性能问题的STL关联容器
- 将一个STL向量有效地分配给另一个STL矢量(WSL问题)
- 链接模板与传递 stl 容器的问题
- C++ STL 地图:BSTR 的问题
- STL链接列表问题
- DEV-C++5.11 STL迭代器的调试问题
- C++:问题向量 STL.
- stl 堆栈和 top() 函数的问题
- 删除元素时使用 STL 映射的迭代器时遇到问题
- STL 映射和指针出现问题
- 关于地图 STL 的问题
- c++:stl哈希编译问题
- 关于C++中STL容器的问题
- 如果我使用函数指针作为一对的值,我会遇到stl::pair问题2051吗
- 在C++STL中使用men_fun的bind1st上的一个旧问题
- C++STL版本控制问题+Boost
- 自定义STL列表实现问题