Re: Sequence order in map values...



Thanks Mr. Kjell,

I've just finished to implement another option... so good for me...

---
class CompareGUs
{
public:

    bool operator()(const Glib::ustring &lhs, const Glib::ustring &rhs)
    {
        bool retVal = true;

        gint comp_size = std::min<gint> (lhs.size(), rhs.size());

        bool next = true;

        for (gint it = 0; (it < comp_size) && next; it++)
        {
            switch (lhs.substr (it, 1).compare (rhs.substr (it, 1)))
            {
                case (-1):

                    next = false;
                    break;

                case (0):

                    break;

                case (1):

                    retVal = false;
                    next = false;
                    break;
            }
        }

        return retVal;

    }
};

std::map<Glib::ustring, Glib::ustring, CompareGUs> my_map;
---

Glus


2015-04-09 16:38 GMT+02:00 Kjell Ahlstedt <kjell ahlstedt bredband net>:
Gtk::Application::create() calls gtk_init(), which calls setlocale(LC_ALL, ""). The call to setlocale() affects the sort order of Glib::ustring. Such strings are compared by calls to g_utf8_collate(). I don't understand why a period (.) is ignored when strings are sorted according to the rules of the user-specified locale (which in my case is sv_SE.UTF-8).

You can avoid the strange sort order by using a std::map<std::string, Glib::ustring> instead of a std::map<Glib::ustring, Glib::ustring>, provided all your map's keys contain only ascii characters.

Kjell

Den 2015-04-09 12:56, Glus Xof skrev:

2015-04-08 21:33 GMT+02:00 Glus Xof <gtglus gmail com>:
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 ??

Can't yet understand why the sign pointer (.) seems not to be significant at Glib::ustring key comparisons when std::map values are extracted when iterate...

How can manage it ?
 
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]