-3

how do i access map of int and vectors of string in the passed_vector function. I just want to print them in that function.

#include <iostream>
#include <vector>
#include <map>
#include <string>

using namespace std; 
typedef vector< map< int, vector<string> > > vmis;
typedef map< int, vector<string> > mis;
typedef vector<string> vstr;

void passing_vector(const vmis &meetings);

//return size of vector

template< typename A >  size_t n_elements( const A& a )
{
   return sizeof a / sizeof a[ 0 ];
}

int main()
{
    vmis meeting_info;
    mis meeting_members;
    vstr sw_vec;
    vstr sys_vec;
    string sw_team[] = {"Ricky", "John", "David"};
    string sys_team[] = {"Simmon", "Brad", "Schmidt", "Fizio"};

    sw_vec.insert(sw_vec.begin(), sw_team, sw_team + n_elements(sw_team) );
    sys_vec.insert(sys_vec.begin(), sys_team, sys_team + n_elements(sys_team) );

    meeting_members.insert(make_pair(520, sw_vec));
    meeting_members.insert(make_pair(440, sys_vec));

    meeting_info.push_back(meeting_members);

    passing_vector(meeting_info);

    return 0;
}

void passing_vector(const vmis &meetings)
{
    vmis::iterator itvmis = meetings.begin();

    //how do i access map of int and vectors of string.
    //I just want to print them.


}

I know how to print them in main function.

vmis::iterator itvims = meeting_info.begin();

for( int i = 0; i < meeting_info.size(); i++ )
{
    mis::iterator itm = meeting_members.begin();
    for(itm; itm != meeting_members.end(); itm++ )
    {
       cout << itm->first << " : ";
       vstr::iterator it = itm->second.begin();

       for(it; it != itm->second.end(); it++)
           cout << *it << " ";

       cout << endl;
    }
}

desired output 440 : Simmon Brad Schmidt Fizio 520 : Ricky John David

if there is a better way of doing this suggestions are always welcome.

bluelight
  • 1
  • 2

1 Answers1

1

The easiest aproach is to use auto, also since your meetings is const, you need to use const_iterator:

void passing_vector(const vmis &meetings)
{
  vmis::const_iterator itvims = meetings.begin();

  //how do i access map of int and vectors of string.
  //I just want to print them.
  for (;itvims != meetings.end(); ++itvims)
  {
    const auto& map_item = *itvims;
    for (const auto& map_it : map_item)
    {
      int map_key = map_it.first;
      const auto& str_vec = map_it.second;
      for (const auto& str : str_vec)
      {
        std::cout << map_key << " - " << str << "\n";
      }
    }
  }

}

[edit]

c++98 version:

void passing_vector(const vmis &meetings)
{
  vmis::const_iterator itvims = meetings.begin();

  //how do i access map of int and vectors of string.
  //I just want to print them.
  for (;itvims != meetings.end(); ++itvims)
  {
    const mis& map_item = *itvims;
    for (mis::const_iterator map_it = map_item.begin(); map_it != map_item.end(); ++map_it)
    {
      int map_key = map_it->first;
      const vstr& str_vec = map_it->second;
      for (vstr::const_iterator sitr = str_vec.begin(); sitr != str_vec.end(); ++sitr)
      {
        std::cout << map_key << " - " << *sitr << "\n";
      }
    }
  }
}
marcinj
  • 48,511
  • 9
  • 79
  • 100
  • This is working. Can you suggest some good books to learn STL – bluelight May 30 '16 at 23:42
  • @bluelight I am not aware of only STL books for beginners, you can find good books here: http://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list. If you find my answer usefull then consider accepting it. – marcinj May 31 '16 at 10:04
  • above solution is working but my workplace uses c++98, so is there a alternative way of doing it in c++98? – bluelight May 31 '16 at 13:32
  • @bluelight sure - replace all the auto and for each loop, I have updates answer – marcinj May 31 '16 at 14:17