Thread: Searching string within vector struct C++ 11

  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
    28,413
    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).
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    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
    28,413
    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.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    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
    28,413
    Because the iterator is a random access iterator, you can just subtract begin(myStorage) from it.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    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, 08:34 AM
  2. Searching for an int in a vector
    By go_loco in forum C++ Programming
    Replies: 14
    Last Post: 04-22-2010, 06:11 PM
  3. Searching a vector/struct for strings?
    By Glauber in forum C++ Programming
    Replies: 17
    Last Post: 05-27-2008, 03: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, 07:14 AM