【发布时间】:2018-07-04 18:46:23
【问题描述】:
我有一个包含不同组数据的向量。如果我将一个组的新数据插入到向量中,它应该用相同组号的新数据替换旧数据。对于此特定示例,std::replace 给出错误std::replace': no matching overloaded function found
#include <iostream>
#include <vector>
#include <algorithm>
class data
{
public:
int group;
bool condition;
int time;
friend bool operator==(const data& lhs, const data& rhs);
data(int g, bool c, int t)
{
group = g;
condition = c;
time = t;
}
};
bool operator==(const data& lhs, const data& rhs)
{
return lhs.group == rhs.group;
}
int main(int argc, char**)
{
data info_1(10, true , 1);
data info_2(20, true, 1);
data info_3(10, false, 4);
std::vector<data> data_vector;
data_vector.push_back(info_1);
data_vector.push_back(info_2);
std::replace(data_vector.begin(), data_vector.end(), data_vector ,info_3);
std::cout << "vector size: " << data_vector.size() << "\n";
for (int i = 0; i < data_vector.size(); i++)
{
std::cout << "group number: " << data_vector[i].group << std::boolalpha << " condition: " << data_vector[i].condition << "\n";
}
system("pause");
return 0;
}
【问题讨论】:
-
这里有什么特殊原因需要使用矢量而不是地图吗?
-
错误是什么?
-
@neo,
'std::replace': no matching overloaded function found -
至于错误,应在问题正文中发布附加信息,而不是在 cmets 中。
-
std::replace_if(data_vector.begin(), data_vector.end(), [&info_3](auto&&el) {return el.getGroup() == info_3.getGroup(); }, info_3);