samedi 27 juin 2015

Print std::multimap keys and values alphabetically

I need to print out std::multimap alphabetically, both the authors names and their works.

#include <string>
#include <map>

int main()
{
    std::multimap<std::string, std::string> authors = {{"Captain", "Nothing"}, {"ChajusSaib", "Foo"}, 
                                                        {"ChajusSaib", "Blah"}, {"Captain", "Everything"}, {"ChajusSaib", "Cat"}};

    for (const auto &b : authors)
    {
        std::cout << "Author:\t" << b.first << "\nBook:\t\t" << b.second << std::endl;
    }

    return 0;   
}

This prints out the authors names but not their works alphabetically, any idea on how I could print their works alphabetically as well. Thanks!

Aucun commentaire:

Enregistrer un commentaire