-1

I have an array with size of n. I want to split this array with size of y. It need to be x times newarray with size of y. I need multidimensional array like myarray[x][y]. Output need to be myarray[0][0...y],myarray[1][0...y],....myarray[x][0...y]. How can I do it in qt(by using QList<QByteArray> myarray) or other solutions? For example

    for (int i = 0; i < y; i++)
    {
        myarray[i] = array[i];
    }
Cenk
  • 1
  • 2
  • Are you aware of how to convert a plain 1D array to a 2D array? – P.W Jan 16 '19 at 06:16
  • No, how can do that in qt? Can I create an array as myarray[x][y] in qt? – Cenk Jan 16 '19 at 06:19
  • What I meant was, if you knew how to convert a plain 1D array to a 2D array, would you be able to apply it to a QList array? – P.W Jan 16 '19 at 06:22

1 Answers1

1

How can I do it in qt(by using QList myarray)

Reusing your names (myarray for a QList is not very intuitive)

#include <QList>
#include <QByteArray>
#include <iostream>

void split(const QByteArray & a, 
           QList<QByteArray> & l,
           int n)
{  
  for (int i = 0; i < a.size(); i += n)
    l.push_back(a.mid(i, n));
}

int main()
{
  QByteArray array("azertyuiop");
  QList<QByteArray> myarray;

  split(array, myarray, 3);

  for (QList<QByteArray>::const_iterator itl = myarray.constBegin();
       itl != myarray.constEnd();
       ++itl) {
    const QByteArray & a = *itl;

    for (QByteArray::const_iterator ita = a.constBegin();
       ita != a.constEnd();
       ++ita) {
      std::cout << *ita << ' ';
    }
    std::cout << std::endl;
  }

  return 0;
}

Execution result :

a z e 
r t y 
u i o 
p 
bruno
  • 32,421
  • 7
  • 25
  • 37