Re: STL algorithms with vectors of c++ objects
From: Andy (arb70sok_at_hotmail.com)
Date: 06/25/04
- Next message: tom_usenet: "Re: std::ostringstream, pointer to first element"
- Previous message: Carl Daniel [VC++ MVP]: "Re: Restrict template expansion to simple types?"
- In reply to: anonymous_at_discussions.microsoft.com: "Re: STL algorithms with vectors of c++ objects"
- Next in thread: Simon Trew: "Re: STL algorithms with vectors of c++ objects"
- Reply: Simon Trew: "Re: STL algorithms with vectors of c++ objects"
- Messages sorted by: [ date ] [ thread ]
Date: Fri, 25 Jun 2004 08:38:09 +0100
I think that PJ did in fact direct you straight to the source of the problem
with his post!
To expand slightly on his explanation.
Your comparison operator declaration looks like this:
// the predicate function is below...
bool sort_into_live_nodes(const CTCP_Socket &a, const CTCP_Socket &b)
{
return a.socket_node_live > b.socket_node_live;
}
This takes two const CTCP_Socket references
- however you are asking equal_range to compare one of these to an int.
Presumably you have not defined an operator in your class that allows a
const CTCP_Socket& to be compared to an int?
It looks as though what you should be doing is creating an instance of your
CTCP_Socket class in which the member socket_node_live is initialised to 1
and passing this instance as the 3rd parameter to equal_range. This was I
think PJ's advise, though he expressed it a lot more tersely. Terse is
good,
but when you are learning it is not that helpful!
i.e.
CTCP_Socket find_me;
find_me.socket_node_live = 1;
p1 = equal_range (v_sock_b, v_sock_e, find_me, sort_into_live_nodes);
If socket_node_live is a private member, which it probably ought to be,
then you will need to use the 'setter' method in your class instead of
find_me.socket_node_live = 1;
That ought to work assuming the rest of your code is OK.
HTH,
Andy
<anonymous@discussions.microsoft.com> wrote in message
news:20b2101c45934$a8149870$a001280a@phx.gbl...
> Hi - thanks for the speedy reply...
>
> what i'm trying to do is to get the pair of iterators
> that point to the objects whose 'socket_node_live'
> member equals 1. I've already sorted them so if i have 3
> live nodes out of 6, the 3 live ones (indicated by
> socket_node_live ==1) will be at the front of the vector.
> What i need are iterators to the 1st and last object
> members (socket_node_live ) that contain 1.
>
> Sounds simple eh?
>
> Looks like i'm not using the comparator correctly....
>
> >-----Original Message-----
> ><anonymous@discussions.microsoft.com> wrote in message
> >news:2008001c4592b$0e791aa0$a601280a@phx.gbl...
> >
> >> I have a vector of objects which i want to sort etc on
> >> various members values.
> >>
> >> I've used the sort algorithm ok using my own predicate
> >> function as shown below (snips of code):
> >>
> >> vector<CTCP_Socket>::iterator
> >> v_sock_b,v_sock_e,v_sock_cur;
> >>
> >> pair<vector<CTCP_Socket>::iterator,
> >> vector<CTCP_Socket>::iterator> p1;
> >>
> >> v_sock_b = vTCP_Alt_Connections.begin();
> >> v_sock_e = vTCP_Alt_Connections.end();
> >>
> >> sort (v_sock_b, v_sock_e, sort_into_live_nodes);
> >>
> >> // the predicate function is below...
> >> bool sort_into_live_nodes(const CTCP_Socket &a, const
> >> CTCP_Socket &b)
> >> {
> >> return a.socket_node_live > b.socket_node_live;
> >> }
> >>
> >> the sort function works ok but if i want to find a pair
> >> using equal_range as follows :
> >>
> >> p1 = equal_range (v_sock_b, v_sock_e, 1,
> >> sort_into_live_nodes);
> >>
> >> I get a compilation errors as follows:
> >>
> >> Compiling...
> >> CVtas.cpp
> >> c:\program files\microsoft visual studio\vc98
> >> \include\algorithm(949) : error C2664: 'bool (const
> class
> >> CTCP_Socket &,const class CTCP_Socket &)' : cannot
> >> convert parameter 2 from 'const int' to 'const class
> >> CTCP_Socket &'
> >
> >You're comparing elements against the third argument
> >(integer value 1). Looks to me like you haven't defined
> >your comparator to take a CTCP_Socket and an int.
> >Better yet, you should replace the 1 with a value of
> >type CTCP_Socket.
> >
> >HTH,
> >
> >P.J. Plauger
> >Dinkumware, Ltd.
> >http://www.dinkumware.com
> >
> >
> >.
> >
- Next message: tom_usenet: "Re: std::ostringstream, pointer to first element"
- Previous message: Carl Daniel [VC++ MVP]: "Re: Restrict template expansion to simple types?"
- In reply to: anonymous_at_discussions.microsoft.com: "Re: STL algorithms with vectors of c++ objects"
- Next in thread: Simon Trew: "Re: STL algorithms with vectors of c++ objects"
- Reply: Simon Trew: "Re: STL algorithms with vectors of c++ objects"
- Messages sorted by: [ date ] [ thread ]
Relevant Pages
|