Search code examples
stdmapboost-tuples

Map lookup using boost::tuple as key


I would like to understand how a key in the form of boost::tuple is looked up in the map, where Compare=std::less. For instance, this is the snippet of the code I'm working on:

typedef boost::tuple<std::string, std::string> Key;
void *Data;

typedef std::map<Key, Data> FileDataMap;
FileDataMap file_map;

lookup_data(std::string s1, std::string s2)
{
    ...
    fk = boost::make_tuple(s1, s2);

    FileDataMap::iterator itr = file_map.find(fk);
    ...
    ...
}

insert_data(std::string s1, std::string s2, void *fdata)
{
    ...
    fk = boost::make_tuple(s1, s2);
    file_map.insert(std::make_pair(fk, fdata));
    ...
    ...
}

At the time of inserting a value into the map, let's suppose s1 is abc and s2 is xyz. During lookup, how is the key match determined?

Is a string comparison of s1 and s2 done with abc and xyz respectively, individually? If so, are the std::string comparison operators used?

Thanks!


Solution

  • I wrote code to test this out. Testing indicates that the strings are indeed compared individually using the defined comparison operators.

    The document talks on similar lines: http://en.cppreference.com/w/cpp/utility/tuple/operator_cmp