Click here to Skip to main content
16,012,316 members
Home / Discussions / C / C++ / MFC
   

C / C++ / MFC

 
AnswerRe: std::set to store pointers Pin
CPallini11-Jun-07 21:36
mveCPallini11-Jun-07 21:36 
GeneralRe: std::set to store pointers Pin
ginjikun11-Jun-07 23:15
ginjikun11-Jun-07 23:15 
GeneralRe: std::set to store pointers Pin
Arman S.11-Jun-07 23:20
Arman S.11-Jun-07 23:20 
GeneralRe: std::set to store pointers Pin
ginjikun12-Jun-07 0:56
ginjikun12-Jun-07 0:56 
GeneralRe: std::set to store pointers Pin
Arman S.12-Jun-07 3:33
Arman S.12-Jun-07 3:33 
GeneralRe: std::set to store pointers Pin
ginjikun12-Jun-07 14:35
ginjikun12-Jun-07 14:35 
GeneralRe: std::set to store pointers Pin
Tim Paaschen11-Jun-07 23:43
Tim Paaschen11-Jun-07 23:43 
GeneralRe: std::set to store pointers Pin
CPallini12-Jun-07 0:56
mveCPallini12-Jun-07 0:56 
You have to pass your own comparer function object, otherwise the set will use the standard pointer comparison one (based on their addresses). I made a little test. Code is not elegant but it shows the point
// test class, object ordering is based on _c member.
class Atest
{
	friend struct comp;
public:
	Atest(int i, char c)
	{
		_i = i;
		_c = c;
	}
	bool comp( Atest * pb)
	{
		return ( _c < pb->_c) ? true : false;
	}
	void dump(FILE * fp)
	{
		fprintf(fp,  "{%d,%c}\n", _i, _c);
	}

	
private:
	int _i;
	char _c;
};

// The comparison function object
struct comp : public binary_function<Atest *, Atest *, bool> 
{
    bool operator()(const Atest * pa, const Atest * pb) const
		{
			return (pa->_c < pb->_c) ? true: false;
		}
};


// test program
int main(int argc, char* argv[])
{
	Atest at1(5,'v'), at2(6,'a');
        // note the comparison font object passed to set ctor
	std::set< Atest *, comp> s;
	s.insert(&at1);
	s.insert(&at2);
	// let's verify ordering
	std::set< Atest *, comp>::iterator it;
	for (it = s.begin(); it != s.end(); it++)
	{
		(*it)->dump(stdout);
	}
        return 0;
}



Smile | :)

If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler.
-- Alfonso the Wise, 13th Century King of Castile.

GeneralRe: std::set to store pointers Pin
ginjikun12-Jun-07 14:37
ginjikun12-Jun-07 14:37 
Questionhow apply action on list control box item Pin
Y_Kaushik11-Jun-07 21:28
Y_Kaushik11-Jun-07 21:28 
AnswerRe: how apply action on list control box item Pin
Hamid_RT11-Jun-07 21:55
Hamid_RT11-Jun-07 21:55 
GeneralRe: how apply action on list control box item Pin
Y_Kaushik12-Jun-07 2:38
Y_Kaushik12-Jun-07 2:38 
GeneralRe: how apply action on list control box item Pin
Hamid_RT12-Jun-07 3:30
Hamid_RT12-Jun-07 3:30 
AnswerRe: how apply action on list control box item Pin
tanvon malik12-Jun-07 0:40
tanvon malik12-Jun-07 0:40 
QuestionRe: how apply action on list control box item Pin
David Crow12-Jun-07 2:55
David Crow12-Jun-07 2:55 
QuestionReading lines from a multiline control Pin
tom groezer11-Jun-07 21:27
tom groezer11-Jun-07 21:27 
AnswerRe: Reading lines from a multiline control Pin
Naveen11-Jun-07 22:21
Naveen11-Jun-07 22:21 
AnswerRe: Reading lines from a multiline control Pin
David Crow12-Jun-07 2:49
David Crow12-Jun-07 2:49 
QuestionDrag-drop Pin
whiteclouds11-Jun-07 20:16
whiteclouds11-Jun-07 20:16 
QuestionRe: Drag-drop Pin
Hamid_RT11-Jun-07 20:28
Hamid_RT11-Jun-07 20:28 
AnswerRe: Drag-drop Pin
whiteclouds11-Jun-07 20:37
whiteclouds11-Jun-07 20:37 
AnswerRe: Drag-drop Pin
whiteclouds12-Jun-07 0:41
whiteclouds12-Jun-07 0:41 
GeneralRe: Drag-drop Pin
Hamid_RT12-Jun-07 3:34
Hamid_RT12-Jun-07 3:34 
GeneralRe: Drag-drop Pin
whiteclouds12-Jun-07 17:10
whiteclouds12-Jun-07 17:10 
GeneralRe: Drag-drop Pin
Hamid_RT12-Jun-07 19:58
Hamid_RT12-Jun-07 19:58 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.