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 501 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.
  • AlbatorA Offline
    AlbatorA Offline
    Albator
    wrote on 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 ?

    jsulmJ 1 Reply Last reply
    0
    • AlbatorA Albator

      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 ?

      jsulmJ Offline
      jsulmJ Offline
      jsulm
      Lifetime Qt Champion
      wrote on 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
      • VRoninV Offline
        VRoninV Offline
        VRonin
        wrote on 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
        • AlbatorA Offline
          AlbatorA Offline
          Albator
          wrote on last edited by
          #4

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

          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