# vector of vtkDataArray

**URL:** https://discourse.vtk.org/t/vector-of-vtkdataarray/3841
**Category:** Support
**Created:** [July 23, 2020, 2:57pm UTC](https://discourse.vtk.org/t/vector-of-vtkdataarray/3841 "2020-07-23T14:57:35Z")
**Posts on this page:** 6
**Page:** 1

<div class="post-metadata">

### Author: ![kayarre](https://discourse.vtk.org/user_avatar/discourse.vtk.org/kayarre/32/808_2.png) [@kayarre](https://discourse.vtk.org/u/kayarre)
#### Post date: [July 23, 2020, 2:57pm UTC](https://discourse.vtk.org/t/vector-of-vtkdataarray/3841/1 "2020-07-23T14:57:36Z")

</div>

I want to make a vector of `vtkDataArray` where each array may be a different type.

I know I can use something like ` std::vector<vtkDataArray *> data_arrays;`  
and I can do something like

```auto
vtkSmartPointer<vtkDoubleArray> test = vtkSmartPointer<vtkDoubleArray> ::New();
 // add values to test

data_arrays.push_back(test)

```

Is there a smarter way to do this? will the smart pointer be manage properly or will it need to be deleted later?

---

<div class="post-metadata">

### Author: ![danlipsa](https://discourse.vtk.org/user_avatar/discourse.vtk.org/danlipsa/32/390_2.png) [@danlipsa](https://discourse.vtk.org/u/danlipsa)
#### Post date: [July 23, 2020, 3:11pm UTC](https://discourse.vtk.org/t/vector-of-vtkdataarray/3841/2 "2020-07-23T15:11:06Z")

</div>

Kurt,  
You should rather use std::vector\<vtkSmartPointer\>\> v;  
and then set your vector:  
v.resize(size)  
forall i:  
v[i] = vtkSmartPointer ::New();  
or you can use  
push\_back as you showed.

This way the pointers will be deallocated when the vector goes out of scope.

Dan

---

<div class="post-metadata">

### Author: ![kayarre](https://discourse.vtk.org/user_avatar/discourse.vtk.org/kayarre/32/808_2.png) [@kayarre](https://discourse.vtk.org/u/kayarre)
#### Post date: [July 23, 2020, 5:16pm UTC](https://discourse.vtk.org/t/vector-of-vtkdataarray/3841/3 "2020-07-23T17:16:03Z")

</div>

Thank you. I think that I am having a hard time with is how to do it generically?

I want to return a specific type of vtkDataArray as a function of some input type

e.g.

```auto
std::vector<vtkSmartPointer> v;
std::vector<std::string> vars = {"float", "double", "int"};

for (int i = 0; i < vars.size(); ++i) {
  if (vars.at(i) == "float") {
    v.push_back(vtkSmartPointer<vtkFloatArray> ::New());
  }
}

```

---

<div class="post-metadata">

### Author: ![danlipsa](https://discourse.vtk.org/user_avatar/discourse.vtk.org/danlipsa/32/390_2.png) [@danlipsa](https://discourse.vtk.org/u/danlipsa)
#### Post date: [July 23, 2020, 5:27pm UTC](https://discourse.vtk.org/t/vector-of-vtkdataarray/3841/4 "2020-07-23T17:27:31Z")

</div>

I see. In that case I don’t have a better solution than just adding the pointers to vktDataArray. You have to make sure the vtkSmartPointer\<…\> don’t go out of scope before your vector,  
otherwise your pointers stored in vector elements will become invalid.

---

<div class="post-metadata">

### Author: ![kayarre](https://discourse.vtk.org/user_avatar/discourse.vtk.org/kayarre/32/808_2.png) [@kayarre](https://discourse.vtk.org/u/kayarre)
#### Post date: [July 27, 2020, 6:57pm UTC](https://discourse.vtk.org/t/vector-of-vtkdataarray/3841/5 "2020-07-27T18:57:27Z")

</div>

I figured out a solution that worked for me.

I can get the typed array via `vtkAOSDataArrayTemplate`

```auto
vtkDataArray * gettypedarray(FieldDataType_e VarType)
{
  switch(VarType)
  {
    case TecplotFloat: return vtkAOSDataArrayTemplate<float>::New();
    case TecplotDouble: return vtkAOSDataArrayTemplate<double>::New();
    case TecplotLongInt: return vtkAOSDataArrayTemplate<long>::New();
    case TecplotShortInt: return vtkAOSDataArrayTemplate<short>::New();
    case TecplotByte: return vtkAOSDataArrayTemplate<char>::New();
    case TecplotBit: return vtkAOSDataArrayTemplate<char>::New();
    default:
      assert(!"Not a valid VarType!");
  }
}

```

```auto
std::vector<vtkDataArray *> pt_data_arrays;
vtkDataArray * data;
for (CZ = 0; CZ < NumZones; CZ++) {
  data = gettypedarray(VarType[CV]);
  data->SetNumberOfTuples(NumPts);
  data->SetNumberOfComponents(1);
  data->SetName(var_names.at(CV).data());
  for (I = 0; I < NumPts; ++I) {
    data->SetComponent(I, 0, VDataBase[CZ * NumVars + CV][I]);
  }
  pt_data_arrays.push_back(data);
}

```

And after I write the file with the data it gets deleted.

```auto
for (int k = 0; k < pt_data_arrays.size(); ++k )
{
  pt_data_arrays.at(k)->Delete();
}

```

---

<div class="post-metadata">

### Author: ![AllisonVacanti](https://discourse.vtk.org/user_avatar/discourse.vtk.org/allisonvacanti/32/2396_2.png) [@AllisonVacanti](https://discourse.vtk.org/u/AllisonVacanti)
#### Post date: [August 6, 2020, 5:41pm UTC](https://discourse.vtk.org/t/vector-of-vtkdataarray/3841/6 "2020-08-06T17:41:12Z")

</div>

The following should work with `vtkSmartPointer`:

```auto
std::vector<vtkSmartPointer<vtkDataArray>> vec;

auto arr = vtkSmartPointer<vtkFloatArray>::New();
// or 
vtkNew<vtkFloatArray> arr;

/* do stuff with arr */

vec.push_back(arr);
// or vec.push_back(std::move(arr)); if you're done with arr and want
// to avoid touching the reference count unnecessarily. 

```

It’s better to use smart pointers in the vector so that you don’t have to worry about the lifetimes of raw `vtkDataArray*` pointers. The smart pointers have optimized move operations so you won’t have to worry about expensive ref count updates when the vector dynamically resizes.
