Help | Site Map
Connecting Tech Pros Worldwide
 
 
LinkBack Thread Tools
  #1  
Old March 27th, 2006, 06:55 AM
tehn.yit.chin@gmail.com
Guest
 
Posts: n/a
Default using find to find a member of a struct in vector

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>
#include <string>

struct abc_struct
{
std::string student;
std::string school;
};

int main()
{

std::vector<abc_struct> abc;

abc_struct temp_abc;

temp_abc.student = "John Snoden";
temp_abc.school = "Melbourne high school";
abc.push_back(temp_abc);

temp_abc.student = "Jane Doe";
temp_abc.school = "Lareto College";
abc.push_back(temp_abc);

std::vector<abc_struct>::iterator result;
temp_abc.student = "John Snoden";

result = find( abc.begin(), abc.end(), temp_abc.student);

if (result == abc.end())
{
std::cout << "NOT found\n";
}
else
{
std::cout << "found\n";
}
}


When I try to build it, the compiler complains for "no match for for
operation ==' in std_algo.h. I don't fully understand what the error
message is trying to say. Am I suppose to provide a camparison function
for the abc_struct? If I am, is it suppose to be a member function of
the abc_struct class?

many thanks for your assistance.

  #2  
Old March 27th, 2006, 07:35 AM
Sunil Varma
Guest
 
Posts: n/a
Default Re: using find to find a member of a struct in vector


tehn.yit.chin@gmail.com wrote:[color=blue]
> 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>
> #include <string>
>
> struct abc_struct
> {
> std::string student;
> std::string school;
> };
>
> int main()
> {
>
> std::vector<abc_struct> abc;
>
> abc_struct temp_abc;
>
> temp_abc.student = "John Snoden";
> temp_abc.school = "Melbourne high school";
> abc.push_back(temp_abc);
>
> temp_abc.student = "Jane Doe";
> temp_abc.school = "Lareto College";
> abc.push_back(temp_abc);
>
> std::vector<abc_struct>::iterator result;
> temp_abc.student = "John Snoden";
>
> result = find( abc.begin(), abc.end(), temp_abc.student);
>
> if (result == abc.end())
> {
> std::cout << "NOT found\n";
> }
> else
> {
> std::cout << "found\n";
> }
> }
>
>
> When I try to build it, the compiler complains for "no match for for
> operation ==' in std_algo.h. I don't fully understand what the error
> message is trying to say. Am I suppose to provide a camparison function
> for the abc_struct? If I am, is it suppose to be a member function of
> the abc_struct class?
>
> many thanks for your assistance.[/color]

find() function assumes you to provide an operator==() overloaded
function, in case the search key not a basic type.
You are supposed to provide a member function in abc_struct structure
overloading == operator.
And also you are trying to compare just string to abc_struct structure
in the find() call.

  #3  
Old March 27th, 2006, 08:25 AM
Gernot Frisch
Guest
 
Posts: n/a
Default Re: using find to find a member of a struct in vector

[color=blue]
> When I try to build it, the compiler complains for "no match for for
> operation ==' in std_algo.h. I don't fully understand what the error
> message is trying to say.[/color]

So, why don't you post the compiler error?


  #4  
Old March 27th, 2006, 09:05 AM
Sumit Rajan
Guest
 
Posts: n/a
Default Re: using find to find a member of a struct in vector


tehn.yit.chin@gmail.com wrote:[color=blue]
> 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>
> #include <string>
>
> struct abc_struct
> {
> std::string student;
> std::string school;
> };
>[/color]

You could add:
bool operator ==(const abc_struct& lhs, const abc_struct& rhs)
{
return (lhs.student == rhs.student); //i'm assuming you do not want
to compare schools here
}


[color=blue]
> int main()
> {
>
> std::vector<abc_struct> abc;
>
> abc_struct temp_abc;
>
> temp_abc.student = "John Snoden";
> temp_abc.school = "Melbourne high school";
> abc.push_back(temp_abc);
>
> temp_abc.student = "Jane Doe";
> temp_abc.school = "Lareto College";
> abc.push_back(temp_abc);
>
> std::vector<abc_struct>::iterator result;
> temp_abc.student = "John Snoden";
>
> result = find( abc.begin(), abc.end(), temp_abc.student);[/color]

result = find( abc.begin(), abc.end(), temp_abc);
[color=blue]
>
> if (result == abc.end())
> {
> std::cout << "NOT found\n";
> }
> else
> {
> std::cout << "found\n";
> }
> }
>[/color]

Regards,
Sumit.

  #5  
Old March 27th, 2006, 03:05 PM
Daniel T.
Guest
 
Posts: n/a
Default Re: using find to find a member of a struct in vector

