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. write qvector in csv file

write qvector in csv file

Scheduled Pinned Locked Moved Solved General and Desktop
flux
4 Posts 3 Posters 496 Views
  • 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.
  • A Offline
    A Offline
    Albator
    wrote on 19 May 2020, 11:57 last edited by
    #1

    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 ?

    J 1 Reply Last reply 19 May 2020, 12:00
    0
    • A Albator
      19 May 2020, 11:57

      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 ?

      J Online
      J Online
      jsulm
      Lifetime Qt Champion
      wrote on 19 May 2020, 12:00 last edited by
      #2

      @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.

      https://forum.qt.io/topic/113070/qt-code-of-conduct

      1 Reply Last reply
      4
      • V Offline
        V Offline
        VRonin
        wrote on 19 May 2020, 12:24 last edited by VRonin
        #3
        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;
        }
        

        "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
        ~Napoleon Bonaparte

        On a crusade to banish setIndexWidget() from the holy land of Qt

        1 Reply Last reply
        4
        • A Offline
          A Offline
          Albator
          wrote on 19 May 2020, 14:11 last edited by
          #4

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

          1 Reply Last reply
          0

          4/4

          19 May 2020, 14:11

          • Login

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