473,396 Members | 1,853 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

using std::find

If I have a class
MyClass
{
...bunch o data and methods
int x;
};

and a stl container of MyClass objects

Is there a way to use std::find to get all the elements whose member x
is of om value?

All the example on the net are of a collection of some built in type
for simplicity, but do not show how to use a member of your own type
as the value being sought.

Mar 14 '07 #1
8 8604
brekehan wrote:
If I have a class
MyClass
{
...bunch o data and methods
int x;
};

and a stl container of MyClass objects

Is there a way to use std::find to get all the elements whose member x
is of om value?
"om" value? What's "om" value? Sorry, English is not my mother
tongue.
All the example on the net are of a collection of some built in type
for simplicity, but do not show how to use a member of your own type
as the value being sought.
What does your favourite C++ book say? Don't you have a copy of
Josuttis' "The C++ Standard Library"? You can define your own
functor to compare the value of the 'x' member with the given value
and then call

std::find(container.begin(), container.end(), yourFunctor(42));

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Mar 14 '07 #2
Victor Bazarov wrote:
brekehan wrote:
>If I have a class
MyClass
{
...bunch o data and methods
int x;
};

and a stl container of MyClass objects

Is there a way to use std::find to get all the elements whose member x
is of om value?

"om" value? What's "om" value? Sorry, English is not my mother
tongue.
>All the example on the net are of a collection of some built in type
for simplicity, but do not show how to use a member of your own type
as the value being sought.

What does your favourite C++ book say? Don't you have a copy of
Josuttis' "The C++ Standard Library"? You can define your own
functor to compare the value of the 'x' member with the given value
and then call

std::find(container.begin(), container.end(), yourFunctor(42));

V
I think he wants remove_copy_if() with negated functor?

std::vector<MyClassv;

struct hasElement: public std::unary_function<MyClass, bool>
{
bool operator()(const MyClass& elem) const
{
return elem.x == x;
}
hasElement(int x_) : x(x_) { }
private
int x;
};

