STABLE_PARTITION 问题:没有可调用的匹配函数 "swap"
STABLE_PARTITION problems: no matching function to call to "swap"
不知何故我无法在
上使用 stable_partition 算法
vector<pair<Class, string>>.
我可以重新组织代码以获得我想要的,但对我来说(因为我是 C++ 的新手)更多的是 "WHY" 而不是 "HOW" 问题。如果您澄清此行为,我们将很高兴:
首先,class日期(可以省略,以后再来看):
#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 的问题:
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;
};
编译时,return有很多同类问题:
我在 vector<pair<int, string>>
上尝试了相同的代码(使用 stable_partition 和 lambda {return p.first == _int; } 并且成功了。
感谢您的帮助
您还需要为 Date class 定义重载 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;
};
std::stable_partition
函数应该修改向量。但是,您在 const
成员函数中调用它,因此 storage
就是 const
。这不行。
解决方案:不要将 Print
设为常量,或在 storage
的副本上使用 std::stable_partition
。两者都不是很好的解决方案,因此您可能应该重新考虑您的设计。
不知何故我无法在
上使用 stable_partition 算法vector<pair<Class, string>>.
我可以重新组织代码以获得我想要的,但对我来说(因为我是 C++ 的新手)更多的是 "WHY" 而不是 "HOW" 问题。如果您澄清此行为,我们将很高兴:
首先,class日期(可以省略,以后再来看):
#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 的问题:
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;
};
编译时,return有很多同类问题:
我在 vector<pair<int, string>>
上尝试了相同的代码(使用 stable_partition 和 lambda {return p.first == _int; } 并且成功了。
感谢您的帮助
您还需要为 Date class 定义重载 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;
};
std::stable_partition
函数应该修改向量。但是,您在 const
成员函数中调用它,因此 storage
就是 const
。这不行。
解决方案:不要将 Print
设为常量,或在 storage
的副本上使用 std::stable_partition
。两者都不是很好的解决方案,因此您可能应该重新考虑您的设计。