Quick stl map question.

Started by
2 comments, last by Thergothon 17 years, 9 months ago
I'm storing settings and values in a map. At the end of the program I would like to be able to save out the current settings as they may have been changed. I figured it would be easy enough to create an iterator and print out the key and value, but I can't figure out how to get access to the value of the key.
Advertisement
stl::map iterators are pair iterators: it->first is the key and it->second is the value.
Elements in std::map are made of of both the key AND the value, using a std::pair to wrap them up. You can access it like this:

typedef std::map<std::string, std::string> settings_map;void outputSettings(const settings_map& settings){    // Start settings file    //...    // Output each setting    for(settings_map::const_iterator it=settings.begin() ; it!=settings.end() ; ++it)    {        const settings_map::key_type& key = it->first;        const settings_map::value_type& value = it->second;        // Output key & value    }    // Finish settings file    //...}

"Voilà! In view, a humble vaudevillian veteran, cast vicariously as both victim and villain by the vicissitudes of Fate. This visage, no mere veneer of vanity, is a vestige of the vox populi, now vacant, vanished. However, this valorous visitation of a bygone vexation stands vivified, and has vowed to vanquish these venal and virulent vermin vanguarding vice and vouchsafing the violently vicious and voracious violation of volition. The only verdict is vengeance; a vendetta held as a votive, not in vain, for the value and veracity of such shall one day vindicate the vigilant and the virtuous. Verily, this vichyssoise of verbiage veers most verbose, so let me simply add that it's my very good honor to meet you and you may call me V.".....V
it->first and it->second were what i was looking for. thanks guys.

[Edited by - Thergothon on June 26, 2006 9:07:05 AM]

This topic is closed to new replies.

Advertisement