lundi 21 janvier 2019

how to find element in array accourding to some field (without scanning all with for loop )

I have some struct with 4 fieds I keep 100 instance of this struct in some array ( not map ) and i want to find some element in the array accourding to speciefic field

I can scan all but i can using maybe std::find or some other way ( i know that the find scan all the array .. but i think it will be simple to use find )

The code:

    struct MyElement 
    {
        std::string val1;
        std::string val2;
        std::string val3;
    }

    std::array<MyElement, 100> _myArray;


    // need to find the first element that the val2 is 'Go' 
    bool found = false; 
    for(int i = 0; i < 100 ; i++)
    {
        if(_myArray[i].val2 == 'Go')
        {
            found = true;
            break;
        }
    }

Aucun commentaire:

Enregistrer un commentaire