Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. QHash<QString, QVector<float> > iteration
Forum Updated to NodeBB v4.3 + New Features

QHash<QString, QVector<float> > iteration

Scheduled Pinned Locked Moved General and Desktop
3 Posts 2 Posters 2.5k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • M Offline
    M Offline
    Mike_newbye
    wrote on last edited by
    #1

    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.

    Kind regards,
    Mikael

    1 Reply Last reply
    0
    • ? Offline
      ? Offline
      A Former User
      wrote on last edited by
      #2

      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
      @

      1 Reply Last reply
      0
      • ? Offline
        ? Offline
        A Former User
        wrote on last edited by
        #3

        Mike_newbye:

        Did you ever get your code to work?

        1 Reply Last reply
        0

        • Login

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • Users
        • Groups
        • Search
        • Get Qt Extensions
        • Unsolved