为什么STD :: MAP需要操作员<以及我如何写一个

发布于 2025-01-27 14:34:48 字数 1957 浏览 1 评论 0原文

我正在使用STD :: MAP库,并且正在尝试将一堆数据放入地图中,我创建了一个地图来保存日期(Time_t)和float我“错误:'运营商<'无匹配(操作数类型是const&日期,const& date)“

我尝试创建一个超载<操作员,但仍然给了我同样的错误。我还尝试在此程序之外创建一张地图,而不需要操作员<那么我该如何写这篇文章,为什么还要呢?

这是我正在参加的课程:

class MutualFund
{
private:
    std::string ticker;
    Date oldestDate; //optional
    Date newestDate; // optional
    float newestNav; //optional
    map<Date,float> navHistory;
    set<Dividend> divHistory;
public:
    MutualFund(string i)
    {
    if( i == "VTSMX")
    {
    ifstream temp;
    string cell,cell2,tempstring;
    int i = 1;
    float tempFloat;
    temp.open("C:\\Users\\Lukas PC\\Desktop\\ass6files\\VTSMXshuffled.csv");
        //what needs to be done here:
        // turn the cell 1 into a Date object by turning it into a time_t
        //turn the cell2 into a float
        //populate the map
        while(!temp.eof())
        {
        // get the numbers from the spreadsheet
        getline(temp,cell,',');
        getline(temp,cell2,',');
        getline(temp,tempstring);

//make a time_t object from cell and put it into a Date object
        struct std::tm tm = {0};
        std::istringstream ss(cell.c_str());
        ss >> std::get_time(&tm, "%Y-%m-%d");
        //tm.tm_mday  = (tm.tm_mday -1);
        std::time_t time = mktime(&tm);
        Date mapDate;
        mapDate.setDate(time);


 //turn cell2 into a float
        if(isalpha(cell.at(1)) && isalpha(cell2.at(1)))
        {
        }
        else
        {
        cell2.erase(5,20);

        //cout << cell2<< endl;

        std::string::size_type sz;
        tempFloat = stof(cell2,&sz);
        navHistory[mapDate] = tempFloat;
        }
        i++;

        }
    }
    else if (i == "VFINX")
    {

    }
    }


   friend const bool operator< ( const Date &lhs ,const Date &rhs)
    {
        return true;
    }


};

感谢您的帮助!总是赞赏。

I was working with the std::map library and I am trying to put a bunch of data into a map, I created a map to hold a date(time_t) and a float but when I try to add them in, my complier tells me "error: no match for 'operator<' (operand types are const &date, const &date)"

I tried creating an overloaded < operator, but still gave me the same error. I also tried to create a map outside of this program, and that did not need an operator< so how do I write this, and why is it even necessary?

here is the class that I am running it in:

class MutualFund
{
private:
    std::string ticker;
    Date oldestDate; //optional
    Date newestDate; // optional
    float newestNav; //optional
    map<Date,float> navHistory;
    set<Dividend> divHistory;
public:
    MutualFund(string i)
    {
    if( i == "VTSMX")
    {
    ifstream temp;
    string cell,cell2,tempstring;
    int i = 1;
    float tempFloat;
    temp.open("C:\\Users\\Lukas PC\\Desktop\\ass6files\\VTSMXshuffled.csv");
        //what needs to be done here:
        // turn the cell 1 into a Date object by turning it into a time_t
        //turn the cell2 into a float
        //populate the map
        while(!temp.eof())
        {
        // get the numbers from the spreadsheet
        getline(temp,cell,',');
        getline(temp,cell2,',');
        getline(temp,tempstring);

//make a time_t object from cell and put it into a Date object
        struct std::tm tm = {0};
        std::istringstream ss(cell.c_str());
        ss >> std::get_time(&tm, "%Y-%m-%d");
        //tm.tm_mday  = (tm.tm_mday -1);
        std::time_t time = mktime(&tm);
        Date mapDate;
        mapDate.setDate(time);


 //turn cell2 into a float
        if(isalpha(cell.at(1)) && isalpha(cell2.at(1)))
        {
        }
        else
        {
        cell2.erase(5,20);

        //cout << cell2<< endl;

        std::string::size_type sz;
        tempFloat = stof(cell2,&sz);
        navHistory[mapDate] = tempFloat;
        }
        i++;

        }
    }
    else if (i == "VFINX")
    {

    }
    }


   friend const bool operator< ( const Date &lhs ,const Date &rhs)
    {
        return true;
    }


};

Thanks for your help! always appreciated.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

等风来 2025-02-03 14:34:48

原因std :: map需要比操作员少的原因是因为它被用作红色黑树。不仅如此,而且保证订购地图中的元素。因此,它要求通过运算符&lt;引用的类型将其引用为密钥。

如果您不需要订购的元素,则可以使用std :: Unordered_map。但是,对于用户定义的类型,您必须明确定义和过载std :: Hash

只有一个操作员重载,它可以确定一个人是否更大或它们都相等。

无论如何,您的代码问题是您正在尝试在主类之外创建少的运营商。将重载的运算符将其移入date类的内部,它应该有效。

The reason std::map requires a less than operator is because it is implemented as a red black tree. Not only that, but the elements in a map are guaranteed to be ordered. Thus, it requires that the type it is referencing as the key be comparable via operator<.

If you don't need the elements to be ordered then you could use std::unordered_map. However, for user defined types you would have to explicitly define and overload std::hash.

With just one of operators overloaded it can determine if one is greater or if they are both equal.

Anyways, the issue with your code is the fact that you are trying to create the less than operator outside the main class. Move the overloaded operator< inside of the Date class and it should work.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文