key ordering in map
when i populate a map<AnsiString *,object *>, miraculously, the map keys are sorted alphabetically by value. i find this is highly desirable.
however, if i change a property of an object in the map, the ordering is corrputed.
is there an easy way to stop the map from reordering its keys?
The map never reorders its keys. But you might be changing the keys, thus ruining the ordering.
I also find it highly suspicious that the map is ordered alphabetically in the first place. The map you've shown here would be ordered by the memory location of the string. Don't rely on this miracle!
Having the second of a map as a pointer is often not ideal, but having the first of a map as a pointer rings some major alarm bells.
Please show more code so we can see how far-reaching the problem is.
Keys in maps should be considered as const and shouldn't be changed, otherwise you'll corrupt the map. I forget the exact details, but I think Scott Meyers explains it in "Effective STL".
my mistake! it is not a pointer/pointer map!
i realize there are lots of pointers here, so before you start asking why: my compiler requires it for any object descended from their VCL framework. otherwise, you get a compile time error saying "VCL style classes must be instantiated with operator new". all of their framework uses pointers, so it's really just more efficient to keep everything under 1 layer of indirection rather than constantly addressing and dereferencing everything.
class paramEditor : public TPanel
paramEditor(TobjectEditor *Owner,parameter *param);
void __fastcall setParam(TObject *Sender);
void __fastcall resetEditColor(TObject *Sender);
void loadParams(paramSet *pset)
clearParams(); //frees elements of params
std::map<String,parameter *>::iterator it = pset->params.begin();
paramEditor *p =new paramEditor(this,it->second);
params[i]->setParam(); // passes value of text field into function that validates / casts text into appropriate data type
i'd rather not post the entire src, but here are snippets of the relevant parts.
the only reason for using a map instead of an array for paramSet is the alphabetizing.
params is a DynamicArray<paramEditor *> member of the objEditor, which is the form that houses all this.
parameters whose values are altered during setParam will not show up in the same order the next time loadParams is called.