Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

write qvector in csv file



  • Hey qt people,

    I have a question to write csv file,

    I have 3 QVector<double> a,b,c
    a = (1,2,3,4)
    b = (0,0,0)
    c = (10.2)

    i want write a file csv like this:
    1;0;10.2
    2;0;
    3;0;
    4;;

    but the size is different, with the same size is simple :

    int cpt=0;
    while(a.size!=cpt)
    {
          flux<<a<<";"<<b<<";"<<c<<endl
    }
    

    but with different size how to do ?


  • Lifetime Qt Champion

    @Albator said in write qvector in csv file:

    but with different size how to do ?

    use for-loop, take the max length from these 3 vectors and inside the loop check whether the current index is valid for each vector.



  • const QVector<double>* vects[] = {&a,&b,&c};
    
    for(int i=0, maxI = std::max_element(sid::begin(vects), std::end(vects ),[](const QVector<double>* left, const QVector<double>* right)->bool{return left->size()<right->size();})->size();i<maxI;++i){
        for(auto&& vec : vects){
            if(i < vec->size())
                flux<<vec->at(i)
            flux <<";"
        }
        flux<<endl;
    }
    


  • It works perfectly!
    thanks for the help for the third or fourth time ahah!


Log in to reply