C++ lambda syntax












10















I have a function that searches a vector of iterators and returns the iterator if its names matches a string passed as an argument.



koalaGraph::PVertex lookUpByName(std::string Name, std::vector<koalaGraph::PVertex>& Vertices) {

for (size_t i = 0; i < Vertices.size(); i++) {

if(Vertices[i]->info.name == Name)
return Vertices[i];
}
}


My question is how can I implement this as a lambda, to use it in connection with std::find_if?



I'm trying this:



std::vector<koalaGraph::PVertex> V;
std::string Name;
std::find_if(V.begin(), V.end(), [&Name]() {return Name == V->info.name;})


But it says that V




an enclosing-function local variable cannot be referenced in a lambda body unless it is in the capture list.











share|improve this question









New contributor




black sheep is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
















  • 1





    No return if nothing found?

    – Deduplicator
    yesterday
















10















I have a function that searches a vector of iterators and returns the iterator if its names matches a string passed as an argument.



koalaGraph::PVertex lookUpByName(std::string Name, std::vector<koalaGraph::PVertex>& Vertices) {

for (size_t i = 0; i < Vertices.size(); i++) {

if(Vertices[i]->info.name == Name)
return Vertices[i];
}
}


My question is how can I implement this as a lambda, to use it in connection with std::find_if?



I'm trying this:



std::vector<koalaGraph::PVertex> V;
std::string Name;
std::find_if(V.begin(), V.end(), [&Name]() {return Name == V->info.name;})


But it says that V




an enclosing-function local variable cannot be referenced in a lambda body unless it is in the capture list.











share|improve this question









New contributor




black sheep is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
















  • 1





    No return if nothing found?

    – Deduplicator
    yesterday














10












10








10


1






I have a function that searches a vector of iterators and returns the iterator if its names matches a string passed as an argument.



koalaGraph::PVertex lookUpByName(std::string Name, std::vector<koalaGraph::PVertex>& Vertices) {

for (size_t i = 0; i < Vertices.size(); i++) {

if(Vertices[i]->info.name == Name)
return Vertices[i];
}
}


My question is how can I implement this as a lambda, to use it in connection with std::find_if?



I'm trying this:



std::vector<koalaGraph::PVertex> V;
std::string Name;
std::find_if(V.begin(), V.end(), [&Name]() {return Name == V->info.name;})


But it says that V




an enclosing-function local variable cannot be referenced in a lambda body unless it is in the capture list.











share|improve this question









New contributor




black sheep is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.












I have a function that searches a vector of iterators and returns the iterator if its names matches a string passed as an argument.



koalaGraph::PVertex lookUpByName(std::string Name, std::vector<koalaGraph::PVertex>& Vertices) {

for (size_t i = 0; i < Vertices.size(); i++) {

if(Vertices[i]->info.name == Name)
return Vertices[i];
}
}


My question is how can I implement this as a lambda, to use it in connection with std::find_if?



I'm trying this:



std::vector<koalaGraph::PVertex> V;
std::string Name;
std::find_if(V.begin(), V.end(), [&Name]() {return Name == V->info.name;})


But it says that V




an enclosing-function local variable cannot be referenced in a lambda body unless it is in the capture list.








c++ lambda






share|improve this question









New contributor




black sheep is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.











share|improve this question









New contributor




black sheep is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.









share|improve this question




share|improve this question








edited yesterday









jvb

1133




1133






New contributor




black sheep is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.









asked yesterday









black sheepblack sheep

603




603




New contributor




black sheep is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.





New contributor





black sheep is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.






black sheep is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.








  • 1





    No return if nothing found?

    – Deduplicator
    yesterday














  • 1





    No return if nothing found?

    – Deduplicator
    yesterday








1




1





No return if nothing found?

– Deduplicator
yesterday





No return if nothing found?

– Deduplicator
yesterday












5 Answers
5






active

oldest

votes


















18














find_if is going to pass the elements of the vector into your lambda. That means you need



std::find_if(V.begin(), V.end(), [&Name](auto const& V) {return Name == V->info.name;})


