std library -- find in a vector inside a struct

Markus Freitag 3,786 Reputation points
2021-11-16T08:52:53.173+00:00

Hello,

    vector<PanelData>   m_PanelData;


//---

    struct PanelData
        {
            CString Error;
            CString OrderNo;
                    bool    PanelReported;

    //----
    std::vector<PanelData>::iterator it;
        it = find(m_PanelData.begin(), m_PanelData.end(), it->PanelReported=true);  //********** not work!  Why?
        int pos = it - m_dequePanelData.begin();

~~~~~~~
Additional relationship 1:n
1:n

 struct PanelData
            {
                CString Error;
                CString OrderNo;
                     bool    PanelReported;
                     vector<Single> VecSingles;

    //-----
    struct Single
        {
            long    Position;
            CString Code;

How do I make the query to find the single Code="4525325"? All examples use only int, no structure, that's why I ask. As an explanation.

.NET CLI
.NET CLI
A cross-platform toolchain for developing, building, running, and publishing .NET applications.
319 questions
C++
C++
A high-level, general-purpose programming language, created as an extension of the C programming language, that has object-oriented, generic, and functional features in addition to facilities for low-level memory manipulation.
3,513 questions
0 comments No comments
{count} votes

Accepted answer
  1. RLWA32 39,911 Reputation points
    2021-11-16T10:25:53.813+00:00

    More complete example using CString and Single struct -

    #include <atlstr.h>
    #include <iostream>
    #include <vector>
    #include <algorithm>
    #include <string>
    using namespace std;
    
    struct Single {
        long position;
        CString code;
    };
    
    struct PanelData {
        CString Error;
        CString OrderNo;
        bool PanelReported;
        vector<Single> vecSingles;
    };
    
    bool pred(const PanelData& pd2)
    {
        vector<Single>::const_iterator found;
        found = find_if(pd2.vecSingles.begin(), pd2.vecSingles.end(), [](const Single& s)->bool {return s.code == "4525325"; });
        return found != pd2.vecSingles.end();
    }
    
    int main()
    {
        vector<PanelData> vData;
    
        for (int i = 0; i < 10; i++)
        {
            PanelData pd2{ "", to_string(i).c_str(), false, {{i, to_string(i + 4525320).c_str()},{i+1, to_string(4525320).c_str()}} };
            vData.emplace_back(pd2);
        }
    
    
    
        vector<PanelData>::iterator found;
        found = find_if(vData.begin(), vData.end(), pred);
    
        if (found != vData.end())
            cout << "Order number is " << found->OrderNo << endl;
        else
            cout << "Not found!\n";
    
        return 0;
    }
    

1 additional answer

Sort by: Most helpful
  1. RLWA32 39,911 Reputation points
    2021-11-16T09:36:02.297+00:00

    Following simple example finds a struct inside a vector -

    #include <iostream>
    #include <vector>
    #include <algorithm>
    using namespace std;
    
    struct PanelData {
        int id;
        bool PanelReported;
    };
    
    int main()
    {
        vector<PanelData> vData;
    
        for (int i = 0; i < 10; i++)
            vData.emplace_back(PanelData{ i, false });
    
        vData[7].PanelReported = true;
    
        vector<PanelData>::iterator found;
    
        found = find_if(vData.begin(), vData.end(), [](PanelData& i)->bool {return i.PanelReported; });
    
        cout << "id is " << found->id << endl;
    }
    
    1 person found this answer helpful.