我有一个多图,它在其中存储日期(自定义类对象)和浮点值。然后,我通过为此映射创建迭代器来创建for循环,以执行一些操作。由于某些原因,我的it-> first中的日期值从第一个日期开始就不会更改。但是我在其他功能中使用了与之完全相同的方法,效果很好。可能是什么原因?
代码:
multimap<Date, float> mapOption5Solar;
multimap<Time, float> mapOption5Time;
Vector<Time> timeVec;
for(int i = 1; i < SIZE; i++)
{
mapOption5Solar.insert(make_pair(windlog[i].d, windlog[i].solar.GetSolar()));
mapOption5Time.insert(make_pair(windlog[i].t, windlog[i].solar.GetSolar()));
}
float maxVal = 0;
cout << "Original values: " << maxVal << endl;
for(std::multimap<Date, float>::iterator it = mapOption5Solar.begin(); it!=mapOption5Solar.end(); it++)
{
//cout << "Entered Date: " << entryDate << " Vector Date: " << it->first << endl;
//cout << "CheckDate Day: " << checkDate.GetDay() << endl;
//cout << "CheckDate Month: " << checkDate.GetMonth() << endl;
//cout << "CheckDate Year: " << checkDate.GetYear() << endl;
//cout << "it Day: " << it->first.GetDay() << endl;
//cout << "it Month: " << it->first.GetMonth() << endl;
//cout << "it Year: " << it->first.GetYear() << endl;
if(checkDate == it->first)
{
if(it->second > maxVal)
{
maxVal = it->second;
cout << "Just checking iterator value: " << it->second << endl;
}
}
}
cout << "Updated values: " << maxVal << endl;
CheckDate是我创建的日期对象,用户在其中输入日期。 it-> first应该存储我的日期对象,但对于所有迭代,仅打印出我输入的第一天的值。
最小可复制示例:
Date tempDate;
Date tempDate2;
Date tempDate3;
tempDate.SetDay("3");
tempDate.SetMonth("2");
tempDate.SetYear("2013");
tempDate2.SetDay("7");
tempDate2.SetMonth("1");
tempDate2.SetYear("2015");
tempDate3.SetDay("4");
tempDate3.SetMonth("2");
tempDate3.SetYear("2017");
//cout << "Date entered: " << checkDate << endl;
multimap<Date, float> mapOption5Solar;
mapOption5Solar.insert(make_pair(tempDate, 35));
mapOption5Solar.insert(make_pair(tempDate2, 56));
mapOption5Solar.insert(make_pair(tempDate3, 37));
float maxVal = 0;
Date checkDate;
checkDate.SetDay("4");
checkDate.SetMonth("2");
checkDate.SetDay("2017");
for(std::multimap<Date, float>::iterator it = mapOption5Solar.begin(); it!=mapOption5Solar.end(); it++)
{
//cout << "Entered Date: " << entryDate << " Vector Date: " << it->first << endl;
//cout << "CheckDate Day: " << checkDate.GetDay() << endl;
//cout << "CheckDate Month: " << checkDate.GetMonth() << endl;
//cout << "CheckDate Year: " << checkDate.GetYear() << endl;
//cout << "it Day: " << it->first.GetDay() << endl;
//cout << "it Month: " << it->first.GetMonth() << endl;
//cout << "it Year: " << it->first.GetYear() << endl;
if(checkDate == it->first)
{
if(it->second > maxVal)
{
maxVal = it->second;
cout << "Just checking iterator value: " << it->second << endl;
}
}
}
Date.h:
#if !defined(_DATE_H)
#define _DATE_H
#include <iostream>
#include <string>
#include <tuple>
using namespace std;
/**
* @class Date
* @brief Manages and holds all dates
*
* This class is used to convert string from the input file into numerical values
* and then store them as date values
* @author Darren Fernando
*
* @date 05/05/2020 Darren Fernando, Started
* @bug No bugs founds as of yet
*/
class Date
{
public:
Date();
Date(unsigned day1, unsigned month1, unsigned year1);
/**
* @brief A set method that is used to set the day value
* It takes a string parameter and calls another method to convert it, and then assign it to the variable day
* @param day1 - A string variable that is converted
* @return void
*/
void SetDay(string day1);
/**
* @brief A set method that is used to set the month value
* It takes a string parameter and calls another method to convert it, and then assign it to the variable month
* @param month1 - A string variable that is converted
* @return void
*/
void SetMonth(string month1);
/**
* @brief A set method that is used to set the year value
* It takes a string parameter and calls another method to convert it, and then assign it to the variable year
* @param year - A string variable that is converted
* @return void
*/
void SetYear(string year1);
/**
* @brief A get method that is used to get the day value by returning it
* @return unsigned
*/
unsigned GetDay() const;
/**
* @brief A get method that is used to get the month value by returning it
* @return unsigned
*/
unsigned GetMonth() const;
/**
* @brief A get method that is used to get the year value by returning it
* @return unsigned
*/
unsigned GetYear() const;
/**
* @brief A method that converts a string to an unsigned numerical value
* @param time - A string parameter that is converted
* @return unsigned
*/
unsigned convertString(string date) const;
/**
* @brief A method that takes an input stream and perfoms some operations to set the date from this stream
* This method isn't used but may be valuable later on
* @param input - An input stream
* @return void
*/
void SetDate(istream &input);
/**
* @brief A method that takes an output stream and outputs it
* This method isn't used but may be valuable later on
* @param os - An output stream
* @return void
*/
void GetDate(ostream & os) const;
unsigned GetDateRaw() const;
private:
int day; /// Variable to store the day
int month; /// Variable to store the month
int year; /// Variable to store the year
};
bool operator==(const Date& firstDate, const Date& secondDate);
bool operator>(const Date& firstDate, const Date& secondDate);
bool operator<(const Date& firstDate, const Date& secondDate);
bool operator!=(const Date& firstDate, const Date& secondDate);
ostream & operator <<(ostream & os, const Date & D); /// Operator << overload
istream & operator >>(istream & input, Date & D); /// Operator >> overload
#endif //_DATE_H
Date.cpp:
//
//
// Generated by StarUML(tm) C++ Add-In
#include "Date.h"
// Default constructor
Date::Date(){}
/* Date constructor to initialize an object of date with the passed parameters
*/
Date::Date(unsigned day1, unsigned month1, unsigned year1) {
day = day1;
month = month1;
year = year1;
}
void Date::SetDay(string day1) {
day = convertString(day1); // Calls method convertString to convert the string parameter and assign the new value to day.
}
void Date::SetMonth(string month1) {
month = convertString(month1); // Calls method convertString to convert the string parameter and assign the new value to month.
}
void Date::SetYear(string year1) {
year = convertString(year1); // Calls method convertString to convert the string parameter and assign the new value to year.
}
unsigned Date::GetDay() const {
return day; // Returns the day of the object
}
unsigned Date::GetMonth() const {
return month; // Returns the month of the object
}
unsigned Date::GetYear() const {
return year; // Returns the year of the object
}
/* This method is used for directly setting the date from a file. It is not used now,
but may prove handy in the future.*/
void Date::SetDate(istream &input){
string day1;
string month1;
string year1;
getline(input, day1, '/'); //Reads line until '/' is found and stores the string in day1
getline(input, month1, '/'); //Reads line until '/' is found and stores the string in month1
getline(input, year1, ' '); //Reads line until ' ' is found and stores the string in year1
/*Sets the strings with setters which convert them automatically
*/
SetDay(day1);
SetMonth(month1);
SetYear(year1);
}
//This method is designed to convert a string to an integer
inline unsigned Date::convertString(string date) const{
int date2 = 0;
date2 = stoi(date); //Uses stoi from cmath library to convert a string to an integer
return date2;
}
// A get function that uses the overloaded output stream to output the date in a specific format.
void Date::GetDate(ostream &os) const{
os << GetDay() << "/" << GetMonth() << "/" << GetYear() << " ";
}
bool operator==(const Date& firstDate, const Date& secondDate)
{
if(firstDate.GetYear() == secondDate.GetYear() && firstDate.GetMonth() == secondDate.GetMonth() && firstDate.GetDay() == secondDate.GetDay())
{
return true;
}
else
{
return false;
}
}
/*bool operator!=(const Date& firstDate, const Date& secondDate)
{
if(firstDate!=secondDate)
{
return true;
}
return false;
}*/
bool operator>(const Date& firstDate, const Date& secondDate)
{
if(firstDate.GetYear() > secondDate.GetYear())
{
return true;
}
else
if(firstDate.GetYear() < secondDate.GetYear())
{
return false;
}
else
{
if(firstDate.GetMonth() > secondDate.GetMonth())
{
return true;
}
else
if(firstDate.GetMonth() < secondDate.GetMonth())
{
return false;
}
else
{
if(firstDate.GetDay() > secondDate.GetDay())
{
return true;
}
else
{
return false;
}
}
}
}
// Input stream operator overload
istream & operator >>( istream & input, Date & D) {
D.SetDate(input);
return input;
}
// Output stream operator overload
ostream & operator <<( ostream & os, const Date & D) {
D.GetDate(os);
return os;
}
由于您的代码(经过修改以适应丢失的信息)可以正常工作,所以我只能认为您的问题在于您正在填充多图with。
具体来说,代码:
#include <iostream>
#include <map>
using namespace std;
int main() {
multimap<int, float> mapOption;
for(int i = 1; i < 5; i++) {
mapOption.insert(make_pair(i, i / 100.0));
}
auto maxVal = 0.0f;
cout << "Original values: " << maxVal << endl;
for(auto it = mapOption.begin(); it != mapOption.end(); it++) {
if(it->second > maxVal) {
maxVal = it->second;
cout << "Just checking iterator value: " << it->second << endl;
}
}
cout << "Updated values: " << maxVal << endl;
}
输出正确的项目没有问题:
Original values: 0
Just checking iterator value: 0.01
Just checking iterator value: 0.02
Just checking iterator value: 0.03
Just checking iterator value: 0.04
Updated values: 0.04
因此您可能应该通过以下方式转储项目:[[进入多图,[>],例如:for(int i = 1; i < SIZE; i++) {
auto one = windlog[i].d;
auto two = windlog[i].solar.GetSolar();
cout << "Inserting " << one << " : " << two << '\n';
mapOption5Solar.insert(make_pair(one, two));
}
对于您更新的代码,我可以看到不起作用的原因(除了它实际上不会以当前形式编译的事实之外。)
one
您限制使用checkDate
打印的内容,因此,根据您的输入(或您的[[think
checkDate.SetDay("4");
checkDate.SetMonth("2");
checkDate.SetDay("2017"); // This is probably too big for a day :-)
我建议您将最后一行更改为SetYear
,然后重试。
并且,经过一番努力(添加了缺少的功能),它编译并生成了它看起来要遵循的结果。我会提到您的代码相当冗长,可以清理很多,因此,我借此机会这样做:将
#include <iostream>
#include <map>
using namespace std;
// Nominal date.h
class Date {
public:
Date();
Date(unsigned day1, unsigned month1, unsigned year1);
void SetDate(unsigned day1, unsigned month1, unsigned year1);
unsigned GetDay() const;
unsigned GetMonth() const;
unsigned GetYear() const;
void SetDate(istream &input);
void GetDate(ostream & os) const;
private:
unsigned day;
unsigned month;
unsigned year;
};
bool operator==(const Date& firstDate, const Date& secondDate);
bool operator>(const Date& firstDate, const Date& secondDate);
bool operator<(const Date& firstDate, const Date& secondDate);
bool operator!=(const Date& firstDate, const Date& secondDate);
// Nominal date.cpp
Date::Date(unsigned day1 = 0, unsigned month1 = 0, unsigned year1 = 0)
: day(day1), month(month1), year(year1) {}
void Date::SetDate(unsigned day1, unsigned month1, unsigned year1) {
day = day1;
month = month1;
year = year1;
}
unsigned Date::GetDay() const { return day; }
unsigned Date::GetMonth() const { return month; }
unsigned Date::GetYear() const { return year; }
void Date::SetDate(istream &input) {
string day1;
string month1;
string year1;
getline(input, day1, '/');
getline(input, month1, '/');
getline(input, year1, ' ');
SetDate(stoi(day1), stoi(month1), stoi(year1));
}
void Date::GetDate(ostream &os) const {
os << GetDay() << "/" << GetMonth() << "/" << GetYear() << " ";
}
bool operator==(const Date& firstDate, const Date& secondDate) {
return firstDate.GetYear() == secondDate.GetYear()
&& firstDate.GetMonth() == secondDate.GetMonth()
&& firstDate.GetDay() == secondDate.GetDay();
}
bool operator!=(const Date& firstDate, const Date& secondDate) {
return ! (firstDate == secondDate);
}
bool operator>(const Date& firstDate, const Date& secondDate) {
if(firstDate.GetYear() > secondDate.GetYear()) return true;
if(firstDate.GetYear() < secondDate.GetYear()) return false;
if(firstDate.GetMonth() > secondDate.GetMonth()) return true;
if(firstDate.GetMonth() < secondDate.GetMonth()) return false;
return firstDate.GetDay() > secondDate.GetDay();
}
bool operator<(const Date& firstDate, const Date& secondDate) {
return ! (firstDate > secondDate || firstDate == secondDate);
}
// Nominal test program.
int main() {
Date tempDate(3, 2, 2013);
Date tempDate2(7, 1, 2015);
Date tempDate3(4, 2, 2017);
multimap<Date, float> mapOption5Solar;
mapOption5Solar.insert(make_pair(tempDate, 35));
mapOption5Solar.insert(make_pair(tempDate2, 56));
mapOption5Solar.insert(make_pair(tempDate3, 37));
auto maxVal = 0f;
Date checkDate(4, 2, 2017);
for (auto it = mapOption5Solar.begin(); it != mapOption5Solar.end(); ++it) {
if (checkDate == it->first) {
if (it->second > maxVal) {
maxVal = it->second;
cout << "Just checking iterator value: " << it->second << '\n';
}
}
}
}
我毫不怀疑可能还会有其他改进,但是我将其留给您练习:-)