so that the V in the lambda body is the element of the vector, not the vector itself.





Ideally you'd give it a different name than V so you keep the vector and local variables separate like



std::find_if(V.begin(), V.end(), [&Name](auto const& element) {return Name == elememt->info.name;})


So now it is clear you are working on a element of the vector, instead of the vector itself.






share|improve this answer





















  • 4





    It would be better not to use the same name V for two different things (the vector and the iterator). Thus, std::find_if(V.begin(), V.end(), [&Name](auto const& it) {return Name == it->info.name;}) would be better.

    – Handy999
    yesterday











  • @Handy999 it is also misleading though, since it's not an iterator. Maybe value, element or whatever else :)

    – Rakete1111
    yesterday











  • @Handy999 You're right about giving it a better name, I just wanted to keep it as V as it makes my last sentence very succinct.

    – NathanOliver
    yesterday



















7














First, V->info.name is ill formed, inside or outside of the lambda.



The function object sent to the algoritm std::find_if must be a unary function. It must take the current element to check as a parameter.



auto found = std::find_if(
V.begin(), V.end(),
[&Name](koalaGraph::PVertex const& item_to_check) {
return Name == item_to_check->info.name;
}
);


The type of found is an iterator to the element that has been found. If none is found, then it returns V.end()



If you use C++14 or better, you can even use generic lambdas:



auto found = std::find_if(
V.begin(), V.end(),
[&Name](auto const& item_to_check) {
return Name == item_to_check->info.name;
}
);





share|improve this answer


























  • V->info.name is valid syntax. It just won't typecheck since vectors don't overload ->.

    – Silvio Mayolo
    yesterday






  • 2





    It just won't typecheck since vectors don't overload -> so... it won't compile? It's an invalid syntax to use -> on object of types that don't overload operator->

    – Guillaume Racicot
    yesterday











  • Not all compile errors are syntax errors. See syntax vs. semantics.

    – Silvio Mayolo
    yesterday






  • 1





    @SilvioMayolo The answer you linked me says that semantics is the meaning of the program, usually in it's runtime behaviour. The code I quoted is simply ill formed, there is no semantic yet. Or maybe I don't understand the concept correctly

    – Guillaume Racicot
    yesterday





















4














std::find_if's predicate will receive a reference to each element of the range in turn. You need:



std::find_if(
V.begin(), V.end(),
[&Name](koalaGraph::PVertex const &v) { return Name == v->info.name; }
);





