• C++ Programming for Financial Engineering
    Highly recommended by thousands of MFE students. Covers essential C++ topics with applications to financial engineering. Learn more Join!
    Python for Finance with Intro to Data Science
    Gain practical understanding of Python to read, understand, and write professional Python code for your first day on the job. Learn more Join!
    An Intuition-Based Options Primer for FE
    Ideal for entry level positions interviews and graduate studies, specializing in options trading arbitrage and options valuation models. Learn more Join!

Unordered_map Find Function

Hi ,
'
I am trying to compile the following code ,
C++:
std::unordered_map<char ,char> mappings = {{')', '('}, {'}' , '{'} , {']','['}};
        std::stack<char> Stack;
        for(int i = 0; i < s.length() ; ++i)
        {
            char c = s.at(i);
            if(mappings.find(c) != s.end()) // ERROR !!!
            {
            ...
            }
        }
I get the following error ,
Invalid operands to binary expression ('std::unordered_map<char, char>::iterator' (aka '__hash_map_iterator<__hash_iterator<std::__hash_node<std::__hash_value_type<char, char>, void *> *>>') and 'std::basic_string<char>::iterator' (aka '__wrap_iter<char *>'))

I think I am comparing the wrong iterators in the "if" loop but I don't see why ? Any help would be much appreciated.
Thank you :)
 
Hi ,
'
I am trying to compile the following code ,
C++:
std::unordered_map<char ,char> mappings = {{')', '('}, {'}' , '{'} , {']','['}};
        std::stack<char> Stack;
        for(int i = 0; i < s.length() ; ++i)
        {
            char c = s.at(i);
            if(mappings.find(c) != s.end()) // ERROR !!!
            {
            ...
            }
        }
I get the following error ,
Invalid operands to binary expression ('std::unordered_map<char, char>::iterator' (aka '__hash_map_iterator<__hash_iterator<std::__hash_node<std::__hash_value_type<char, char>, void *> *>>') and 'std::basic_string<char>::iterator' (aka '__wrap_iter<char *>'))

I think I am comparing the wrong iterators in the "if" loop but I don't see why ? Any help would be much appreciated.
Thank you :)
Yes, s.end() (which is the string) will never be the same iterator with the keyset in unordered. Compare it mappings.end() instead of s.end() will resolve this issue.
 
Back
Top