In article <1143441709.867871.228430@i40g2000cwc.googlegroups .com>,
tehn.yit.chin@gmail.com wrote:
[color=blue]
> 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>
> #include <string>
>
> struct abc_struct
> {
> std::string student;
> std::string school;
> };
>
> int main()
> {
>
> std::vector<abc_struct> abc;
>
> abc_struct temp_abc;
>
> temp_abc.student = "John Snoden";
> temp_abc.school = "Melbourne high school";
> abc.push_back(temp_abc);
>
> temp_abc.student = "Jane Doe";
> temp_abc.school = "Lareto College";
> abc.push_back(temp_abc);
>
> std::vector<abc_struct>::iterator result;
> temp_abc.student = "John Snoden";
>
> result = find( abc.begin(), abc.end(), temp_abc.student);
>
> if (result == abc.end())
> {
> std::cout << "NOT found\n";
> }
> else
> {
> std::cout << "found\n";
> }
> }
>
>
> When I try to build it, the compiler complains for "no match for for
> operation ==' in std_algo.h. I don't fully understand what the error
> message is trying to say. Am I suppose to provide a camparison function
> for the abc_struct? If I am, is it suppose to be a member function of
> the abc_struct class?
>
> many thanks for your assistance.[/color]

The obvious patch is to simply add an op== that compares an abc_struct
to a string:

bool operator==( const abc_struct& abc, const string& str ) {
return abc.student == str;
}

But, what if you later want to look for abc_structs that have a
particular value for 'school'? In that case, your best bet would be to
provide a function that gets the student or school out of abc_struct and
use the op== provided for the string class. For this though, you can't
use 'find' you must use 'find_if' instead...

struct comp_student : std::binary_function<abc_struct, std::string, bool>
{
bool operator()( const abc_struct& abc, const std::string& name )
const {
return abc.student == name;
}
};

result = find_if( abc.begin(), abc.end(),
std::bind2nd( comp_student(), "John Snoden" ) );

However, since you have to create a structure anyway...

struct has_name
{
std::string name_sought;
has_name( std::string n ): name_sought( n ) { }
bool operator()( const abc_struct& abc ) const {
return abc.student == name_sought;
}
};


result = find_if( abc.begin(), abc.end(), has_name( "John Snoden" ) );



--
Magic depends on tradition and belief. It does not welcome observation,
nor does it profit by experiment. On the other hand, science is based
on experience; it is open to correction by observation and experiment.
  #6  
Old March 27th, 2006, 11:05 PM
tehn.yit.chin@gmail.com
Guest
 
Posts: n/a
Default Re: using find to find a member of a struct in vector

A copy of the compiler output is ....

