C++ using copy_if

C++ using copy_if

本文关键字:if copy using C++      更新时间:2023-10-16

我是C++新手,正在尝试使用copy_if函数:

set<Person> people; // contains people objects
set<Person> copyedPeople;
string name = "joe"; // Multiple people with that name
copy_if(people.begin(), people.end(), copyedPeople, Person.getName() == name);

问题出在Person.getName(),它说不允许类型名称?

您需要插入器,加上有效的谓词:

std::copy_if(people.begin(), people.end(),
             std::inserter(copyedPeople, copyedPeople.end()),
             [](const auto& person){ return person.getName() == name; });

我不知道你的比较函数,但是如果你使用这个名字,以前的答案最多会返回1个人。 std::multiset可能适合equal_range.