C++ 遍历对象向量,并找到与从文本文件中提取的变量相匹配的变量
所以我有一个物体的向量C++ 遍历对象向量,并找到与从文本文件中提取的变量相匹配的变量,c++,algorithm,c++11,iterator,stdvector,C++,Algorithm,C++11,Iterator,Stdvector,所以我有一个物体的向量 vector<Module*> moduleVector; 但我不确定如何在迭代器中引用对象的属性。所以我的问题是, 如何使用迭代器将文件中的属性与对象进行比较? 这里是我的对象构造函数(id是我要比较的属性),仅供参考 Module::Module(字符串id、字符串标题、字符串讲师、int 课程作业重量) { 代码=id; 姓名=头衔; 讲师; cwWeight=课程作业重量; exMark=0;//ex mark初始化为0 /* 映射
vector<Module*> moduleVector;
但我不确定如何在迭代器中引用对象的属性。所以我的问题是,
如何使用迭代器将文件中的属性与对象进行比较?
这里是我的对象构造函数(id是我要比较的属性),仅供参考
Module::Module(字符串id、字符串标题、字符串讲师、int
课程作业重量)
{
代码=id;
姓名=头衔;
讲师;
cwWeight=课程作业重量;
exMark=0;//ex mark初始化为0
/*
映射以存储作业标记
*/
地图标记;
//cwMarks.clear();//清除cw标记映射
//创建一个存储
}
而
exMark
是需要添加到对象的属性。模块
构造函数中的所有属性都是私有的
只需取消对迭代器的引用即可访问其模块*
指针,然后您可以使用操作符->
访问对象,如:
for (std::vector<Module*>::iterator iter = moduleVector.begin(), end = moduleVector.end(); iter != end; ++iter)
{
Module *m = *iter;
if (m->code == markModId)
m->exMark = ...;
}
或者,将lambda与标准迭代算法之一结合使用,例如:
std::for_each(moduleVector.begin(), moduleVector.end(),
[&](Module *m)
{
if (m->code == markModId)
m->exMark = ...;
}
);
如果您只对更新1个模块感兴趣,则在找到所需模块时中断循环:
for (std::vector<Module*>::iterator iter = moduleVector.begin(), end = moduleVector.end(); iter != end; ++iter)
{
Module *m = *iter;
if (m->code == markModId)
{
m->exMark = ...;
break; // <-- add this
}
}
如何使用
迭代器
简短回答:假设您有一个迭代器std::vector::iterator iter
您可以访问模块
类的公共成员,如:
(*iter)->/*public member*/;
长答案:首先,您需要一个私有成员
id
的getter和一个exMark
的setter,通过该setter,您可以获取每个模块的id
,并与文件中的id进行比较,然后将其exMark
设置为某个值
std::string getId()const { return code; }
void setExMark(const double newMark) { exMark = newMark; }
如果要更改模块
的第一个真实实例,可以使用来查找模块
:
std::string idFromFile = "two";
auto Condition = [&idFromFile](Module* element){ return element->getId() == idFromFile; };
auto iter = std::find_if(moduleVector.begin(), moduleVector.end(), Condition);
if(iter != moduleVector.end())
(*iter)->setExMark(10.0); // see this
// ^^^^^^^^^
对于多个实例,您可以执行以下操作:
for(auto iter = moduleVector.begin(); iter != moduleVector.end(); ++iter)
if ( (*iter)->getId() == idFromFile)
(*iter)->setExMark(10.0);
<强>注:在现代C++中,可以使用原始指针,而不是原始指针,因为它超出范围后会自动删除对象。 您假设
code
和exMark
首先是私有的,而不是公共的。OP没有显示模块的声明
@JeJo I实现了getter和setter,但是由于某些原因,exMark仍然显示为零。@Sean您检查了:this:的输出了吗?但是,如果要更改id
的所有匹配项,则需要迭代向量并设置exMark。类似这样的内容:@JeJo嗨,JeJo,我检查并尝试了两种方法,但它仍然输出exMark作为0@Shaun它应该会起作用。我认为问题在于您将模块的成员变量与文件中的字符串或变量进行比较。尝试调试(或打印文件的输出数据)代码,并查看从文件中获得的值。(我评估成员变量是否正常)
auto iter = std::find_if(moduleVector.begin(), moduleVector.end(),
[&](Module *m) { return (m->code == markModId); });
if (iter != moduleVector.end())
{
Module *m = *iter;
m->exMark = ...;
}
(*iter)->/*public member*/;
std::string getId()const { return code; }
void setExMark(const double newMark) { exMark = newMark; }
std::string idFromFile = "two";
auto Condition = [&idFromFile](Module* element){ return element->getId() == idFromFile; };
auto iter = std::find_if(moduleVector.begin(), moduleVector.end(), Condition);
if(iter != moduleVector.end())
(*iter)->setExMark(10.0); // see this
// ^^^^^^^^^
for(auto iter = moduleVector.begin(); iter != moduleVector.end(); ++iter)
if ( (*iter)->getId() == idFromFile)
(*iter)->setExMark(10.0);