C++ C++;函数,该函数向具有给定值的第一条记录返回迭代器
我正在尝试创建一个简单函数,该函数返回一个迭代器,以给定值返回第一条记录。请尝试以下操作C++ C++;函数,该函数向具有给定值的第一条记录返回迭代器,c++,c++11,lambda,listiterator,const-iterator,C++,C++11,Lambda,Listiterator,Const Iterator,我正在尝试创建一个简单函数,该函数返回一个迭代器,以给定值返回第一条记录。请尝试以下操作 std::list<records>::const_iterator findrecords(const std::list<records>& registry, const std::string& student_id) { return std::find
std::list<records>::const_iterator findrecords(const std::list<records>& registry,
const std::string& student_id)
{
return std::find_if(registry.begin(),
registry.end(),
[&]( const records &r ) { return r.student_id == student_id; } );
}
-1.您是否至少花了1秒钟在互联网上搜索此信息?有无数的例子,例如:请不要破坏您的帖子。错误C2064术语不计算为带1个参数的函数记录c:\visual studio\2017\professional\vc\tools\msvc\14.11.25503\include\list1556@JasonW尝试演示程序。考虑到我自己定义了课堂记录。它可能与你的班级不同。
#include <iostream>
#include <list>
#include <string>
#include <algorithm>
struct records
{
std::string student_id;
};
std::list<records>::const_iterator findrecords(const std::list<records>& registry,
const std::string& student_id)
{
return std::find_if(registry.begin(),
registry.end(),
[&](const records &r) { return r.student_id == student_id; });
}
int main()
{
std::list<records> registry = { { "A" }, { "B" }, { "C" } };
std::string student_id( "B" );
auto it = findrecords(registry, student_id);
if (it != registry.end())
{
std::cout << it->student_id << std::endl;
}
return 0;
}
B