[RESOLVED] Using Lambdas to search a std::vector for an object ... (Help)
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 3 of 3

Thread: [RESOLVED] Using Lambdas to search a std::vector for an object ... (Help)

  1. #1
    Join Date
    Sep 2016
    Posts
    8

    [RESOLVED] Using Lambdas to search a std::vector for an object ... (Help)

    I'm trying to understand Lambda expressions.
    And I guess it's true that teaching an old dog new tricks is tricky.
    I've inserted a code sample below; what I'm looking for is a lambda expression/statement that searches myVector for an element in
    the vector that has an integer (endpoint) with a specified value.
    I'd like the code to simply print out the position of the element in the vector where the endpoints' value matches the expression.

    Code:
    #include <algorithm>
    #include <cstdint>
    #include <cstdio>
    #include <cstdlib>
    #include <iostream>
    #include <memory>
    
    using namespace std;
    
    /*
     * 
     */
    int
    main (int argc, char** argv)
    {
    
        typedef struct mystruct
        {
            int endpoint;
    
            mystruct (int a)
            {
                endpoint = a;
            }
        } MyStruct;
    
        std::vector<std::unique_ptr < MyStruct>> myVector;
    
        for ( int i = 0; i < 5; i++ )
            myVector.emplace_back(new MyStruct (i*i));
    
        // Lambda expression here to search myVector for instance where endpoint = 9;
        // and print the position of that element in the vector. 
        // Output should obviously be 3;
    
        return 0;
    }

  2. #2
    Join Date
    Jan 2006
    Location
    Singapore
    Posts
    6,711

    Re: Using Lambdas to search a std::vector for an object ... (Help)

    Perhaps you could consider something like this, compiling with respect to C++14 or later:
    Code:
    #include <algorithm>
    #include <iostream>
    #include <memory>
    #include <vector>
    
    struct MyStruct
    {
        int endpoint;
    
        explicit MyStruct(int a) : endpoint(a) {}
    };
    
    int main()
    {
        std::vector<std::unique_ptr<MyStruct>> myVector;
    
        for (int i = 0; i < 5; ++i)
        {
            myVector.emplace_back(new MyStruct(i * i));
        }
    
        // Lambda expression here to search myVector for instance where endpoint = 9;
        // and print the position of that element in the vector. 
        // Output should obviously be 3;
        auto result = find_if(myVector.begin(), myVector.end(),
                              [](const auto& x){ return x->endpoint == 9; });
        if (result != myVector.end())
        {
            std::cout << "Found: " << (*result)->endpoint
                      << " @ index " << (result - myVector.begin()) << std::endl;
        }
        else
        {
            std::cout << "Not Found" << std::endl;
        }
    
        return 0;
    }
    C + C++ Compiler: MinGW port of GCC
    Build + Version Control System: SCons + Bazaar

    Look up a C/C++ Reference and learn How To Ask Questions The Smart Way
    Kindly rate my posts if you found them useful

  3. #3
    Join Date
    Sep 2016
    Posts
    8

    Re: Using Lambdas to search a std::vector for an object ... (Help)

    Hey Laserlight.... I sincerely appreciate your kind response. I learn by example and that one is one of the best. I always like to repay the kindness of others when it comes to things like this. If you have a PayPal account I'd like to send you $20 USD for your trouble.
    Last edited by Non Sequitur; April 4th, 2017 at 08:05 PM.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This a Codeguru.com survey!


HTML5 Development Center