CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 6 of 6
  1. #1
    Join Date
    Feb 2006
    Posts
    1

    Matching a word in any element of a vector?

    hello, i can match up a word in an specific element of a vector and then perform a action if all the elements match the specific word. the vector is called 'tokens'.
    The problem is i want to able to go through a vector of any size and be able to match up one word (or more) to any element of the vector, then to perform the action.


    for ( int i=0; i<tokens.size();i++)
    {
    if (tokens[i] == "MY" && tokens[i+1] =="NAME")
    {
    // then perform a action......

    }
    cout << myResponse << endl;
    fileout << myResponse ;
    }

    thanks for anyhelp.
    V

  2. #2
    Join Date
    Oct 2002
    Location
    Germany
    Posts
    6,205

    Re: Matching a word in any element of a vector?

    [ redirected ]

    Regards,
    Siddhartha

  3. #3
    Join Date
    Feb 2005
    Location
    "The Capital"
    Posts
    5,306

    Re: Matching a word in any element of a vector?

    You can use the std::find algorithm in a loop. It takes the begin and end iterator for the container and find the value passed as the third argument. Returns the iterator position where it first finds the occurence. You can then input that iterator position as the begin and the end one as end and search for that item again.

    Using a multimap might prove better where the key would be these strings. You can find the range in one shot using equal_range().

    What is the problem you are facing? What you are trying to do will also work? Regards.

  4. #4
    Join Date
    Feb 2006
    Location
    Croatia - Zagreb
    Posts
    459

    Re: Matching a word in any element of a vector?

    you can't do that on char, you must use string if you want to do '==' operations, and in your code u must use in for loop
    for (int c=0;c<tokens.size();c+=2) beacuse you are using it to check the word[c] and word[c+1] (operator &&).

  5. #5
    Join Date
    Oct 2000
    Location
    London, England
    Posts
    4,773

    Re: Matching a word in any element of a vector?

    You will have an access violation on the last test where you read one past the last element of the vector.

    There are many algorithms but there is no for_each_if. You could write one or you can just loop where you need it.

    Writing a for_each_if would look something like this:

    Code:
    template < typename FwdIter, typename Op, typename Pred >
    Op for_each_if( FwdIter first, FwdIter last, Op op, Pred pred )
    {
       for ( ; first != last; ++first )
       {
            if ( pred( *first ) )
            {
                op( first );
            }
       }
       return op;
    }
    In your case though your condition seems to be based on consecutive elements so it wouldn't work here.

  6. #6
    Join Date
    Jan 2006
    Location
    croatia
    Posts
    88

    Re: Matching a word in any element of a vector?

    for loop should be:
    Code:
    for (unsigned int i=0; i<tokens.size()-1; i+=2)

Posting Permissions

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





Click Here to Expand Forum to Full Width

Featured