0

I have unordered_map. which has client and their associated users in std::list. I can print my client, but don't know how to print its userslist.

mapType clientUserMap;

clientUserMap.insert (mapType::value_type("C1", std::list<std::string> (userlist)));

boost::unordered_map<std::string, std::list<std::string> >
         ::const_iterator it = clientUserMap.find("C1");

 std::cout << it->first << std::endl;

3 Answers 3

2

It's a list, so iterate through the list and print.. e.g.

for(std::list<std::string>::const_iterator l_it = begin(it->second); l_it != end(it->second); ++l_it)
  std::cout << *l_it << std::endl;

Of course there are many more fancy ways of doing this...

Sign up to request clarification or add additional context in comments.

2 Comments

When I tried I got the following errors warning: 'auto' will change meaning in C++0x; please remove it error: ISO C++ forbids declaration of 'l_it' with no type error: 'begin' was not declared in this scope error: 'end' was not declared in this scope error: invalid type argument of 'unary *'
@user1841046, I am using a keyword from c++11, please change it to match the iterator type of the list...
0

All you have to do is iterate over the list:

std::list<string> const &users = it->second;
std::for_each(users.begin(), users.end(), [](string const& user){std::cout << user << std::endl;}

Comments

0

as you already using boost, you could also use BOOST_FOREACH to iterate over the user list. The code would then look like this:

#include <boost/foreach.hpp>
...

boost::unordered_map<std::string, std::list<std::string> >
  ::const_iterator it = clientUserMap.find("C1");

std::cout << it->first << std::endl;

BOOST_FOREACH( std::string user, it->second )
{
   std::cout << user << endl;
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.