[英]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.