[Solved] QHash and QVector



  • Hi,

    I have some data read by lines from a .csv file

    @
    while(!file.atEnd())
    {
    ...
    line = file.readLine();
    list = line.split(',');
    ...
    listSize = list.size();
    @

    I also have to defined a QHash
    @
    QHash<QString, QVector<float> > hash;
    @

    Now I want to store the data read to the hash. I can fill in case if i have @int@ instead of @QVector<float>@. E.g.
    @
    for(int i = 1; i<= listSize; i++)
    {
    hash[QString::number(i)] = 1;
    }
    @

    How to do it with @QVector<float>@ if i want to keep the rows/first element of each row in QVectors.##

    Thanks in advance.



  • I solved it myself for filling lines to QVector, can anyone help to do it for rows??

    @while(!file.atEnd())
    {
    line = file.readLine();
    list = line.split(',');
    QVector<float> vec;
    for(int j = 0; j < list.size(); j++)
    {
    vec.push_back(list[j].toFloat());
    }

        hash[line] = vec;
        //qDebug() << list;
    }@

  • Lifetime Qt Champion

    Hi,

    I have some question about your logic:
    Your hash key is the string representation of the QVector content, is that really what you want ?

    What do you mean by rows ?



  • Hi,

    Thank you for answering.
    Actually I already solved it by myself as follows:

    @for(int j=0; j<listSize; j++)
    {
    hash[headerList[j]].push_back(list[j].toFloat());
    }
    @

    I need this kind of representation as I am doing clustering and I need to separately keep the data from the headers.


  • Lifetime Qt Champion

    You're welcome,

    But still... That logic seems very convoluted...

    If everything is working now, please update the thread's title to solved


Log in to reply
 

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