DevX Home Today's Headlines   Articles Archive   Tip Bank   Forums

# Thread: finding 2 elements in one vector iteration

1. Registered User
Join Date
Oct 2010
Posts
119

## finding 2 elements in one vector iteration

Hello All

is there anyway I can find two elements in a vector in one iteration? e-g

if(find(this) AND find(this))
{
// then do this
}

Thanks

2. technically speaking, every find() call is an iteration so if you have two find() calls in an if expression, you have two iterations. However, I can't see any other way to accomplish your task so here's what you can do:

if (find((myvec.begin(), myvec.end(), myval)!=myvec.end())==(find(myvec.begin(), myvec.end(), anotherval))!=myvec.end())

...

3. Registered User
Join Date
Oct 2010
Posts
119
Danny!

There is actually a vector of type string declared inside a struct and there are two string type variables declared inside the same struct. I want to check if both the string type variables are present in the string type vector? Could you please assist me on that.

Code:
```struct aStruct
{
std::vector<std::string> vect;

std::string var1;
std::string var2;
};```
Many Thanks

4. Registered User
Join Date
Oct 2007
Posts
369
define
Code:
```bool operator==(const aStruct& lhs, const aStruct& rhs) {
return ((lhs.var1 == rhs.var1) && (lhs.var2==rhs.var2) && (lhs.vect==rhs.vect));
}```
Then
Code:
```bool foundBoth(const std::vector<aStruct>& myvect,
const aStruct& myval,
const aStruct& anotherval) {
bool found1st = false;
bool found2nd = false;
for (std::vector<aStruct>::const_iterator it=myvect.begin();
(it != myvect.end()) && !(found1st && found2nd));
++it) {
if (*it == myval) findFirst = true;
if (*it == anotherval) findSecond = true;
}

return (findFirst && findSecond);
}```

5. Registered User
Join Date
Oct 2010
Posts
119
Thanks hendrixj.

I have modified my code according to that of yours and it seems to be working :)

THANK YOU

6. I'd use STL find() but the princinple is the same:

#include <algorithm>
using std::find;

if ((find(vect.begin(), vect.end(), var1)!=vect.end()) && (find(vect.begin(), vect.end(), var2)!=vect.end())
{//then both strings are present.}
else ...

This way you don't have to overload anything; STL has already done it for you and the code is most likely more efficient because it short-circuits the whole expression.

7. Registered User
Join Date
Oct 2010
Posts
119
Thanks Danny :)

8. Senior Member
Join Date
Dec 2003
Posts
3,366
Hendrixj's code short circuits when both are found, it looks at the data the same number of times as split find commands but does an extra comparison sometimes. Unfortunately, if that comparison is complex (strings, classes, etc) it can be a LOT of slow, hidden code behind the scenes.

A slight modification lets his code shortcircuit a little better (I think). Unless I am having one of *those* days and mucked it up. I dunno if it beats find or not, find is pretty slick, but anyway...

-----------------------
bool foundBoth(const std::vector<aStruct>& myvect,
const aStruct& myval,
const aStruct& anotherval) {
bool found1st = false;
bool found2nd = false;
for (std::vector<aStruct>::const_iterator it=myvect.begin();
(it != myvect.end()) ); //get rid of the found test here.
++it) {

if (!findfirst && *it == myval) findFirst = true; //short this one
if (!findsecond *it == anotherval) findSecond = true; //short this one
if(findfirst && findsecond) return (true, true); //done, get outta here
}

return (findFirst && findSecond);
}

9. Registered User
Join Date
Oct 2010
Posts
119
Alright THANK YOU so much jonnin :)

#### Posting Permissions

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

 FAQ Latest Articles Java .NET XML Database Enterprise