Thread: Search a vector element with multiple strings

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

    Search a vector element with multiple strings

    How could I search a vector element with multiple strings

    i.e. vector.at(i) = This is a test

    How could I search that to look for the word "test" in that element?

    Thanks in advance!

  2. #2
    Registered User MutantJohn's Avatar
    Join Date
    Feb 2013
    Posts
    2,665
    Use for_each and a custom lambda.

  3. #3
    Registered User
    Join Date
    Nov 2012
    Posts
    20
    Thanks for the reply but not sure what you mean.

  4. #4
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    for_each is a standard algorithm (specified in the standard header <algorithm>) can be used to perform some action on every element of a vector. A custom lambda is one way to specify that action as being to find a particular substring.

    Look up the concepts of algorithms (in all standard versions of C++) and lambdas (in C++ 11).
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

  5. #5
    SAMARAS std10093's Avatar
    Join Date
    Jan 2011
    Location
    Nice, France
    Posts
    2,694
    As the canadian guy quoted once in his signature, simplicity is the ultimate virtue.
    Why not use string::find() ?
    I think that you already now, that the "vector.at(i)", will give you actually an std::string, which of course can contain a colletction of strings.
    Code - functions and small libraries I use


    It’s 2014 and I still use printf() for debugging.


    "Programs must be written for people to read, and only incidentally for machines to execute. " —Harold Abelson

  6. #6
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    By your question, it would seem as if you know how to search through a string normally. Now searching through a string in a vector is no different.
    A particularly easy option would be to just declare a const-reference to the string in the container, then it should reduce the problem to exactly what you appear to know how to do already.
    Code:
    const std::string s = vector.at(i);
    // Now do your search within s.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  7. #7
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    As the canadian guy quoted once in his signature, simplicity is the ultimate virtue
    There is more than one Canadian here for sure. ???

  8. #8
    SAMARAS std10093's Avatar
    Join Date
    Jan 2011
    Location
    Nice, France
    Posts
    2,694
    Quote Originally Posted by whiteflags View Post
    There is more than one Canadian here for sure. ???
    True.. The musician one, oogabooga!
    Ntoice that now he has changed his signature.
    Code - functions and small libraries I use


    It’s 2014 and I still use printf() for debugging.


    "Programs must be written for people to read, and only incidentally for machines to execute. " —Harold Abelson

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Advanced Search -> Search Multiple Content Types
    By phantomotap in forum Tech Board
    Replies: 2
    Last Post: 05-21-2011, 07:28 AM
  2. vector<vector<int> > access element.
    By nimitzhunter in forum C++ Programming
    Replies: 0
    Last Post: 01-23-2011, 05:14 AM
  3. vector remove element of element
    By Ducky in forum C++ Programming
    Replies: 6
    Last Post: 09-12-2010, 03:24 PM
  4. Replies: 5
    Last Post: 01-02-2008, 06:53 AM
  5. max element in vector
    By acosgaya in forum C++ Programming
    Replies: 3
    Last Post: 10-23-2006, 04:07 PM

Tags for this Thread