【问题标题】:how to access vector of map of ( int and vector of strings )如何访问映射的向量(int和字符串向量)
【发布时间】:2016-05-30 17:46:12
【问题描述】:

如何在passed_vector 函数中访问int 的映射和字符串的向量。 我只想在那个函数中打印它们。

#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.


}

我知道如何在主函数中打印它们。

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;
    }
}

期望的输出 440:西蒙·布拉德·施密特·菲齐奥 520:瑞奇·约翰·大卫(Ricky John David)

如果有更好的方法,欢迎随时提出建议。

【问题讨论】:

  • 你能使用 C++11 的特性吗(for (const auto&amp; meeting: meeting_info))?
  • 我没用过。这是我第一次使用 STL,所以我对此一无所知。

标签: c++ vector stl


【解决方案1】:

最简单的方法是使用auto,因为你的meetings是const,你需要使用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";
      }
    }
  }

}

[编辑]

c++98版本:

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";
      }
    }
  }
}

【讨论】:

  • 这是有效的。你能推荐一些学习STL的好书吗
  • @bluelight 我不知道只有适合初学者的 STL 书籍,你可以在这里找到好书:stackoverflow.com/questions/388242/…。如果您觉得我的回答有用,请考虑接受它。
  • 上述解决方案有效,但我的工作场所使用 c++98,那么在 c++98 中是否有替代方法?
  • @bluelight 当然 - 替换所有自动和每个循环,我有更新答案
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-11-05
  • 1970-01-01
  • 2019-02-07
  • 1970-01-01
相关资源
最近更新 更多