Thread: key ordering in map

  1. #1
    3735928559
    Join Date
    Mar 2008
    Location
    RTP
    Posts
    838

    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?

  2. #2
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    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!
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  3. #3
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    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.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  4. #4
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    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".

  5. #5
    3735928559
    Join Date
    Mar 2008
    Location
    RTP
    Posts
    838
    my mistake! it is not a pointer/pointer map!

    Code:
    class paramEditor : public TPanel
    {
            public:
            paramEditor(TobjectEditor *Owner,parameter *param);
            void __fastcall setParam(TObject *Sender);
            void __fastcall resetEditColor(TObject *Sender);        
            TLabel *title;
            TLabel *currentValue;
            TEdit *edit;
            parameter *param;
    };
    
    void loadParams(paramSet *pset)
    {
            clearParams(); //frees elements of params
            std::map<String,parameter *>::iterator it = pset->params.begin();
            while(it!=pset->params.end())
            {
                    params.Length++;
                    paramEditor *p =new paramEditor(this,it->second);
                    params[params.High]=p;
                    ++it;
            }
    }
    
    void setParams()
    {
            for(int i=0;i<params.Length;i++)
            {
                            params[i]->setParam(); // passes value of text field into function that validates / casts text into appropriate data type
            }
            loadParams();
    }
    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.

    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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Polynomials and ADT's
    By Emeighty in forum C++ Programming
    Replies: 20
    Last Post: 08-19-2008, 08:32 AM
  2. Virtual keys
    By Arkanos in forum Windows Programming
    Replies: 4
    Last Post: 12-12-2005, 10:00 AM
  3. Directional Keys - Useing in Console
    By RoD in forum C++ Programming
    Replies: 38
    Last Post: 10-06-2002, 04:42 PM
  4. FAQ: Directional Keys - Useing in Console
    By RoD in forum FAQ Board
    Replies: 38
    Last Post: 10-06-2002, 04:42 PM