[tehn-yit.chin@riesling Promotion_Manager]$ g++ -o abc test.cpp
/usr/lib/gcc/i386-redhat-linux/3.4.3/../../../../include/c++/3.4.3/bits/stl_algo.h:
In function `_RandomAccessIterator std::find(_RandomAccessIterator,
_RandomAccessIterator, const _Tp&, std::random_access_iterator_tag)
[with _RandomAccessIterator = __gnu_cxx::__normal_iterator<abc_struct*,
std::vector<abc_struct, std::allocator<abc_struct> > >, _Tp =
std::basic_string<char, std::char_traits<char>, std::allocator<char>[color=blue]
>]':[/color]
/usr/lib/gcc/i386-redhat-linux/3.4.3/../../../../include/c++/3.4.3/bits/stl_algo.h:314:
instantiated from `_InputIterator std::find(_InputIterator,
_InputIterator, const _Tp&) [with _InputIterator =
__gnu_cxx::__normal_iterator<abc_struct*, std::vector<abc_struct,
std::allocator<abc_struct> > >, _Tp = std::string]'
test.cpp:31: instantiated from here
/usr/lib/gcc/i386-redhat-linux/3.4.3/../../../../include/c++/3.4.3/bits/stl_algo.h:207:
error: no match for 'operator==' in
'(&__first)->__gnu_cxx::__normal_iterator<_Iterator,
_Container>::operator* [with _Iterator = abc_struct*, _Container =
std::vector<abc_struct, std::allocator<abc_struct> >]() == __val'
/usr/lib/gcc/i386-redhat-linux/3.4.3/../../../../include/c++/3.4.3/bits/stl_algo.h:314:
instantiated from `_InputIterator std::find(_InputIterator,
_InputIterator, const _Tp&) [with _InputIterator =
__gnu_cxx::__normal_iterator<abc_struct*, std::vector<abc_struct,
std::allocator<abc_struct> > >, _Tp = std::string]'
test.cpp:31: instantiated from here
/usr/lib/gcc/i386-redhat-linux/3.4.3/../../../../include/c++/3.4.3/bits/stl_algo.h:211:
error: no match for 'operator==' in
'(&__first)->__gnu_cxx::__normal_iterator<_Iterator,
_Container>::operator* [with _Iterator = abc_struct*, _Container =
std::vector<abc_struct, std::allocator<abc_struct> >]() == __val'
/usr/lib/gcc/i386-redhat-linux/3.4.3/../../../../include/c++/3.4.3/bits/stl_algo.h:314:
instantiated from `_InputIterator std::find(_InputIterator,
_InputIterator, const _Tp&) [with _InputIterator =
__gnu_cxx::__normal_iterator<abc_struct*, std::vector<abc_struct,
std::allocator<abc_struct> > >, _Tp = std::string]'
test.cpp:31: instantiated from here
/usr/lib/gcc/i386-redhat-linux/3.4.3/../../../../include/c++/3.4.3/bits/stl_algo.h:215:
error: no match for 'operator==' in
'(&__first)->__gnu_cxx::__normal_iterator<_Iterator,
_Container>::operator* [with _Iterator = abc_struct*, _Container =
std::vector<abc_struct, std::allocator<abc_struct> >]() == __val'
/usr/lib/gcc/i386-redhat-linux/3.4.3/../../../../include/c++/3.4.3/bits/stl_algo.h:314:
instantiated from `_InputIterator std::find(_InputIterator,
_InputIterator, const _Tp&) [with _InputIterator =
__gnu_cxx::__normal_iterator<abc_struct*, std::vector<abc_struct,
std::allocator<abc_struct> > >, _Tp = std::string]'
test.cpp:31: instantiated from here
/usr/lib/gcc/i386-redhat-linux/3.4.3/../../../../include/c++/3.4.3/bits/stl_algo.h:219:
error: no match for 'operator==' in
'(&__first)->__gnu_cxx::__normal_iterator<_Iterator,
_Container>::operator* [with _Iterator = abc_struct*, _Container =
std::vector<abc_struct, std::allocator<abc_struct> >]() == __val'
/usr/lib/gcc/i386-redhat-linux/3.4.3/../../../../include/c++/3.4.3/bits/stl_algo.h:314:
instantiated from `_InputIterator std::find(_InputIterator,
_InputIterator, const _Tp&) [with _InputIterator =
__gnu_cxx::__normal_iterator<abc_struct*, std::vector<abc_struct,
std::allocator<abc_struct> > >, _Tp = std::string]'
test.cpp:31: instantiated from here
/usr/lib/gcc/i386-redhat-linux/3.4.3/../../../../include/c++/3.4.3/bits/stl_algo.h:227:
error: no match for 'operator==' in
'(&__first)->__gnu_cxx::__normal_iterator<_Iterator,
_Container>::operator* [with _Iterator = abc_struct*, _Container =
std::vector<abc_struct, std::allocator<abc_struct> >]() == __val'
/usr/lib/gcc/i386-redhat-linux/3.4.3/../../../../include/c++/3.4.3/bits/stl_algo.h:314:
instantiated from `_InputIterator std::find(_InputIterator,
_InputIterator, const _Tp&) [with _InputIterator =
__gnu_cxx::__normal_iterator<abc_struct*, std::vector<abc_struct,
std::allocator<abc_struct> > >, _Tp = std::string]'
test.cpp:31: instantiated from here
/usr/lib/gcc/i386-redhat-linux/3.4.3/../../../../include/c++/3.4.3/bits/stl_algo.h:231:
error: no match for 'operator==' in
'(&__first)->__gnu_cxx::__normal_iterator<_Iterator,
_Container>::operator* [with _Iterator = abc_struct*, _Container =
std::vector<abc_struct, std::allocator<abc_struct> >]() == __val'
/usr/lib/gcc/i386-redhat-linux/3.4.3/../../../../include/c++/3.4.3/bits/stl_algo.h:314:
instantiated from `_InputIterator std::find(_InputIterator,
_InputIterator, const _Tp&) [with _InputIterator =
__gnu_cxx::__normal_iterator<abc_struct*, std::vector<abc_struct,
std::allocator<abc_struct> > >, _Tp = std::string]'
test.cpp:31: instantiated from here
/usr/lib/gcc/i386-redhat-linux/3.4.3/../../../../include/c++/3.4.3/bits/stl_algo.h:235:
error: no match for 'operator==' in
'(&__first)->__gnu_cxx::__normal_iterator<_Iterator,
_Container>::operator* [with _Iterator = abc_struct*, _Container =
std::vector<abc_struct, std::allocator<abc_struct> >]() == __val'
[tehn-yit.chin@riesling Promotion_Manager]$

  #7  
Old March 27th, 2006, 11:45 PM
tehn.yit.chin@gmail.com
Guest
 
Posts: n/a
Default Re: using find to find a member of a struct in vector

Thanks guys for your respones. This has cleared this up enormously.

cheers,
tyc

  #8  
Old March 29th, 2006, 07:25 AM
Gernot Frisch
Guest
 
Posts: n/a
Default Re: using find to find a member of a struct in vector


error: no match for 'operator==' in
[with _Iterator = abc_struct*, _Container =
std::vector<abc_struct, std::allocator<abc_struct> ]

provide this one.


 

Bookmarks


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are Off
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

What is Bytes?

We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights. Get the best answers to your questions from over network members.
Post your question now . . .
It's fast and it's free

Popular Articles