|
-
February 26th, 2006, 11:44 AM
#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
-
February 26th, 2006, 11:58 AM
#2
Re: Matching a word in any element of a vector?
[ redirected ]
Regards,
Siddhartha
-
February 27th, 2006, 01:00 AM
#3
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.
Can you help me with my homework assignment?, Before you post!, Use code tags, How to post!, Codeguru technical FAQs, C++ FAQ Lite, Stroustrup: C++ Style and Technique FAQ, Guru of the Week, Comeau C and C++ FAQs, Comeau C++ Templates FAQs, CUJ @ DDJ, Spam threshold
My Blogs : Learning C++ is fun | Abnegator's reflections
Open Threads : C++ Aha! Moments | Nature of work in C++?
-
February 27th, 2006, 03:19 AM
#4
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 &&).
-
February 27th, 2006, 01:47 PM
#5
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.
-
February 28th, 2006, 01:10 PM
#6
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|