Click here to Skip to main content
15,912,837 members
Home / Discussions / C / C++ / MFC
   

C / C++ / MFC

 
GeneralShell ComboBox Pin
Nicholas Naddaf18-Jun-03 9:28
Nicholas Naddaf18-Jun-03 9:28 
GeneralRe: Shell ComboBox Pin
valikac18-Jun-03 12:49
valikac18-Jun-03 12:49 
GeneralRe: Shell ComboBox Pin
Nicholas Naddaf19-Jun-03 2:58
Nicholas Naddaf19-Jun-03 2:58 
GeneralTool bar is moving.... Pin
Shay Harel18-Jun-03 9:14
Shay Harel18-Jun-03 9:14 
GeneralC++ Filter Pin
Anton A. Loukine18-Jun-03 9:11
Anton A. Loukine18-Jun-03 9:11 
GeneralRe: C++ Filter Pin
act_x18-Jun-03 9:49
act_x18-Jun-03 9:49 
GeneralRe: C++ Filter Pin
Anton A. Loukine18-Jun-03 9:54
Anton A. Loukine18-Jun-03 9:54 
GeneralRe: C++ Filter Pin
Andrew Walker18-Jun-03 16:50
Andrew Walker18-Jun-03 16:50 
The following example shows how to use the standard library to sort an array of Data objects. You can then use find() and find_end() to find the first and last iterators within the container. It is possible to do this with arrays but the code was easier to write with vectors.

The code for handling function pointers is in the DataSorter class. This solution is extensible to any number of properties. If you need to pass in parameters have a look at binders in the STL or in Boost.

Good Luck Smile | :)

#include <iostream>
#include <algorithm>
#include <string>
#include <vector>

using namespace std;

class Data
{
public:
	Data()
		: price_(0), years_(0)
	{
	}

	Data(int price, int years, const string& country, const string& currency)
		: price_(price), years_(years), country_(country), currency_(currency)
	{
	}

	int getPrice() { return price_; }
	int getYears() { return years_; }
	string getCurrency() { return currency_; }
	string getCountry() { return country_; }

private:
	int price_;
	string country_;
	string currency_;
	int years_;
};

template<class T>
class DataSorter
{
public:
	typedef T (Data::*Method)();

	DataSorter(Method m)
		: m_(m)
	{
	}

	bool operator()(Data& a, Data& b)
	{
		return (a.*m_)() < (b.*m_)();
	}

private:
	Method m_;
};

int main()
{
	vector<Data> v;
	v.push_back( Data(10,1,"US","US") );
	v.push_back( Data(11,2,"Australia","AUD") );
	v.push_back( Data(9,4,"France","Francs") );

	// Change the method and return type passed to the DataSorter to sort any way you choose
	sort(v.begin(),v.end(),DataSorter<string>(&Data::getCountry));
	
	for(int i = 0; i < v.size(); i++)
	{
		cout << v.at(i).getCountry()  << endl;
	}
	return 0;
}



If you can keep you head when all about you
Are losing theirs and blaming it on you;
If you can dream - and not make dreams your master;
If you can think - and not make thoughts you aim;
Yours is the Earth and everything that's in it.

Rudyard Kipling

QuestionWhere to find DISK DEFRAGMENTER source code? Pin
Bartosz Bien18-Jun-03 9:09
Bartosz Bien18-Jun-03 9:09 
AnswerRe: Where to find DISK DEFRAGMENTER source code? Pin
John M. Drescher18-Jun-03 9:27
John M. Drescher18-Jun-03 9:27 
AnswerRe: Where to find DISK DEFRAGMENTER source code? Pin
John M. Drescher18-Jun-03 16:47
John M. Drescher18-Jun-03 16:47 
GeneralRe: Where to find DISK DEFRAGMENTER source code? Pin
Bartosz Bien18-Jun-03 22:35
Bartosz Bien18-Jun-03 22:35 
GeneralRe: Where to find DISK DEFRAGMENTER source code? Pin
John M. Drescher19-Jun-03 0:14
John M. Drescher19-Jun-03 0:14 
QuestionHow to clear CFrameWnd Pin
Shay Harel18-Jun-03 8:21
Shay Harel18-Jun-03 8:21 
AnswerRe: How to clear CFrameWnd Pin
Maximilien18-Jun-03 8:49
Maximilien18-Jun-03 8:49 
GeneralRe: How to clear CFrameWnd Pin
Shay Harel18-Jun-03 9:12
Shay Harel18-Jun-03 9:12 
QuestionPossible reason why a fileread fails? Pin
georgiek5018-Jun-03 8:10
georgiek5018-Jun-03 8:10 
AnswerRe: Possible reason why a fileread fails? Pin
AlexO18-Jun-03 8:18
AlexO18-Jun-03 8:18 
GeneralRe: Possible reason why a fileread fails? Pin
georgiek5018-Jun-03 14:17
georgiek5018-Jun-03 14:17 
GeneralQuestion about Kodak CImgEdit Control Pin
MarcoNedwig18-Jun-03 7:08
MarcoNedwig18-Jun-03 7:08 
GeneralGetting Started in Visual C++.NET Pin
Nino_118-Jun-03 6:11
Nino_118-Jun-03 6:11 
GeneralRe: Getting Started in Visual C++.NET Pin
valikac18-Jun-03 6:47
valikac18-Jun-03 6:47 
GeneralRe: Getting Started in Visual C++.NET Pin
Nino_118-Jun-03 15:48
Nino_118-Jun-03 15:48 
GeneralRe: Getting Started in Visual C++.NET Pin
valikac19-Jun-03 6:41
valikac19-Jun-03 6:41 
GeneralRe: Getting Started in Visual C++.NET Pin
Nino_122-Jun-03 11:11
Nino_122-Jun-03 11:11 

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.