Associative Array/Dictionary in C++?

Hello, Python experienced and new at C++ user here.

I've a function, and need to return usernames and their passwords (well, their hashes to be precise). Anyway, in Python I'd have something like this

 
x = {"A":"ASDF", "B":"DESU", "C":"BLAH"}


How do I make this Dictionary/Associative Array in C++? Thanks!
Last edited on
That's called std::map (or std::multimap if multiple keys are allowed, or std::unordered_map if you want a hash table, etc. You really should look at a reference or a textbook, I think)

std::map<std::string, std::string> x = {{"A", "ASDF"}, {"B", "DESU"}, {"C", "BLAH"}};
online demo: http://ideone.com/J8eeIb

(for older compilers, same caveat as with vectors: either add them one by one, x["A"] = "ASDF"; etc, or build from an array of pairs)
That's called std::map (or std::multimap if multiple keys are allowed, or std::unordered_map if you want a hash table, etc. You really should look at a reference or a textbook, I think)

std::map<std::string, std::string> x = {{"A", "ASDF"}, {"B", "DESU"}, {"C", "BLAH"}};
online demo: http://ideone.com/J8eeIb

(for older compilers, same caveat as with vectors: either add them one by one, x["A"] = "ASDF"; etc, or build from an array of pairs)


Thanks again Cubbi!
Topic archived. No new replies allowed.