share|improve this answer































    2














    Get V as parameter to the lambda.



    std::find_if(V.begin(), V.end(), [&Name](type& V) {return Name == V->info.name;)






    share|improve this answer































      1














      Use const auto & to access the individual elements from your vector in the lambda expression. Since the vector is an lvalue, auto will be deduced to const vector<PVertex> &. Then, you can use std::distance to find the element location of the object in the vector.



      struct PVertex
      {
      std::string name;
      };

      int main()
      {
      std::vector<PVertex> V = {{"foo"},{"bar"},{"cat"},{"dog"}};

      std::string Name = "cat";

      auto found = std::find_if(std::begin(V), std::end(V), [&Name](const auto &v){return (Name == v.name);});

      std::cout<< "found at: V["<<std::distance(std::begin(V),found)<<"]" <<std::endl;
      }


      Result is:



      found at: V[2]


      Example: https://rextester.com/IYNA58046






      share|improve this answer























        Your Answer






        StackExchange.ifUsing("editor", function () {
        StackExchange.using("externalEditor", function () {
        StackExchange.using("snippets", function () {
        StackExchange.snippets.init();
        });
        });
        }, "code-snippets");

        StackExchange.ready(function() {
        var channelOptions = {
        tags: "".split(" "),
        id: "1"
        };
        initTagRenderer("".split(" "), "".split(" "), channelOptions);

        StackExchange.using("externalEditor", function() {
        // Have to fire editor after snippets, if snippets enabled
        if (StackExchange.settings.snippets.snippetsEnabled) {
        StackExchange.using("snippets", function() {
        createEditor();
        });
        }
        else {
        createEditor();
        }
        });

        function createEditor() {
        StackExchange.prepareEditor({
        heartbeatType: 'answer',
        autoActivateHeartbeat: false,
        convertImagesToLinks: true,
        noModals: true,
        showLowRepImageUploadWarning: true,
        reputationToPostImages: 10,
        bindNavPrevention: true,
        postfix: "",
        imageUploader: {
        brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
        contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
        allowUrls: true
        },
        onDemand: true,
        discardSelector: ".discard-answer"
        ,immediatelyShowMarkdownHelp:true
        });


        }
        });






        black sheep is a new contributor. Be nice, and check out our Code of Conduct.










        draft saved

        draft discarded


















        StackExchange.ready(
        function () {
        StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55243276%2fc-lambda-syntax%23new-answer', 'question_page');
        }
        );

        Post as a guest















        Required, but never shown

























        5 Answers
        5






        active

        oldest

        votes








        5 Answers
        5






        active

        oldest

        votes









        active

        oldest

        votes






        active

        oldest

        votes









        18














        find_if is going to pass the elements of the vector into your lambda. That means you need



        std::find_if(V.begin(), V.end(), [&Name](auto const& V) {return Name == V->info.name;})


        so that the V in the lambda body is the element of the vector, not the vector itself.





        Ideally you'd give it a different name than V so you keep the vector and local variables separate like



        std::find_if(V.begin(), V.end(), [&Name](auto const& element) {return Name == elememt->info.name;})


        So now it is clear you are working on a element of the vector, instead of the vector itself.






        share|improve this answer





















        • 4





          It would be better not to use the same name V for two different things (the vector and the iterator). Thus, std::find_if(V.begin(), V.end(), [&Name](auto const& it) {return Name == it->info.name;}) would be better.

          – Handy999
          yesterday











        • @Handy999 it is also misleading though, since it's not an iterator. Maybe value, element or whatever else :)

          – Rakete1111
          yesterday











        • @Handy999 You're right about giving it a better name, I just wanted to keep it as V as it makes my last sentence very succinct.

          – NathanOliver
          yesterday
















        18














        find_if is going to pass the elements of the vector into your lambda. That means you need



        std::find_if(V.begin(), V.end(), [&Name](auto const& V) {return Name == V->info.name;})


        so that the V in the lambda body is the element of the vector, not the vector itself.





        Ideally you'd give it a different name than V so you keep the vector and local variables separate like



        std::find_if(V.begin(), V.end(), [&Name](auto const& element) {return Name == elememt->info.name;})


        So now it is clear you are working on a element of the vector, instead of the vector itself.






        share|improve this answer





















        • 4





          It would be better not to use the same name V for two different things (the vector and the iterator). Thus, std::find_if(V.begin(), V.end(), [&Name](auto const& it) {return Name == it->info.name;}) would be better.

          – Handy999
          yesterday











        • @Handy999 it is also misleading though, since it's not an iterator. Maybe value, element or whatever else :)

          – Rakete1111
          yesterday











        • @Handy999 You're right about giving it a better name, I just wanted to keep it as V as it makes my last sentence very succinct.

          – NathanOliver
          yesterday














        18












        18








        18







        find_if is going to pass the elements of the vector into your lambda. That means you need



        std::find_if(V.begin(), V.end(), [&Name](auto const& V) {return Name == V->info.name;})


        so that the V in the lambda body is the element of the vector, not the vector itself.





        Ideally you'd give it a different name than V so you keep the vector and local variables separate like



        std::find_if(V.begin(), V.end(), [&Name](auto const& element) {return Name == elememt->info.name;})


        So now it is clear you are working on a element of the vector, instead of the vector itself.






        share|improve this answer















        find_if is going to pass the elements of the vector into your lambda. That means you need



        std::find_if(V.begin(), V.end(), [&Name](auto const& V) {return Name == V->info.name;})


        so that the V in the lambda body is the element of the vector, not the vector itself.





        Ideally you'd give it a different name than V so you keep the vector and local variables separate like



        std::find_if(V.begin(), V.end(), [&Name](auto const& element) {return Name == elememt->info.name;})


        So now it is clear you are working on a element of the vector, instead of the vector itself.







        share|improve this answer














        share|improve this answer



        share|improve this answer








        edited yesterday

























        answered yesterday









        NathanOliverNathanOliver

        95.8k16135208




        95.8k16135208








        • 4





          It would be better not to use the same name V for two different things (the vector and the iterator). Thus, std::find_if(V.begin(), V.end(), [&Name](auto const& it) {return Name == it->info.name;}) would be better.

          – Handy999
          yesterday











        • @Handy999 it is also misleading though, since it's not an iterator. Maybe value, element or whatever else :)

          – Rakete1111
          yesterday











        • @Handy999 You're right about giving it a better name, I just wanted to keep it as V as it makes my last sentence very succinct.

          – NathanOliver
          yesterday














        • 4





          It would be better not to use the same name V for two different things (the vector and the iterator). Thus, std::find_if(V.begin(), V.end(), [&Name](auto const& it) {return Name == it->info.name;}) would be better.

          – Handy999
          yesterday











        • @Handy999 it is also misleading though, since it's not an iterator. Maybe value, element or whatever else :)

          – Rakete1111
          yesterday











        • @Handy999 You're right about giving it a better name, I just wanted to keep it as V as it makes my last sentence very succinct.

          – NathanOliver
          yesterday








        4




        4





        It would be better not to use the same name V for two different things (the vector and the iterator). Thus, std::find_if(V.begin(), V.end(), [&Name](auto const& it) {return Name == it->info.name;}) would be better.

        – Handy999
        yesterday





        It would be better not to use the same name V for two different things (the vector and the iterator). Thus, std::find_if(V.begin(), V.end(), [&Name](auto const& it) {return Name == it->info.name;}) would be better.

        – Handy999
        yesterday













        @Handy999 it is also misleading though, since it's not an iterator. Maybe value, element or whatever else :)

        – Rakete1111
        yesterday





        @Handy999 it is also misleading though, since it's not an iterator. Maybe value, element or whatever else :)

        – Rakete1111
        yesterday













        @Handy999 You're right about giving it a better name, I just wanted to keep it as V as it makes my last sentence very succinct.

        – NathanOliver
        yesterday





        @Handy999 You're right about giving it a better name, I just wanted to keep it as V as it makes my last sentence very succinct.

        – NathanOliver
        yesterday













        7














        First, V->info.name is ill formed, inside or outside of the lambda.



        The function object sent to the algoritm std::find_if must be a unary function. It must take the current element to check as a parameter.



        auto found = std::find_if(
        V.begin(), V.end(),
        [&Name](koalaGraph::PVertex const& item_to_check) {
        return Name == item_to_check->info.name;
        }
        );


        The type of found is an iterator to the element that has been found. If none is found, then it returns V.end()



        If you use C++14 or better, you can even use generic lambdas:



        auto found = std::find_if(
        V.begin(), V.end(),
        [&Name](auto const& item_to_check) {
        return Name == item_to_check->info.name;
        }
        );





        share|improve this answer


























        • V->info.name is valid syntax. It just won't typecheck since vectors don't overload ->.

          – Silvio Mayolo
          yesterday






        • 2





          It just won't typecheck since vectors don't overload -> so... it won't compile? It's an invalid syntax to use -> on object of types that don't overload operator->

          – Guillaume Racicot
          yesterday











        • Not all compile errors are syntax errors. See syntax vs. semantics.

          – Silvio Mayolo
          yesterday






        • 1





          @SilvioMayolo The answer you linked me says that semantics is the meaning of the program, usually in it's runtime behaviour. The code I quoted is simply ill formed, there is no semantic yet. Or maybe I don't understand the concept correctly

          – Guillaume Racicot
          yesterday


















        7














        First, V->info.name is ill formed, inside or outside of the lambda.



        The function object sent to the algoritm std::find_if must be a unary function. It must take the current element to check as a parameter.



        auto found = std::find_if(
        V.begin(), V.end(),
        [&Name](koalaGraph::PVertex const& item_to_check) {
        return Name == item_to_check->info.name;
        }
        );


        The type of found is an iterator to the element that has been found. If none is found, then it returns V.end()



        If you use C++14 or better, you can even use generic lambdas:



        auto found = std::find_if(
        V.begin(), V.end(),
        [&Name](auto const& item_to_check) {
        return Name == item_to_check->info.name;
        }
        );





        share|improve this answer


























        • V->info.name is valid syntax. It just won't typecheck since vectors don't overload ->.

          – Silvio Mayolo
          yesterday






        • 2





          It just won't typecheck since vectors don't overload -> so... it won't compile? It's an invalid syntax to use -> on object of types that don't overload operator->

          – Guillaume Racicot
          yesterday











        • Not all compile errors are syntax errors. See syntax vs. semantics.

          – Silvio Mayolo
          yesterday






        • 1





          @SilvioMayolo The answer you linked me says that semantics is the meaning of the program, usually in it's runtime behaviour. The code I quoted is simply ill formed, there is no semantic yet. Or maybe I don't understand the concept correctly

          – Guillaume Racicot
          yesterday
















        7












        7








        7







        First, V->info.name is ill formed, inside or outside of the lambda.



        The function object sent to the algoritm std::find_if must be a unary function. It must take the current element to check as a parameter.



        auto found = std::find_if(
        V.begin(), V.end(),
        [&Name](koalaGraph::PVertex const& item_to_check) {
        return Name == item_to_check->info.name;
        }
        );


        The type of found is an iterator to the element that has been found. If none is found, then it returns V.end()



        If you use C++14 or better, you can even use generic lambdas:



        auto found = std::find_if(
        V.begin(), V.end(),
        [&Name](auto const& item_to_check) {
        return Name == item_to_check->info.name;
        }
        );





        share|improve this answer















        First, V->info.name is ill formed, inside or outside of the lambda.



        The function object sent to the algoritm std::find_if must be a unary function. It must take the current element to check as a parameter.



        auto found = std::find_if(
        V.begin(), V.end(),
        [&Name](koalaGraph::PVertex const& item_to_check) {
        return Name == item_to_check->info.name;
        }
        );


        The type of found is an iterator to the element that has been found. If none is found, then it returns V.end()



        If you use C++14 or better, you can even use generic lambdas:



        auto found = std::find_if(
        V.begin(), V.end(),
        [&Name](auto const& item_to_check) {
        return Name == item_to_check->info.name;
        }
        );






        share|improve this answer














        share|improve this answer



        share|improve this answer








        edited yesterday

























        answered yesterday









        Guillaume RacicotGuillaume Racicot

        15.4k53568




        15.4k53568













        • V->info.name is valid syntax. It just won't typecheck since vectors don't overload ->.

          – Silvio Mayolo
          yesterday






        • 2





          It just won't typecheck since vectors don't overload -> so... it won't compile? It's an invalid syntax to use -> on object of types that don't overload operator->

          – Guillaume Racicot
          yesterday











        • Not all compile errors are syntax errors. See syntax vs. semantics.

          – Silvio Mayolo
          yesterday






        • 1





          @SilvioMayolo The answer you linked me says that semantics is the meaning of the program, usually in it's runtime behaviour. The code I quoted is simply ill formed, there is no semantic yet. Or maybe I don't understand the concept correctly

          – Guillaume Racicot
          yesterday





















        • V->info.name is valid syntax. It just won't typecheck since vectors don't overload ->.

          – Silvio Mayolo
          yesterday






        • 2





          It just won't typecheck since vectors don't overload -> so... it won't compile? It's an invalid syntax to use -> on object of types that don't overload operator->

          – Guillaume Racicot
          yesterday











        • Not all compile errors are syntax errors. See syntax vs. semantics.

          – Silvio Mayolo
          yesterday






        • 1





          @SilvioMayolo The answer you linked me says that semantics is the meaning of the program, usually in it's runtime behaviour. The code I quoted is simply ill formed, there is no semantic yet. Or maybe I don't understand the concept correctly

          – Guillaume Racicot
          yesterday



















        V->info.name is valid syntax. It just won't typecheck since vectors don't overload ->.

        – Silvio Mayolo
        yesterday





        V->info.name is valid syntax. It just won't typecheck since vectors don't overload ->.

        – Silvio Mayolo
        yesterday




        2




        2





        It just won't typecheck since vectors don't overload -> so... it won't compile? It's an invalid syntax to use -> on object of types that don't overload operator->

        – Guillaume Racicot
        yesterday





        It just won't typecheck since vectors don't overload -> so... it won't compile? It's an invalid syntax to use -> on object of types that don't overload operator->

        – Guillaume Racicot
        yesterday













        Not all compile errors are syntax errors. See syntax vs. semantics.

        – Silvio Mayolo
        yesterday





        Not all compile errors are syntax errors. See syntax vs. semantics.

        – Silvio Mayolo
        yesterday




        1




        1





        @SilvioMayolo The answer you linked me says that semantics is the meaning of the program, usually in it's runtime behaviour. The code I quoted is simply ill formed, there is no semantic yet. Or maybe I don't understand the concept correctly

        – Guillaume Racicot
        yesterday







        @SilvioMayolo The answer you linked me says that semantics is the meaning of the program, usually in it's runtime behaviour. The code I quoted is simply ill formed, there is no semantic yet. Or maybe I don't understand the concept correctly

        – Guillaume Racicot
        yesterday













        4














        std::find_if's predicate will receive a reference to each element of the range in turn. You need:



        std::find_if(
        V.begin(), V.end(),
        [&Name](koalaGraph::PVertex const &v) { return Name == v->info.name; }
        );





        share|improve this answer




























          4














          std::find_if's predicate will receive a reference to each element of the range in turn. You need:



          std::find_if(
          V.begin(), V.end(),
          [&Name](koalaGraph::PVertex const &v) { return Name == v->info.name; }
          );





          share|improve this answer


























            4












            4








            4







            std::find_if's predicate will receive a reference to each element of the range in turn. You need:



            std::find_if(
            V.begin(), V.end(),
            [&Name](koalaGraph::PVertex const &v) { return Name == v->info.name; }
            );





            share|improve this answer













            std::find_if's predicate will receive a reference to each element of the range in turn. You need:



            std::find_if(
            V.begin(), V.end(),
            [&Name](koalaGraph::PVertex const &v) { return Name == v->info.name; }
            );






            share|improve this answer












            share|improve this answer



            share|improve this answer










            answered yesterday









            QuentinQuentin

            46.6k589146




            46.6k589146























                2














                Get V as parameter to the lambda.



                std::find_if(V.begin(), V.end(), [&Name](type& V) {return Name == V->info.name;)






                share|improve this answer




























                  2














                  Get V as parameter to the lambda.



                  std::find_if(V.begin(), V.end(), [&Name](type& V) {return Name == V->info.name;)






                  share|improve this answer


























                    2












                    2








                    2







                    Get V as parameter to the lambda.



                    std::find_if(V.begin(), V.end(), [&Name](type& V) {return Name == V->info.name;)






                    share|improve this answer













                    Get V as parameter to the lambda.



                    std::find_if(V.begin(), V.end(), [&Name](type& V) {return Name == V->info.name;)







                    share|improve this answer












                    share|improve this answer



                    share|improve this answer










                    answered yesterday









                    Petar VelevPetar Velev

                    1,681619




                    1,681619























                        1














                        Use const auto & to access the individual elements from your vector in the lambda expression. Since the vector is an lvalue, auto will be deduced to const vector<PVertex> &. Then, you can use std::distance to find the element location of the object in the vector.



                        struct PVertex
                        {
                        std::string name;
                        };

                        int main()
                        {
                        std::vector<PVertex> V = {{"foo"},{"bar"},{"cat"},{"dog"}};

                        std::string Name = "cat";

                        auto found = std::find_if(std::begin(V), std::end(V), [&Name](const auto &v){return (Name == v.name);});

                        std::cout<< "found at: V["<<std::distance(std::begin(V),found)<<"]" <<std::endl;
                        }


                        Result is:



                        found at: V[2]


                        Example: https://rextester.com/IYNA58046






                        share|improve this answer




























                          1














                          Use const auto & to access the individual elements from your vector in the lambda expression. Since the vector is an lvalue, auto will be deduced to const vector<PVertex> &. Then, you can use std::distance to find the element location of the object in the vector.



                          struct PVertex
                          {
                          std::string name;
                          };

                          int main()
                          {
                          std::vector<PVertex> V = {{"foo"},{"bar"},{"cat"},{"dog"}};

                          std::string Name = "cat";

                          auto found = std::find_if(std::begin(V), std::end(V), [&Name](const auto &v){return (Name == v.name);});

                          std::cout<< "found at: V["<<std::distance(std::begin(V),found)<<"]" <<std::endl;
                          }


                          Result is:



                          found at: V[2]


                          Example: https://rextester.com/IYNA58046






                          share|improve this answer


























                            1












                            1








                            1







                            Use const auto & to access the individual elements from your vector in the lambda expression. Since the vector is an lvalue, auto will be deduced to const vector<PVertex> &. Then, you can use std::distance to find the element location of the object in the vector.



                            struct PVertex
                            {
                            std::string name;
                            };

                            int main()
                            {
                            std::vector<PVertex> V = {{"foo"},{"bar"},{"cat"},{"dog"}};

                            std::string Name = "cat";

                            auto found = std::find_if(std::begin(V), std::end(V), [&Name](const auto &v){return (Name == v.name);});

                            std::cout<< "found at: V["<<std::distance(std::begin(V),found)<<"]" <<std::endl;
                            }


                            Result is:



                            found at: V[2]


                            Example: https://rextester.com/IYNA58046






                            share|improve this answer













                            Use const auto & to access the individual elements from your vector in the lambda expression. Since the vector is an lvalue, auto will be deduced to const vector<PVertex> &. Then, you can use std::distance to find the element location of the object in the vector.



                            struct PVertex
                            {
                            std::string name;
                            };

                            int main()
                            {
                            std::vector<PVertex> V = {{"foo"},{"bar"},{"cat"},{"dog"}};

                            std::string Name = "cat";

                            auto found = std::find_if(std::begin(V), std::end(V), [&Name](const auto &v){return (Name == v.name);});

                            std::cout<< "found at: V["<<std::distance(std::begin(V),found)<<"]" <<std::endl;
                            }


                            Result is:



                            found at: V[2]


                            Example: https://rextester.com/IYNA58046







                            share|improve this answer












                            share|improve this answer



                            share|improve this answer










                            answered yesterday









                            Constantinos GlynosConstantinos Glynos

                            1,581820




                            1,581820






















                                black sheep is a new contributor. Be nice, and check out our Code of Conduct.










                                draft saved

                                draft discarded


















                                black sheep is a new contributor. Be nice, and check out our Code of Conduct.













                                black sheep is a new contributor. Be nice, and check out our Code of Conduct.












                                black sheep is a new contributor. Be nice, and check out our Code of Conduct.
















                                Thanks for contributing an answer to Stack Overflow!


                                • Please be sure to answer the question. Provide details and share your research!

                                But avoid



                                • Asking for help, clarification, or responding to other answers.

                                • Making statements based on opinion; back them up with references or personal experience.


                                To learn more, see our tips on writing great answers.




                                draft saved


                                draft discarded














                                StackExchange.ready(
                                function () {
                                StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55243276%2fc-lambda-syntax%23new-answer', 'question_page');
                                }
                                );

                                Post as a guest















                                Required, but never shown





















































                                Required, but never shown














                                Required, but never shown












                                Required, but never shown







                                Required, but never shown

































                                Required, but never shown














                                Required, but never shown












                                Required, but never shown







                                Required, but never shown







                                Popular posts from this blog

                                How did Captain America manage to do this?

                                迪纳利

                                南乌拉尔铁路局