C++ 通过函数插入无序地图的正确方法
假设我有这样的东西:C++ 通过函数插入无序地图的正确方法,c++,C++,假设我有这样的东西: std::unordered_map<std::string, std::string> *map; void myFunc(std::string& s, std::string& s2){ map->insert(std::make_pair(s, s2)); } 我一辈子都不能按照这些思路来编译一些东西。很高兴发布更多代码。但是我已经将我的映射类型从string改为int,这没有问题。谢谢 使用指向地图的指针有什么原因吗?
std::unordered_map<std::string, std::string> *map;
void myFunc(std::string& s, std::string& s2){
map->insert(std::make_pair(s, s2));
}
我一辈子都不能按照这些思路来编译一些东西。很高兴发布更多代码。但是我已经将我的映射类型从string改为int,这没有问题。谢谢 使用指向地图的指针有什么原因吗?我注意到了一些奇怪的问题。确保地图确实已创建。但以下操作有效(无指针):
#包括
#包括
#包括
#包括
使用名称空间std;
课堂测试
{
公众:
显式测试(){}
内联void myFunc(常量字符串&s、常量字符串&s2){
map.insert(make_pair(s.c_str(),s2.c_str());
}
内联void Print()常量{
用于(常数自动输入:映射)
你不知道你得到了什么编译器错误?你的问题和这些细节。a)发布a.b)你为什么问一些你没有问题的问题?我的道歉,这些评论隐藏在哪里。我遇到了散列错误。做了一些进一步的挖掘,答案是我没有包括在内。悲伤的生活。从我加入cpp以来已经有一段时间了。d:Dc_str,不是吗应该需要这样的建议
#include <iostream>
#include <memory>
#include <unordered_map>
#include <string>
using namespace std;
class Test
{
public:
explicit Test() { }
inline void myFunc(const string& s, const string& s2) {
map.insert(make_pair<string, string>(s.c_str(), s2.c_str()));
}
inline void Print() const {
for (const auto& entry : map)
cout << entry.first << ", " << entry.second << endl;
}
private:
unordered_map<string, string> map;
};
int main()
{
unique_ptr<Test> test = make_unique<Test>();
test->myFunc("Key", "Value");
test->Print();
getchar();
}