std::remove_copy_if(container.begin(), container.end(),
std::back_inserter(v),
std::not(hasElement(42))_;
Mar 14 '07 #3
red floyd wrote:
Victor Bazarov wrote:
>brekehan wrote:
>>If I have a class
MyClass
{
...bunch o data and methods
int x;
};

and a stl container of MyClass objects

Is there a way to use std::find to get all the elements whose
member x is of om value?

"om" value? What's "om" value? Sorry, English is not my mother
tongue.
>>All the example on the net are of a collection of some built in type
for simplicity, but do not show how to use a member of your own type
as the value being sought.

What does your favourite C++ book say? Don't you have a copy of
Josuttis' "The C++ Standard Library"? You can define your own
functor to compare the value of the 'x' member with the given value
and then call

std::find(container.begin(), container.end(), yourFunctor(42));

V

I think he wants remove_copy_if() with negated functor?

std::vector<MyClassv;

struct hasElement: public std::unary_function<MyClass, bool>
{
bool operator()(const MyClass& elem) const
{
return elem.x == x;
}
hasElement(int x_) : x(x_) { }
private
int x;
};

std::remove_copy_if(container.begin(), container.end(),
std::back_inserter(v),
std::not(hasElement(42))_;
I couldn't find the word "remove" in the original post. Only "get"
and "find". For that 'find' is called in a 'while' loop and the
beginning of the range should keep changing:

it = c.begin();
while (true) {
it = std::find(it, c.end(), hasElement(42));
if (it != c.end()) {
// do something with 'it' or with '*it' or with it->x or...
}
else
break;
}

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Mar 14 '07 #4
On 14 Mar 2007 10:48:40 -0700 in comp.lang.c++, "brekehan"
<cp***@austin.rr.comwrote,
>If I have a class
MyClass
{
...bunch o data and methods
int x;
};

and a stl container of MyClass objects

Is there a way to use std::find to get all the elements whose member x
is of om value?
I think you might end up using std::find_if() with a predicate that
selects the values you want. The predicate could be a boost::lambda
expression, or it could be hand crafted. Since find() and find_if()
return just one thing, you could loop to find them all.

Some random fragments:

bool ifx(MyClass const & c, int x)
{ return c.x == x; }

for (iterator_type it = container.begin(); it != container.end();
it = find_if(it, container.end(), std::bind_2nd(ifx, om))) {
if (it != container.end())
cout << *it++;
}

Mar 14 '07 #5
On Mar 14, 12:53 pm, "Victor Bazarov" <v.Abaza...@comAcast.netwrote:
brekehan wrote:
If I have a class
MyClass
{
...bunch o data and methods
int x;
};
and a stl container of MyClass objects
Is there a way to use std::find to get all the elements whose member x
is of om value?

"om" value? What's "om" value? Sorry, English is not my mother
tongue.
All the example on the net are of a collection of some built in type
for simplicity, but do not show how to use a member of your own type
as the value being sought.

What does your favourite C++ book say? Don't you have a copy of
Josuttis' "The C++ Standard Library"? You can define your own
functor to compare the value of the 'x' member with the given value
and then call

std::find(container.begin(), container.end(), yourFunctor(42));

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask

om = some. this keyboard sucks. Sorry. I also used to have three
really good STL references, but the girlfriend got angry that I love
the computer more than her and disposed of most of my office. I plan
on getting some more, meantime I got a deadline to meet.

I see that a few of the functions in <algorithmor the container
itself take the functor argument you mentioned, but if I understand
correctly this is passed 1 argument of the type contained. remove_if
would be awesome, because that is exatly what my goal is. Remove an
element if it has a member (we'll call it TYPE m_type) of some type.
However, I need to have two arguments...the element and the TYPE to
compare it with, so my functor can say is this element of this type?
Otherwise, I have to write a functor for every possible value of
m_type and there is no telling how many there may be in the future.

Mar 14 '07 #6
On 14 Mar 2007 11:35:52 -0700 in comp.lang.c++, "brekehan"
<cp***@austin.rr.comwrote,
>However, I need to have two arguments...the element and the TYPE to
compare it with, so my functor can say is this element of this type?
Otherwise, I have to write a functor for every possible value of
m_type and there is no telling how many there may be in the future.
Solving that part of the problem is the job of the std::bind* family.
Or else something more modern and powerful like boost::bind.

Mar 14 '07 #7
Victor Bazarov wrote:
red floyd wrote:
>Victor Bazarov wrote:
>>brekehan wrote:
If I have a class
MyClass
{
...bunch o data and methods
int x;
};

and a stl container of MyClass objects

Is there a way to use std::find to get all the elements whose
member x is of om value?
"om" value? What's "om" value? Sorry, English is not my mother
tongue.

All the example on the net are of a collection of some built in type
for simplicity, but do not show how to use a member of your own type
as the value being sought.
What does your favourite C++ book say? Don't you have a copy of
Josuttis' "The C++ Standard Library"? You can define your own
functor to compare the value of the 'x' member with the given value
and then call

std::find(container.begin(), container.end(), yourFunctor(42));

V
I think he wants remove_copy_if() with negated functor?

std::vector<MyClassv;

struct hasElement: public std::unary_function<MyClass, bool>
{
bool operator()(const MyClass& elem) const
{
return elem.x == x;
}
hasElement(int x_) : x(x_) { }
private
int x;
};

std::remove_copy_if(container.begin(), container.end(),
std::back_inserter(v),
std::not(hasElement(42))_;

I couldn't find the word "remove" in the original post. Only "get"
and "find". For that 'find' is called in a 'while' loop and the
beginning of the range should keep changing:

it = c.begin();
while (true) {
it = std::find(it, c.end(), hasElement(42));
if (it != c.end()) {
// do something with 'it' or with '*it' or with it->x or...
}
else
break;
}
I thought remove_copy_if doesn't modify the original container. The
construct I gave should copy all elements with x == 42.

Mar 14 '07 #8
brekehan <cp***@austin.rr.comwrote:
If I have a class
MyClass
{
...bunch o data and methods
int x;
};

and a stl container of MyClass objects

Is there a way to use std::find to get all the elements whose member x
is of om value?
I believe std::find by default uses std::less<T>, which by default uses
operator<. So, you can provide your own operator<, or explicitly
provide a comparison functor to find().

Then, one way would be to sort your container, then use std::lower_bound
and std::upper_bound.

--
Marcus Kwok
Replace 'invalid' with 'net' to reply
Mar 14 '07 #9

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
by: zhou | last post by:
Hi there, We have a compiler specific issue which requires us to force template instantiation. This works fine. The problem comes when I try using std:find() on vector. Since vector has no member...
3
by: Peter Meier | last post by:
Hello, I have a question. I have the following construct: std::vector < std::vector < std::vector< AsMarkerMatchMatrix >> > m_matchMatrices; If I run a std::find on that I sometimes get the...
3
by: Old Wolf | last post by:
In the documentation for std::find, it says: find returns the first iterator i in the range [first, last) for which the following condition holds: *i == value. However in my compiler's...
1
by: Adam Teasdale Hartshorne | last post by:
I would be extremely grateful if somebody could tell me what as I getting wrong with this little bit of code to find the index of a particular element in a std::vector std::vector<int>...
18
by: ma740988 | last post by:
Trying to get more acclimated with the use of function objects. As part of my test, consider: # include <vector> # include <iostream> # include <algorithm> #include <stdexcept> #include...
7
by: tehn.yit.chin | last post by:
I am trying to experiment <algorithm>'s find to search for an item in a vector of struct. My bit of test code is shown below. #include <iostream> #include <vector> #include <algorithm>...
7
by: Bit byte | last post by:
I have the ff code: list<string> *m_alive_list ; list<string>::iterator my_iter; my_iter = find(m_alive_list->begin(), m_alive_list->end, string(inbox) ; Compiler barfs on 2nd line with this...
4
by: Anonymous | last post by:
I ahve a vector of doubles taht I need to extract values from. I was just about to use the STL find() algo, but I have a couple of questions: first: can you specify the tolerance threshold to...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.