crossplatform.ru

Здравствуйте, гость ( Вход | Регистрация )

> Непонятки с QMap
trdm
  опции профиля:
сообщение 8.10.2009, 18:20
Сообщение #1


Дмитрий Трошин
****

Группа: Участник
Сообщений: 575
Регистрация: 12.1.2008
Пользователь №: 68

Спасибо сказали: 21 раз(а)




Репутация:   6  


Непонятки с QMap....
struct uoCell
{
    uoCell():m_nomber(0){};
    int m_nomber;
};

int main(int argc, char *argv[])
{
    QApplication app(argc, argv);
    QDialog dlg;
    QMap<int,uoCell*> map;
    for (int i = 1; i<10; i++)
    {
        if (!map.contains(i)){
            uoCell* cell = new uoCell;
            cell->m_nomber = i;
            cout << "insert" << i;
            map.insert(i,cell);
        }
    }
    uoCell* cell2 = NULL;

    QMap<int,uoCell*>::iterator it = map.find(5);

    while(it != map.begin()){
        cell2 = it.value();
        cell2->m_nomber += 0;
        cout << "find" << cell2->m_nomber;
        --it;
    }


    dlg.show();


    return app.exec();
}


Казалось бы
while(it != map.begin()){
гарантирует, что найдется пара с m_nomber == 1
Ан нет, нефига....
Перейти в начало страницы
 
Быстрая цитата+Цитировать сообщение
 
Начать новую тему
Ответов
trdm
  опции профиля:
сообщение 8.10.2009, 19:22
Сообщение #2


Дмитрий Трошин
****

Группа: Участник
Сообщений: 575
Регистрация: 12.1.2008
Пользователь №: 68

Спасибо сказали: 21 раз(а)




Репутация:   6  


Думаю правильно, однако я привык, что с бегина по энд и с энда по бегин выведется все.
А тут нужны какие-то извраты.
Путем "тыка" нашел нужные мне "нормальные" проходные алгоритмы:
Раскрывающийся текст

    QMap<int,uoCell*> map;
    for (int i = 1; i<=10; i++)
    {
        if (!map.contains(i)){
            uoCell* cell = new uoCell;
            cell->m_nomber = i;
            cout << "\n insert: \t" << i;
            map.insert(i,cell);
        }
    }
    uoCell* cell2 = NULL;

    QMap<int,uoCell*>::iterator it;
    it = map.find(5);

    cout << "\n\n test1 5->1 \n";
    while(it != map.begin()){
        cell2 = it.value();
        cell2->m_nomber += 0;
        cout << "\n find \t" << cell2->m_nomber << " key: " << it.key();
        --it;
    }

    cout << "\n\n test2 1->10 \n";
    it = map.begin();
    while(it != map.end()){
        cell2 = it.value();
        cell2->m_nomber += 0;
        cout << "\n find \t" << cell2->m_nomber << " key: " << it.key();
        ++it;
    }

    cout << "\n\n test3 10-1 \n";

    it = map.end();
    do {
        --it;
        cell2 = it.value();
        cell2->m_nomber += 0;
        cout << "\n find \t" << cell2->m_nomber << " key: " << it.key();

    }while(it != map.begin());


    cout << "\n\n test4 5->10 \n";

    it = map.find(5);
    while(it != map.end()){
        cell2 = it.value();
        cell2->m_nomber += 0;
        cout << "\n find \t" << cell2->m_nomber << " key: " << it.key();
        ++it;
    }

    cout << "\n\n test5 5->1 \n";

    it = map.find(5);
    bool last = false;
    do {
        if (last)
            --it;
        last = true;
        cell2 = it.value();
        cell2->m_nomber += 0;
        cout << "\n find \t" << cell2->m_nomber << " key: " << it.key();

    }while(it != map.begin());



Вывод:
Раскрывающийся текст


insert: 1
insert: 2
insert: 3
insert: 4
insert: 5
insert: 6
insert: 7
insert: 8
insert: 9
insert: 10

test1 5->1

find 5 key: 5
find 4 key: 4
find 3 key: 3
find 2 key: 2

test2 1->10

find 1 key: 1
find 2 key: 2
find 3 key: 3
find 4 key: 4
find 5 key: 5
find 6 key: 6
find 7 key: 7
find 8 key: 8
find 9 key: 9
find 10 key: 10

test3 10-1

find 10 key: 10
find 9 key: 9
find 8 key: 8
find 7 key: 7
find 6 key: 6
find 5 key: 5
find 4 key: 4
find 3 key: 3
find 2 key: 2
find 1 key: 1

test4 5->10

find 5 key: 5
find 6 key: 6
find 7 key: 7
find 8 key: 8
find 9 key: 9
find 10 key: 10

test5 5->1

find 5 key: 5
find 4 key: 4
find 3 key: 3
find 2 key: 2
find 1 key: 1
Process returned 0 (0x0) execution time : 1.890 s
Press any key to continue.

Перейти в начало страницы
 
Быстрая цитата+Цитировать сообщение

Сообщений в этой теме


Быстрый ответОтветить в данную темуНачать новую тему
Теги
Нет тегов для показа


1 чел. читают эту тему (гостей: 1, скрытых пользователей: 0)
Пользователей: 0




RSS Текстовая версия Сейчас: 28.4.2024, 17:55