QHash<QString, QVector<float> > iteration



  • Hi,

    I have a
    @QHash<QString, QVector<float> >@
    and i want to loop it changing the values in the vectors like, skipping some header, like following:

    @
    string a = "Cluster";
    const char b = a.c_str();
    float new_value;
    float old_value;
    float min = getMinHash(qhash);
    float max = getMaxHash(qhash);
    QHashIterator<QString, QVector<float> > i(qhash);
    while (i.hasNext())
    {
    i.next();
    if(i.key().operator !=(b))
    {
    for(int j = 0; j<i.value().size(); j++)
    {
    old_value = i.value().at(j);
    new_value = (old_value - min)/(max-min)
    (0.99-0.01) + 0.01;
    i.value().replace(j, new_value);
    }
    qDebug() << i.value();
    }
    }
    @

    And I have the following error:
    @C:\Qt\latest test\Prototype\Coordinate.cpp:264: error: passing 'const QVector<float>' as 'this' argument of 'void QVector<T>::replace(int, const T&) [with T = float]' discards qualifiers [-fpermissive]@

    on this: @i.value().replace(j, new_value);@

    Could you please help me fixing this?

    Thanks in advance.



  • According to the documentation, QHashIterator is defined as a const iterator, so the value associated with a given instance would also be const. If you're wanting to loop over all the items in a QHash, I'd suggest using the begin() and end() iterator methods found in the class.

    Sample code:
    @
    typedef QHash<QString, QVector<float> > MyHash;
    typedef MyHash::iterator MyIterator;

    MyHash qhash;

    for( int i = 0; i < 3; i++ )
    {
    QVector<float> fvector;
    QString key( "key" );

    if( i > 0 )
    {
        fvector.fill( i, i + 1 );
    }
    
    key += QString::number( i );
    qhash.insert( key, fvector );
    

    }

    for( MyIterator it = qhash.begin(); it != qhash.end(); it++ )
    {
    // print key
    qDebug() << "key:" << it.key();

    // get value
    QVector<float>& fvector = it.value();
    
    // print previous size
    qDebug() << "  prev size:" << fvector.size();
    
    // modify QVector contents by removing first item
    if( !fvector.isEmpty() )
    {
        fvector.removeFirst();
    }
    
    // print new size
    qDebug() << "  new size:" << fvector.size();
    

    }
    @

    Sample output:

    @
    key: "key0"
    prev size: 0
    new size: 0
    key: "key1"
    prev size: 2
    new size: 1
    key: "key2"
    prev size: 3
    new size: 2
    @



  • Mike_newbye:

    Did you ever get your code to work?


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.