Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Correct way to work with vector of arrays

Could someone tell what is the correct way to work with a vector of arrays?

I declared a vector of arrays (vector<float[4]>) but got error: conversion from 'int' to non-scalar type 'float [4]' requested when trying to resize it. What is going wrong?

like image 382
Pulkit Sinha Avatar asked Jan 06 '11 06:01

Pulkit Sinha


People also ask

How do you use an array as a vector?

Each index of array stores a vector which can be traversed and accessed using iterators. Insertion: Insertion in array of vectors is done using push_back() function. Above pseudo-code inserts element 35 at every index of vector <int> A[n]. Traversal: Traversal in an array of vectors is perform using iterators.

How do you pass an array of vectors to a function?

void gridlist(std::vector<int> *grid, int rows, int cols){ ..... } int rows=4; int cols=5; std::vector<int> grid[rows][cols]; gridlist(grid,rows,cols); The only method which has worked for me to pass arrays to a function was by pointer (*) ?.

Can you have a vector of arrays in C++?

To sort the Vector of Arrays using the built-in sort() in C++ STL it needs an array template which defined in a boost libraries, to store vector of arrays. where, std::array is a container that encapsulates fixed size arrays.


1 Answers

You cannot store arrays in a vector or any other container. The type of the elements to be stored in a container (called the container's value type) must be both copy constructible and assignable. Arrays are neither.

You can, however, use an array class template, like the one provided by Boost, TR1, and C++0x:

std::vector<std::array<double, 4> > 

(You'll want to replace std::array with std::tr1::array to use the template included in C++ TR1, or boost::array to use the template from the Boost libraries. Alternatively, you can write your own; it's quite straightforward.)

like image 117
James McNellis Avatar answered Sep 22 '22 03:09

James McNellis