繁体   English   中英

STABLE_PARTITION问题:没有匹配的函数可以调用“交换”

[英]STABLE_PARTITION problems: no matching function to call to “swap”

我不能以某种方式在上面使用stable_partition算法

vector<pair<Class, string>>. 

我可以重新组织代码以获得想要的东西,但是对我来说(因为我是C ++的新手),这更多是“为什么”而不是“如何”的问题。 如果您能澄清以下行为,我们将非常高兴:

首先,对Date进行分类(您可以忽略它,稍后再看):

#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
#include <set>
#include <vector>

using namespace std;

class Date {
public:
    Date(int new_year, int new_month, int new_day) {
        year = new_year;    month = new_month;      day = new_day;
      }

    int GetYear() const {return year;}
    int GetMonth() const {return month;}
    int GetDay() const {return day;}

private:
  int year, month, day;
};

bool operator<(const Date& lhs, const Date& rhs) {
  return vector<int>{lhs.GetYear(), lhs.GetMonth(), lhs.GetDay()} <
      vector<int>{rhs.GetYear(), rhs.GetMonth(), rhs.GetDay()};
}

bool operator==(const Date& lhs, const Date& rhs) {
  return vector<int>{lhs.GetYear(), lhs.GetMonth(), lhs.GetDay()} ==
      vector<int>{rhs.GetYear(), rhs.GetMonth(), rhs.GetDay()};
}

所以这是有问题的课程:

class Database {
public:
    void Add(const Date& date, const string event){
            storage.push_back(make_pair(date, event));
            set_dates.insert(date);

    }


    void Print(ostream& s) const{

        for(const auto& date : set_dates) {
// TROUBLE IS HERE:
            auto it = stable_partition(begin(storage), end(storage),
                [date](const pair<Date, string> p){
                return p.first == date;
            });

        };

}

private:
      vector<pair<Date, string>> storage;
      set<Date> set_dates;

};

编译时,它会返回很多同类问题: 在此处输入图片说明

我已经在vector<pair<int, string>>上尝试了相同的代码(使用带有lambda {return p.first == _int;}的stable_partition,并且可以正常工作。

感谢您的帮助

应该使用std::stable_partition函数修改向量。 但是,你在呼唤它在一个const成员函数,所以storageconst存在。 这行不通。

解决方案:不要使Print const或在storage的副本上使用std::stable_partition 都不是一个好的解决方案,因此您可能应该重新考虑您的设计。

您还需要为Date类定义重载operator =。 如果您这样做,它将起作用。

class Date {
public:
Date(int new_year, int new_month, int new_day) {
    year = new_year;    month = new_month;      day = new_day;
  }

// Need to define overloading operator=
Date& operator=(const Date& rhs)
{

}

int GetYear() const {return year;}
int GetMonth() const {return month;}
int GetDay() const {return day;}

private:
  int year, month, day;
};

暂无
暂无

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM