简体   繁体   中英

Map data structure in C++

I need to create a map of vectors. The first vector contains a list of strings and the second vector contains a list of integer IDs. Is this possible?

std::map<std::string, int> isThisWhatYouNeed; //?
std::map<std::vector<std::string>, std::vector<int> > orThis; //?

You might want to look at Boost Variant (see http://www.boost.org ). Then use:

std::map<std::string, boost::variant<std::vector<string>, std::vector<int>>>

If, on the other hand, you're trying to map from one vector to another, then you can do that by providing a custom sorting predicate for vectors of the key type when you construct the map.

If I understand your question correctly, you want a map of key=>vector, where one vector contains strings and the other ints?

You can't mix types like that. The closest you can do is create a map of key=>CustomClass where your custom class contains a vector of strings and a vector of ints, and both are populated.

Alternatively, if you want to be very clever and potentially create a massive debugging headache for you, you could do a map to void*, and manually manage what each pointer points to :)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM