Click here to Skip to main content
15,891,529 members
Home / Discussions / C / C++ / MFC
   

C / C++ / MFC

 
AnswerRe: operator ":" new for me in C++ PinPopular
Aescleal16-Jan-11 9:01
Aescleal16-Jan-11 9:01 
AnswerRe: operator ":" new for me in C++ Pin
moemass17-Jan-11 3:12
moemass17-Jan-11 3:12 
QuestionHow to get computer color setting? Pin
includeh1015-Jan-11 20:37
includeh1015-Jan-11 20:37 
AnswerRe: How to get computer color setting? Pin
Andrew Brock15-Jan-11 20:53
Andrew Brock15-Jan-11 20:53 
GeneralRe: How to get computer color setting? Pin
includeh1016-Jan-11 2:47
includeh1016-Jan-11 2:47 
GeneralRe: How to get computer color setting? Pin
Andrew Brock16-Jan-11 2:51
Andrew Brock16-Jan-11 2:51 
QuestionSorting data using std::map or std::multimap Pin
cmacgowan15-Jan-11 14:19
cmacgowan15-Jan-11 14:19 
AnswerRe: Sorting data using std::map or std::multimap Pin
Andrew Brock15-Jan-11 18:08
Andrew Brock15-Jan-11 18:08 
Maps are sorted by the key, so you cant have them stored in the static order for 1.
Depending on how often you are adding to the list and to where i would recomend either a std::vector or std::list for storing the data in the static order

Your question is a little unclear, but if i'm reading it correctly you need something like this.

cmacgowan wrote:
4. Followed by Sphere decending (-0.5, 0.0, 1.0 ...)
The order there is ascending

#include <stdio.h>
#include <string>
#include <vector>
#include <map>

//save putting std:: infront of everything
using std::string;
using std::map;
using std::vector;

//This struct stores the data that is required for sorting
class ProductTypeKey {
	public:
		ProductTypeKey() { }
		ProductTypeKey(unsigned _nPackageSize, float _nBaseCurve, float _nSphere)
			: nPackageSize(_nPackageSize), nBaseCurve(_nBaseCurve), nSphere(_nSphere) { }

	public:
		unsigned nPackageSize;
		float nBaseCurve;
		float nSphere;
};

//"The data is contained in a struct with other data.". This struct stores "[the] other data"
typedef struct {
	//the other data you spoke of
} ProductTypeData;

struct MyItemCompare {
	bool operator()(const ProductTypeKey &mkLeft, const ProductTypeKey &mkRight) const {
		//Compare the package sizes, if they are not equal, then we are sorting by this key
		if (mkLeft.nPackageSize != mkRight.nPackageSize) {
			//greater than for decending order
			return mkLeft.nPackageSize > mkRight.nPackageSize;
		}
		//Compare the base curves, if they are not equal, then we are sorting by this key
		if (mkLeft.nBaseCurve != mkRight.nBaseCurve) {
			//greater than for decending order
			return mkLeft.nBaseCurve > mkRight.nBaseCurve;
		}
		//Compare the sphere, if they are not equal, then we are sorting by this key
		if (mkLeft.nSphere != mkRight.nSphere) {
			//less than for ascending order
			return mkLeft.nSphere < mkRight.nSphere;
		}
		//We will only make it here if the 2 products are identical from the data we are sorting on.
		return false;
	}
};

//This struct contains the information that is stored in static order
class Product {
	public:
		typedef map<ProductTypeKey, ProductTypeData, MyItemCompare> TypesMap;

	public:
		Product() { }
		Product(const string &_strCompany, const string &_strProduct)
			: strCompany(_strCompany), strProduct(_strProduct) { }
		void AddType(const ProductTypeKey &key, const ProductTypeData &data){
			colTypes.insert(TypesMap::value_type(key, data));
		}

	public:
		string strCompany;
		string strProduct;
		TypesMap colTypes; //this stores the data for this product that in sorted order
};

int main(int argc, char *argv[]) {
	Product prodCibaFocus("Ciba", "Focus");
	//Adding types before insert
	prodCibaFocus.AddType(ProductTypeKey(10, 8.6f, +0.00f), ProductTypeData());
	prodCibaFocus.AddType(ProductTypeKey(20, 8.6f, -0.25f), ProductTypeData());
	prodCibaFocus.AddType(ProductTypeKey(20, 8.6f, -0.10f), ProductTypeData());
	prodCibaFocus.AddType(ProductTypeKey(20, 8.6f, +0.25f), ProductTypeData());
	Product prodCibaAir("Ciba", "Air");
	vector<Product> colProducts;
	colProducts.push_back(prodCibaFocus);
	colProducts.push_back(prodCibaAir);
	//Adding types after insert. prodCibaAir is the 2nd item in the list (index 1)
	colProducts[1].AddType(ProductTypeKey(10, 8.6f, -0.25f), ProductTypeData());
	colProducts[1].AddType(ProductTypeKey(10, 8.6f, -0.10f), ProductTypeData());
	colProducts[1].AddType(ProductTypeKey(20, 8.1f, -0.25f), ProductTypeData());
	colProducts[1].AddType(ProductTypeKey(20, 8.1f, -0.10f), ProductTypeData());
	colProducts[1].AddType(ProductTypeKey(20, 8.1f, +0.00f), ProductTypeData());
	colProducts[1].AddType(ProductTypeKey(20, 8.6f, +0.25f), ProductTypeData());
	colProducts[1].AddType(ProductTypeKey(20, 8.6f, +0.40f), ProductTypeData());
	//now print them
	for (vector<Product>::const_iterator iProduct = colProducts.begin(); iProduct != colProducts.end(); ++iProduct) {
		for (Product::TypesMap::const_iterator iType = iProduct->colTypes.begin(); iType != iProduct->colTypes.end(); ++iType) {
			printf("%s %s %u %.02f %.02f\n", iProduct->strCompany.c_str(), iProduct->strProduct.c_str(),
				iType->first.nPackageSize, iType->first.nBaseCurve, iType->first.nSphere);
		}
	}
	return 0;
}

GeneralRe: Sorting data using std::map or std::multimap Pin
cmacgowan16-Jan-11 11:01
cmacgowan16-Jan-11 11:01 
AnswerRe: Sorting data using std::map or std::multimap Pin
Aescleal16-Jan-11 6:10
Aescleal16-Jan-11 6:10 
GeneralRe: Sorting data using std::map or std::multimap Pin
cmacgowan16-Jan-11 11:02
cmacgowan16-Jan-11 11:02 
QuestionSoftware Keyboard Simulator Pin
RedDragon2k15-Jan-11 4:48
RedDragon2k15-Jan-11 4:48 
AnswerRe: Software Keyboard Simulator Pin
jschell15-Jan-11 10:02
jschell15-Jan-11 10:02 
AnswerRe: Software Keyboard Simulator Pin
Andrew Brock15-Jan-11 17:16
Andrew Brock15-Jan-11 17:16 
AnswerRe: Software Keyboard Simulator Pin
loyal ginger15-Jan-11 17:25
loyal ginger15-Jan-11 17:25 
GeneralRe: Software Keyboard Simulator Pin
RedDragon2k16-Jan-11 4:11
RedDragon2k16-Jan-11 4:11 
QuestionHow to Set Position where a Window will be minimised Pin
SoftwareDeveloperGoa15-Jan-11 4:42
SoftwareDeveloperGoa15-Jan-11 4:42 
AnswerRe: How to Set Position where a Window will be minimised Pin
Emilio Garavaglia15-Jan-11 7:09
Emilio Garavaglia15-Jan-11 7:09 
GeneralRe: How to Set Position where a Window will be minimised Pin
SoftwareDeveloperGoa15-Jan-11 7:17
SoftwareDeveloperGoa15-Jan-11 7:17 
GeneralRe: How to Set Position where a Window will be minimised Pin
Emilio Garavaglia15-Jan-11 8:52
Emilio Garavaglia15-Jan-11 8:52 
GeneralRe: How to Set Position where a Window will be minimised Pin
SoftwareDeveloperGoa15-Jan-11 9:01
SoftwareDeveloperGoa15-Jan-11 9:01 
QuestionWhat wrong with this loop? Pin
Le@rner14-Jan-11 19:34
Le@rner14-Jan-11 19:34 
AnswerRe: What wrong with this loop? Pin
Andrew Brock14-Jan-11 21:10
Andrew Brock14-Jan-11 21:10 
AnswerRe: What wrong with this loop? Pin
jixuduxing14-Jan-11 22:37
jixuduxing14-Jan-11 22:37 
GeneralRe: What wrong with this loop? Pin
Andrew Brock14-Jan-11 23:39
Andrew Brock14-Jan-11 23:39 

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.