I have a variable that changes during operation (this is variable f). f = 0;

After I try to create names for arrays (example f = 3 means three arrays will be created with the names "Array1", "Array2", "Array3")

But I can not find the built-in function. that qt would create an array (CreateFile, CreateArray would not work)

for (int i = 1; i<f;i++) { QString st = "Array"+ i; CreateFile(st,int); CreateArray(st, int); } 

// --------------------------------------------

I tried as suggested here

 QMap <QString, QVector<int>> map; int t_ter = 1; for (int i = 0; i<f;i++) { QString st = "Array"+ i; map[st] = t_ter; t_ter= t_ter+1; qDebug() << "map[st]" << map[st]; } 

but on line

  map[st] = t_ter; 

Swears says error C2679. What have I done wrong ???

  • IMHO, in the pros it does not happen. In Qt, you can write new QVector, for example. - Vladimir Martyanov
  • I won’t guess what language you are switching to C ++ from, but there’s no such immediate possibility ... - Harry
  • For map [st], the value is type QVector <int>, so you can not tell it the value of type int - gil9red

2 answers 2

I understand that you need to just access the array by some name?

Just create an associative array, for this you can use

 QMap< QString, QVector<int> > map; 

or

 std::map< std::string, std::vector<int> > map; 

Then the name will be the key by which you can always access the array itself, for example in Qt:

 QVector<int> arr( map.Value( "Array1" ) ); 

Depending on the library used (Qt or std), the names of the functions will differ, but the essence is the same.

For example, you can add an array like this (using your variables):

 map[st] = result; 

It is more effective, of course, in this case to use pointers to arrays, so it will be possible to avoid unnecessary copying, but I don’t want to make confusion now.

EDIT: I can not leave comments because of insufficient reputation, so I will clarify the answer here.

In the comments you were told that the value type should be QVector, not int, so do, for example

 QVector<int> t_ter; t_ter.Add( 1 ); 

PS And do not play with fire: leave a space when closing nested template parameters, as I wrote QMap< QString, QVector<int> > , and not QVector<int>> , because some compilers may take this for the stream operator ">> "and then it will take a long time to rebuild all such places when the programs become a bit more.

  • >> in templates is normally allowed starting with c ++ 11. Those. in modern c ++ there should be no problems with this. - αλεχολυτ

From your question it is not quite clear what you need to do in the end.

I will try to suggest:

 // функция возвращает массив массивов int. Длина возвращаемого массива == count QVector<QVector<int> > makeArrays(int count) { QVector<QVector<int> > result; for (int i = 0; i < count; ++i) { result.push_back(QVector<int>()); // создаём новый массив и добавляем к результату } return result; }