#unordered map not working

20 messages · Page 1 of 1 (latest)

old light
#

Hi, how do I create an unordered map and modify the values? I've tried google, though none of the solutions worked. I'm trying to create a unordered map with all values in a string initialized to 0. Then I want to add 1 for every count of the letter. Any help is appreciated Thanks!

Code:

    unordered_map<string, int> letter_count, needle_count;
    for (int i = 0; i < H.size(); i++)
    {
        letter_count.emplace(H[i], 0).first->second = 0;
        needle_count.emplace(H[i], 0).first->second = 0;
    }

    for (int i = 0; i < N.size(); i++)
    {
        letter_count[H[i]] += 1;
        needle_count[H[i]] += 1;
    }
Error Given:
error: no match for 'operator[]' (operand types are 'std::unordered_map<std::__cxx11::basic_string<char>, int>' and '__gnu_cxx::__alloc_traits<std::allocator<char>, char>::value_type' {aka 'char'})|
iron siloBOT
#

When your question is answered use !solved to mark the question as resolved.

Remember to ask specific questions, provide necessary details, and reduce your question to its simplest form. For tips on how to ask a good question run !howto ask.

old light
#

unordered map not working

river quail
#

@old light You've created an unordered map of string keys to integer values

#

But you're emplacing single characters (which might have weird implications with std::string's constructor) and then indexing with single characters

#

That's what the error message means, there's no operator[] for std::unordered_map<std::__cxx11::basic_string<char>, int> that takes a single char as an argument

old light
#

So I should just use char instead of string?

river quail
#

Try it and see

old light
#

ok

river quail
#

After you play around with it a little bit, here's my suggestion for how to do this: Just do:

    unordered_map<char, int> letter_count, needle_count;
    for(char c : H) {
        letter_count[H[i]] += 1;
        needle_count[H[i]] += 1;
    }
```When `std::unordered_map::operator[]` is called with a key that doesn't exist it inserts a default-constructed value into the map, so this should just work
old light
#

Nice Thanks! I think that works. btw is there a way to reset all the values back to zero as well or do I just have to loop through and set it individually?

river quail
#

You can do

for(auto& entry : letter_count) {
    entry->second = 0;
}
#

Now, since your map has a relatively small set of keys, less than 128 in practice, an std::unordered_map is probably overkill here

#

A normal array could be used. But you're welcome to use the map if you want, it's probably more expressive

old light
#

Maybe. I was just trying to translate my python code as best as I could

river quail
#

That's as good a reason as any 🙂

old light
#

Anyways, Thanks for all the help!

river quail
#

My pleasure, let us know if you have any more questions

iron siloBOT
#

@old light Has your question been resolved? If so, run !solved :)

old light
#

!solved