简体   繁体   English

从基于迭代器的 for 循环转换后,如何在 map::find() 中调用方法?

[英]How do I call method inside map::find() after converting from iterator-based for-loop?

I would like to convert my existing for-loop map to use map.find().我想将现有的 for 循环 map 转换为使用 map.find()。

As seen below the WeatherTree.search checks against the year variable but WeatherMap also checks against the same year variable.如下所示, WeatherTree.search会检查年份变量,但WeatherMap也会检查相同的年份变量。

The below portion of the map is found in Menu.cpp. map 的以下部分可在 Menu.cpp 中找到。

    if(WeatherTree.search(year))
    {
        for (itr = WeatherMap.begin(); itr != WeatherMap.end(); itr++)
        {
            if(itr->first == year)
            {
                for(auto itr2 = itr->second.begin(); itr2 != itr->second.end(); itr2++)
                {
                    if(itr2->GetMonth() == month)
                    {
                        wind += itr2->GetWind();
                        temperature += itr2->GetTemperature();
                        count++;
                    }
                }
            }
        }
    }

What I would like to do instead is WeatherTree.search continue to check against year variable while WeatherMap using find() check against month.我想做的是WeatherTree.search继续检查年份变量,而WeatherMap使用find()检查月份。 WeatherTree is a binary search tree (BST) but it is not important as my question is about STL map. WeatherTree是二叉搜索树 (BST),但它并不重要,因为我的问题是关于 STL map。 The WeatherMap is also index using month rather than year. WeatherMap也是使用月份而不是年份的索引。

But how do I call the GetWind() and GetTemperature() and also what about the count which will be use for calculating average later.但是我如何调用GetWind()GetTemperature()以及稍后将用于计算平均值的计数。

I have Google they mostly return what the cpp documentation is showing which is not I wanted.我有谷歌,他们大多返回 cpp 文档显示的内容,这不是我想要的。

WeatherMap[W.GetMonth()].insert(W);

if(WeatherTree.search(year))
{
    auto itr = WeatherMap.find(month);
    if (itr != WeatherMap.end())
    {
        wind +=
        temperature +=
        count++;
    }
}

Menu.cpp菜单.cpp

#include "Menu.h"

using namespace std;

Menu::Menu()
{
    Load();
}

void Menu::Load()
{
    ifstream ifile;
    string filename, line, smonth, syear, swind, stemperature, shumidity;
    cout << "Enter the filename to open" << endl;
    getline(cin, filename);
    ifile.open(filename);
    if (!ifile.is_open())
    {
        cout << "Please try again!" << endl;
        system("PAUSE");
        exit(EXIT_FAILURE);
    }
    else
    {
        getline(ifile, line);
        while (getline(ifile, line))
        {
            if (!line.empty())
            {
                istringstream istream(line);
                getline(istream, smonth, ',');
                getline(istream, syear ',');
                getline(istream, swind, ',');
                getline(istream, shumidity, ',');
                getline(istream, stemperature, '\n');

                int month = stoi(smonth);
                int year = stoi(syear);
                double wind = stod(swind);
                int humidity = stoi(shumidity);
                double temperature = stod(stemperature);

                Weather W;
                W.SetWeather(month, year, wind, humidity, temperature);
                WeatherMap[W.GetYear()].insert(W);
                WeatherTree.insert(W.GetYear());
            }
        }
        ifile.close();
    }
}

void Menu::Options()
{
    int options = 0;

    while(options != 3)
    {
        cout << "1) Average wind speed and temperature of a month and year" << endl;
        cout << "2) Average humidity of a year" << endl;
        cout << "3) Quit" << endl;
        cin >> options;

        switch(options)
        {
            case 1:
                FirstOption();
                break;
            case 2:
                SecondOption();
                break;
            case 3:
                cout << "Goodbye!" << endl;
                break;
            default:
                cout << "Invalid input" << endl;
                break;
        }
    }
}

void Menu::FirstOption()
{
    int month = 0;
    int year = 0;
    int count = 0;
    double wind = 0.0;
    double temperature = 0.0;
    Weather W2;
    map<int, set<Weather>>::iterator itr;

    cout << "Enter month" << endl;
    cin >> month;
    cout << "Enter year" << endl;
    cin >> year;

    if(WeatherTree.search(year))
    {
        for (itr = WeatherMap.begin(); itr != WeatherMap.end(); itr++)
        {
            if(itr->first == year)
            {
                for(auto itr2 = itr->second.begin(); itr2 != itr->second.end(); itr2++)
                {
                    if(itr2->GetMonth() == month)
                    {
                        wind += itr2->GetWind();
                        temperature += itr2->GetTemperature();
                        count++;
                    }
                }
            }
        }
    }

    if (wind != 0 || temperature != 0 || wind < 0 || temperature < 0)
    {
        cout << W2.GetAvg(wind, count) << "km/h" << W2.GetAvg(temperature, count) << "degree celsius" << endl;
    }
    else
    {
        cout << "No information" << endl;
    }
}

