在 unordered_map 上寻找价值
Find value on unordered_map
所以一开始我有一个 unordered_map 以 int 作为键,字符串作为值,用 256 个 ASCII 字符初始化,但后来我发现自己需要在字典中搜索字符串但是我无法使用 map::find 因为我的密钥是 int 。
所以我的问题是如何在无序地图中搜索特定值?或者我应该如何修改此方法以初始化 unordered_map 中的所有 ASCII 字符,其中键是字符串并且我可以使用 map::find?
void InitDico (unordered_map<int,string>& Dico){
Dico.clear();
for (int i = 0; i < 256; ++i)
{
Dico[i] = (char)i;
}}
我尝试了什么:
void InitDico (unordered_map<string,int>& Dico){
Dico.clear();
for (int i = 0; i < 256; ++i)
{
Dico.insert({(char)i , i});
}}
所以 tanks to super 我做到了,我犯了一个非常愚蠢的错误....所以这是代码:
void InitDico2(unordered_map<string,int>& Dico) {
Dico.clear();
string s ="";
char c;
for (int i = 0; i < 256; i++)
{
c = (char)i;
s += c;
Dico[s] = i;
s.clear();
}}
std::string
有一个构造函数,它接受一个计数和一个 char
,并构造一个 char
的计数重复字符串。
It is implementation-defined whether a char
object can hold negative values.
void InitDico2(std::unordered_map<std::string,int>& Dico) {
Dico.clear();
for (unsigned char c = 0; c < 256; c++)
{
Dico[{1, c}] = c;
}
}
所以一开始我有一个 unordered_map 以 int 作为键,字符串作为值,用 256 个 ASCII 字符初始化,但后来我发现自己需要在字典中搜索字符串但是我无法使用 map::find 因为我的密钥是 int 。 所以我的问题是如何在无序地图中搜索特定值?或者我应该如何修改此方法以初始化 unordered_map 中的所有 ASCII 字符,其中键是字符串并且我可以使用 map::find?
void InitDico (unordered_map<int,string>& Dico){
Dico.clear();
for (int i = 0; i < 256; ++i)
{
Dico[i] = (char)i;
}}
我尝试了什么:
void InitDico (unordered_map<string,int>& Dico){
Dico.clear();
for (int i = 0; i < 256; ++i)
{
Dico.insert({(char)i , i});
}}
所以 tanks to super 我做到了,我犯了一个非常愚蠢的错误....所以这是代码:
void InitDico2(unordered_map<string,int>& Dico) {
Dico.clear();
string s ="";
char c;
for (int i = 0; i < 256; i++)
{
c = (char)i;
s += c;
Dico[s] = i;
s.clear();
}}
std::string
有一个构造函数,它接受一个计数和一个 char
,并构造一个 char
的计数重复字符串。
It is implementation-defined whether a char
object can hold negative values.
void InitDico2(std::unordered_map<std::string,int>& Dico) {
Dico.clear();
for (unsigned char c = 0; c < 256; c++)
{
Dico[{1, c}] = c;
}
}