Sequence order in map values...



Hi guys,

I write a simple code (see at the bottom) to ask you why the order sequence of values listed in the 'treeview' and in the "2nd Sequence" don't correspond with the "1st Sequence"...

I mean, in the "1st Sequence" I get,

.1 ==> Point -- One
.2 ==> Point -- Two
.3 ==> Point -- Three
01 ==> Zero -- One
02 ==> Zero -- Two
11 ==> One -- One

Which is the desired sequence order... but in the "2nd..." (elaborated in a Gtk Application context...)

01 * > Zero -- One
02 * > Zero -- Two
.1 * > Point -- One
11 * > One -- One
.2 * > Point -- Two
.3 * > Point -- Three


What happens, here ??

Glus

----

#include <gtkmm.h>
#include <iostream>
#include <map>

class MainWindow
:
    public Gtk::Window
{
public:

    MainWindow();
    virtual ~MainWindow();

private:

    void update_values();
    void fill_treeview();

    void on_button_quit_clicked();

    std::map<Glib::ustring, Glib::ustring> my_map2;

    // Gtkmm widgets...
    Gtk::ScrolledWindow scrwindow;
    Gtk::TreeView treeview;

    Gtk::Box vbox;
   
    Gtk::ButtonBox bbox;
    Gtk::Button button_quit;

    class ModelColumns
    :
        public Gtk::TreeModelColumnRecord
    {
    public:

        ModelColumns()
        {
            add (nid);
            add (code);
            add (vlue);
        }

        Gtk::TreeModelColumn<gint> nid;
        Gtk::TreeModelColumn<Glib::ustring> code;
        Gtk::TreeModelColumn<Glib::ustring> vlue;
    }
        model_columns;

    Glib::RefPtr<Gtk::ListStore> refListStore;

    Gtk::TreeRow row;

};

MainWindow::MainWindow()
:
    vbox (Gtk::ORIENTATION_VERTICAL),

    button_quit ("Quit"),

    refListStore (Gtk::ListStore::create (model_columns))
{
    set_title ("TreeView Test");
    set_border_width (10);
   
    treeview.set_enable_search (false);
    treeview.set_model (refListStore);

    treeview.append_column ("Nº Id", model_columns.nid);
    treeview.append_column ("Code", model_columns.code);
    treeview.append_column ("Value", model_columns.vlue);

    if (Gtk::CellRenderer * c_nid = treeview.get_column_cell_renderer (0))
        c_nid->set_alignment (0.5, 0.0);

    if (Gtk::CellRenderer * c_code = treeview.get_column_cell_renderer (1))
        c_code->set_alignment (0.5, 0.0);

    scrwindow.set_policy (Gtk::POLICY_NEVER, Gtk::POLICY_NEVER);
    scrwindow.add (treeview);

    bbox.set_margin_top (10);
    bbox.pack_start (button_quit, Gtk::PACK_SHRINK);
    bbox.set_layout (Gtk::BUTTONBOX_END);

    button_quit.signal_clicked().connect (
        sigc::mem_fun (*this, &MainWindow::on_button_quit_clicked));

    vbox.pack_start (scrwindow);
    vbox.pack_start (bbox, Gtk::PACK_SHRINK);

    add (vbox);

    fill_treeview();

    show_all_children();
}

void
MainWindow::update_values()
{
    my_map2.insert (std::pair<Glib::ustring, Glib::ustring> ("02", "Zero -- Two"));
    my_map2.insert (std::pair<Glib::ustring, Glib::ustring> (".2", "Point -- Two"));
    my_map2.insert (std::pair<Glib::ustring, Glib::ustring> ("11", "One -- One"));
    my_map2.insert (std::pair<Glib::ustring, Glib::ustring> ("01", "Zero -- One"));
    my_map2.insert (std::pair<Glib::ustring, Glib::ustring> (".1", "Point -- One"));
    my_map2.insert (std::pair<Glib::ustring, Glib::ustring> (".3", "Point -- Three"));
}

void
MainWindow::fill_treeview()
{
    if (!refListStore->children().empty())
        refListStore->clear();

    update_values();

    std::cout << "2nd Sequence:" << std::endl;

    // C++11 for...
    for (auto &it : my_map2)
    {
        std::cout << it.first << " * > " << it.second << std::endl;
 
        row = *(refListStore->append());

        row[model_columns.nid] = refListStore->children().size();
        row[model_columns.code] = it.first;
        row[model_columns.vlue] = it.second;
    }
}


void
MainWindow::on_button_quit_clicked()
{
    hide();
}

MainWindow::~MainWindow()
{
}

int main()
{
    std::map<Glib::ustring, Glib::ustring> my_map1;

    my_map1.insert (std::pair<Glib::ustring, Glib::ustring> ("02", "Zero -- Two"));
    my_map1.insert (std::pair<Glib::ustring, Glib::ustring> (".2", "Point -- Two"));
    my_map1.insert (std::pair<Glib::ustring, Glib::ustring> ("11", "One -- One"));
    my_map1.insert (std::pair<Glib::ustring, Glib::ustring> ("01", "Zero -- One"));
    my_map1.insert (std::pair<Glib::ustring, Glib::ustring> (".1", "Point -- One"));
    my_map1.insert (std::pair<Glib::ustring, Glib::ustring> (".3", "Point -- Three"));

    std::cout << "1st Sequence:" << std::endl;

    // C++11 for...
    for (auto &it : my_map1)
        std::cout << it.first << " ==> " << it.second << std::endl;

    std::cout << std::endl;


    Glib::RefPtr<Gtk::Application> app = Gtk::Application::create();

    MainWindow main_window;

    return app->run (main_window);
}

----


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]