Like Tree1Likes
  • 1 Post By laserlight

Searching string within vector struct C++ 11

This is a discussion on Searching string within vector struct C++ 11 within the C++ Programming forums, part of the General Programming Boards category; Hello, new to C++ lambda, got two question in regards of searching for strings within a vector struct. Minimalistic example: ...

  1. #1
    Registered User
    Join Date
    Nov 2012
    Posts
    38

    Searching string within vector struct C++ 11

    Hello,

    new to C++ lambda, got two question in regards of searching for strings within a vector struct.

    Minimalistic example:
    Code:
    struct singleFile {
        std::string filename;
    };
    
    std::vector<singleFile>myStorage;
    
    myStorage.push_back(singleFile());
    myStorage.push_back(singleFile());
    myStorage.push_back(singleFile());
    myStorage[0].filename = "file0";
    myStorage[1].filename = "file1";
    myStorage[2].filename = "file2";
    
    
    auto it = std::find_if(std::begin(myStorage), std::end(myStorage), [] (myStorage const& f)
        { 
        return (f.filename == "file1"); 
        });
    bool found = (it != std::end(myStorage));
    1) why does found always return 0 eventhough the filename exists?
    2) I don't know how to pass an actual search string instead of the hardcoded approach as above with filename =="bla" :/ could you give me some hints by any chance?

    thx in advance=)

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    22,271
    Quote Originally Posted by coffee_cup
    1) why does found always return 0 eventhough the filename exists?
    It shouldn't. However, I notice that your lambda is wrong. The "myStorage const& f" parameter declaration should be "singleFile const& f".

    Quote Originally Posted by coffee_cup
    2) I don't know how to pass an actual search string instead of the hardcoded approach as above with filename =="bla" :/ could you give me some hints by any chance?
    Make use of a capture, e.g.,
    Code:
    std::string subject = "file1";
    auto it = std::find_if(std::begin(myStorage), std::end(myStorage), [&subject](const singleFile& f)
    {
        return f.filename == subject;
    });
    though of course you presumably will not hard code the initialisation of subject

    By the way, because of argument dependent (Koenig) lookup, you can write:
    Code:
    std::string subject = "file1";
    auto it = std::find_if(begin(myStorage), end(myStorage), [&subject](const singleFile& f)
    {
        return f.filename == subject;
    });
    even without the using declarations/directive. (You can leave out the qualification for find_if too, but perhaps it makes sense to qualify it anyway).
    coffee_cups likes this.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Jun 2013
    Posts
    25
    thanks alot!:D

    1) ah was just a typo sry:) I think the issue is me making wrong use of the bool found thus having 0 returned all time:/
    basic example:
    Code:
    #include <stdio.h>
    #include <string>
    #include <iostream>    
    #include <algorithm>   
    #include <vector>    
    
    
    struct singleFile {
        std::string filename;
    };
     
    std::vector<singleFile>myStorage;
     
    
    std::string subject = "file2";
    auto it = std::find_if(begin(myStorage), end(myStorage), [&subject](const singleFile& f)
    {
        return f.filename == subject;
    });
    
    bool found = (it != std::end(myStorage));
    
    int main()
    {
        myStorage.push_back(singleFile());
        myStorage.push_back(singleFile());
        myStorage.push_back(singleFile());
        myStorage[0].filename = "file0";
        myStorage[1].filename = "file1";
        myStorage[2].filename = "file2";
        if (found) std::cout << "Found!" << std::endl;
        return 0;
    }

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    22,271
    You should move the code starting from the declaration of the vector named myStorage into the main function. They don't belong at file scope.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  5. #5
    Registered User
    Join Date
    Jun 2013
    Posts
    25
    meh I'm stupid... bool found = (it != std::end(myStorage)); obviously won't return anything but 0 if the declaration is before the push+naming of the files... >< thx again

  6. #6
    Registered User
    Join Date
    Nov 2012
    Posts
    38
    hm one final question:
    sounds quite simple, but how can I actually get the position or value of the iterator of my vector struct?

    for instance
    Code:
        if (found) 
             std::cout << *it << std::endl;
    would work if it was vector<int>myVec; instead of vector<singleFile>myVec;

    basically all I would need is the position of the found object (i.e. 1 for vector[1]) =/

  7. #7
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    22,271
    Because the iterator is a random access iterator, you can just subtract begin(myStorage) from it.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 3
    Last Post: 07-20-2013, 09:34 AM
  2. Searching for an int in a vector
    By go_loco in forum C++ Programming
    Replies: 14
    Last Post: 04-22-2010, 07:11 PM
  3. Searching a vector/struct for strings?
    By Glauber in forum C++ Programming
    Replies: 17
    Last Post: 05-27-2008, 04:57 PM
  4. searching an element of a vector
    By strickey in forum C++ Programming
    Replies: 4
    Last Post: 02-15-2005, 08:29 AM
  5. Searching the contents of a vector
    By Sparkle1984 in forum C++ Programming
    Replies: 4
    Last Post: 10-16-2003, 08:14 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21