Weather.cpp天气.cpp

#include "Weather.h"

using namespace std;

Weather::Weather()
{
    wind = 0;
    humidity = 0;
    temperature = 0;
}

int Weather::GetMonth()
{
    return month;
}

int Weather::GetYear()
{
    return year;
}

double Weather::GetWind()
{
    return wind;
}

int Weather::GetHumidity()
{
    return humidity;
}

double Weather::GetTemperature()
{
    return temperature;
}

void Weather::SetMonth(int m)
{
    month = m;
}

void Weather::SetYear(int y)
{
    year = y;
}

void Weather::SetWind(double w)
{
    wind = w;
}

void Weather::SetHumidity(int h)
{
    humidity = h;
}

void Weather::SetTemperature(double t)
{
    temperature = t;
}

void Weather::SetWeather(int m, int y, double w, int h, double t)
{
    month = m;
    year = y;
    wind = w;
    humidity = h;
    temperature = t;
}

double Weather::GetAvg(double total, int count)
{
    return total / count;
}

First your should know map::find() works by comparing keys of the map to a given query.首先,您应该知道map::find()通过将 map 的键与给定查询进行比较来工作。 So you can't using find() to compare months since month is not the key of you map.所以你不能使用find()来比较月份,因为月份不是你 map 的关键。

Note: Possibly you don't need WeatherTree since you are using year as a key saving Weather records.注意:您可能不需要WeatherTree ,因为您使用year作为保存天气记录的关键。

What you can do:你可以做什么:

Method 1:方法一:

Keep your current map definition not changed.保持当前 map 定义不变。 And iterate in the corresponding Weather set once you are sure the year exists.一旦确定存在年份,就在相应的 Weather 集中进行迭代。

    if(WeatherMap.find(year) != WeatherMap.end())
    {
        for (auto itr = WeatherMap[year].begin(); itr != WeatherMap[year].end(); itr++)
        {
           if(itr->GetMonth() == month)
           {
               wind += itr->GetWind();
               temperature += itr->GetTemperature();
               count++;
            }
        }
    }

Method 2方法二

Change your map to map<<int, int>, W> , ie, using both year and month as a key.将您的 map 更改为map<<int, int>, W> ,即使用年份和月份作为键。 Then you have:然后你有:

     if (WeatherMap.find(make_pair(year, month)) != WeatherMap.end()) {
         // calculate
     }

There is a while that I do not program in C++, but this could be a solution (I did not test it):有一段时间我没有在 C++ 中编程,但这可能是一个解决方案(我没有测试过):

std::for_each(WeatherMap.begin(), WeatherMap.end(), [&](const auto& value) {
    if ( value.first == year ) {
        auto it1 = value.second.begin();
        while( (it1 = std::find_if(it1, value.second.end(), [&](const auto& item) {
                if ( item.GetMonth() == month ) {
                    ...
                    return true;
                }
                return false;
                })) != value.second.end()
              )
              it1++;
    }
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何将基于迭代器的通用算法与基于实现的算法结合在一起? - How can I combine generic iterator-based algorithms with implementation based algorithms? 在C ++中使用基于范围的for循环时,如何告诉它使用哪个迭代器 - When using the range based for-loop in c++, how do I tell it which iterator to use 如何在基于范围的 for 循环中找出此地图的类型? - How do I find out the type of this map in a range based for loop? 如何使用迭代器值创建地图? - How do I create a map with iterator values? C ++迭代器,使用set find方法后是否需要重置 - C++ iterator, do I need to reset after using set find method 以基于迭代器的方式学习C ++:很好的获得背景知识,但是对获得工作是否实际? - Learning C++ in an iterator-based fashion: Good for gaining a background, but is it practical for gaining a job? 如何从在循环内使用迭代器的函数内部具有 getter 的类中检索特定成员? - How can I retrieve a specific member from a class with a getter inside of a function that uses an iterator inside of a loop? 如何正确地从 do 循环转换为 for 循环? - How to properly convert from a do-loop to a for-loop? 在 STL 关联容器中使用基于迭代器的搜索和使用是否更快? - Is it faster to use iterator-based search-and-use in STL associative containers? 在我的for循环中,如何将索引更改为字符串? - In my for-loop, how do I change the index to a string?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM