#include <unordered_map>
#include <iostream>
#include <string>
#include <vector>
#include <utility>
#include <algorithm>
using namespace std;
unordered_map <string, int> setupDictionary(vector<string> book)
{
unordered_map<string, int> table;
for (int i =0;i< book.size(); i++)
{
string word = book[i];
if(word != "")
{
if (table.find(word)==table.end())
{
std::pair<std::string,int> myshopping (word,0);
table.insert(myshopping);
}else
{
int num = table[word];
std::pair<std::string,int> myshopping (word,num+1);
table.insert(myshopping );
}
}
}
return table;
}
int main()
{
vector<string> book;
book[1] = "hello";
book[2] = "world";
book[3] = "hello";
book[4] = "world2";
unordered_map < string, int> dict= setupDictionary(book);
// printf("%s,%d",dict["hello"]);
}
编译和构建是好的。但是在我运行它之后,我得到了分段错误。需要帮助 真的不知道我的代码有什么问题。真的谢谢你!