【问题标题】:How to print out a found element from a STL List inside of a map?如何从地图内的 STL 列表中打印出找到的元素?
【发布时间】:2020-08-23 11:40:00
【问题描述】:
void Cinema::movieRunningAt(Movie& m, std::list<int>& movieList)
{

    map<const Movie*, std::list<int>>::iterator mov_it;
    mov_it = movie_times.find(&m);

    if (mov_it == movie_times.end())
    {
        cout << "No movie was found" << endl;
        return;
    }
    cout << mov_it->second << endl;
}

second 是一个列表,那么如何将找到的元素打印出来呢?

cout << mov_it->second << endl;

?

【问题讨论】:

  • 那张地图非常非常很奇怪,它会将 Movie 指针映射到列表中的整数。
  • 已经说过 ->second 是一个列表 你不能只计算一个列表。您必须从列表中选择一些元素

标签: c++ c++11


【解决方案1】:

mov_it->second 是一个列表容器打印它的元素需要再次遍历
示例如下

map<int, list<int>> mapContainer{ {1, {1,2,3}}, {2, {2, 3, 4}} };
map<int, list<int>>::iterator it = mapContainer.find(1);
if (it != mapContainer.end())
{
    for (int element : it->second)
    {
        cout << element << endl;
    }
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-10-11
    • 1970-01-01
    • 2020-09-25
    • 1970-01-01
    • 2012-02-07
    • 1970-01-01
    相关资源
    最近更新 更多