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



我想将现有的for循环映射转换为使用map.find((.

如下所示,WeatherTree.search检查年份变量,但WeatherMap也检查同一年份变量。

地图的以下部分可在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++;
}
}
}
}
}

相反,我想做的是WeatherTree.search继续检查年份变量,而WeatherMap使用find()检查月份。WeatherTree是一个二进制搜索树(BST(,但它并不重要,因为我的问题是关于STL映射的。WeatherMap也是使用月份而不是年份的索引。

但是我如何调用GetWind()GetTemperature(),以及稍后用于计算平均值的计数

我有谷歌,他们大多返回cpp文档显示的内容,这不是我想要的。

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

菜单.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;
}
}

天气.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;
}

首先,您应该知道map::find()的工作原理是将映射的键与给定的查询进行比较。所以你不能用find()来比较月份,因为月份不是你映射的关键。

注意:可能您不需要WeatherTree,因为您使用year作为保存天气记录的密钥

你能做什么:

方法1:

保持当前地图定义不变。一旦确定年份存在,就在相应的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++;
}
}
}

方法2

将地图更改为map<<int, int>, W>,即使用年份和月份作为关键字。然后你有:

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

有一段时间我没有用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++;
}
}

最新更新