簡體   English   中英

遍歷對象向量並找到與從文本文件中提取的變量匹配的變量

[英]Iterate through a vector of objects and find a variable that matches one pulled from a text file

所以我有一個對象向量

vector<Module*> moduleVector;         

我需要遍歷它並將對象的屬性與我從文本文件中提取的另一個屬性進行比較

我正在使用ifstreamgetLine()來存儲需要與對象屬性進行比較的元素( fileD是打開的文件, markModId是字符串變量)

getline(fileD, markModId, ' ');

但我不確定如何在迭代器中引用對象的屬性。 所以我的問題是,

如何使用迭代器將文件中的屬性與對象進行比較?

參考這里是我的對象構造函數(id 是我要比較的屬性)

Module::Module(string id, string title, string lecturer, int 
courseworkWeight)
{
   code = id;
   name = title;
   lect = lecturer;
   cwWeight = courseworkWeight;
   exMark = 0; //ex mark initialised as 0
   /*
   Map to store coursework marks
   */
   map<string, float> CWmarks;
   //cwMarks.clear(); //cw marks map cleared
   //create a map that stores
}

exMark就是需要添加到對象中的屬性。 Module構造函數中的所有屬性都是private

如何使用迭代器將文件中的屬性與對象進行比較?

簡短回答:假設您有一個迭代器std::vector<Module*>::iterator iter您可以訪問Module類的公共成員,例如:

(*iter)->/*public member*/;

龍答:首先,你需要私有成員一個getter id為和一個二傳手exMark ,通過它可以得到的id每個Module ,並從文件進行比較的ID,然后它的設置exMark一些價值。

std::string getId()const { return code; }
void setExMark(const double newMark) { exMark = newMark; }

如果要更改Module的第一個 true 實例,可以使用std::find_if來查找Module

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++ 中,您可以使用智能指針而不是原始指針,當對象超出范圍時,它將自動刪除對象。

簡單地取消引用迭代器以訪問其Module*指針,然后您可以使用operator->訪問該對象,例如:

for (std::vector<Module*>::iterator iter = moduleVector.begin(), end = moduleVector.end(); iter != end; ++iter)
{
    Module *m = *iter;
    if (m->code == markModId)
        m->exMark = ...;
}

或者,如果您使用的是 C++11 或更高版本,則讓編譯器為您處理迭代器:

for (Module *m : moduleVector)
{
    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
    }
}

for (Module *m : moduleVector)
{
    if (m->code == markModId)
    {
        m->exMark = ...;
        break; // <-- add this
    }
}

auto iter = std::find_if(moduleVector.begin(), moduleVector.end(),
    [&](Module *m) { return (m->code == markModId); });
if (iter != moduleVector.end())
{
    Module *m = *iter;
    m->exMark = ...;
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM