#include <iostream>
#include <map>
#include <deque>
#include <string>
#include <algorithm>
int main()
{
const std::string my_strings[] =
{
"thing1", "abcd", "accc", "super",
"consider", "using", "the", "standard", "library",
"it", "makes", "life", "a", "lot", "easier"
};
// http://w...content-available-to-author-only...s.com/reference/map/map/
// http://w...content-available-to-author-only...s.com/reference/deque/deque/
std::map< char, std::deque<std::string> > dict ;
// add the strings to the dictionary in any order
// http://w...content-available-to-author-only...p.com/C++11FAQ.html#for
for( const std::string& str : my_strings )
if( !str.empty() ) dict[ str[0] ].push_back(str) ;
// http://w...content-available-to-author-only...p.com/C++11FAQ.html#auto
for( auto& pair : dict ) // sort strings under each alphabet
{
auto& deque = pair.second ;
// http://w...content-available-to-author-only...s.com/reference/algorithm/sort/
std::sort( std::begin(deque), std::end(deque) ) ;
}
for( const auto& pair : dict ) // print out the contemts
{
std::cout << "key: '" << pair.first << "' values: [ " ;
for( const std::string& str : pair.second )
std::cout << "'" << str << "' " ;
std::cout << "]\n" ;
}
}
I2luY2x1ZGUgPGlvc3RyZWFtPgojaW5jbHVkZSA8bWFwPgojaW5jbHVkZSA8ZGVxdWU+CiNpbmNsdWRlIDxzdHJpbmc+CiNpbmNsdWRlIDxhbGdvcml0aG0+CgppbnQgbWFpbigpCnsKICAgIGNvbnN0IHN0ZDo6c3RyaW5nIG15X3N0cmluZ3NbXSA9CiAgICB7CiAgICAgICAgInRoaW5nMSIsICJhYmNkIiwgImFjY2MiLCAic3VwZXIiLAogICAgICAgICJjb25zaWRlciIsICJ1c2luZyIsICJ0aGUiLCAic3RhbmRhcmQiLCAibGlicmFyeSIsCiAgICAgICAgIml0IiwgIm1ha2VzIiwgImxpZmUiLCAiYSIsICJsb3QiLCAiZWFzaWVyIgogICAgfTsKCiAgICAvLyBodHRwOi8vdy4uLmNvbnRlbnQtYXZhaWxhYmxlLXRvLWF1dGhvci1vbmx5Li4ucy5jb20vcmVmZXJlbmNlL21hcC9tYXAvCiAgICAvLyBodHRwOi8vdy4uLmNvbnRlbn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