Parsing a multidimentional vector into an array

For a rather complex and strange reason that I won't explain right now, I need to have this going on in my program.

1
2
3
4
5
6
class FVF{
private:
    vector<vector<float>> data;    //Contains fvf data for Direct3D stuff
public:
//Various get/set functions for the actual data, and structure
};


The idea is that this can contain ANY format of D3D-FVF styles (for compatibility with file handling things I've got set up).

1
2
3
4
5
6
class Model3D{
private:
    vector<FVF> vertex;    //Implies vector<vector<vector<float>>>
    vector<uint> index;
//Other stuffs
};


The FVF allows this Model3D class to also be compatible with file handling methods I've got, but here's the problem.
D3D buffers require an array to feed them the information, and I know that for a single dimension of vector I can use vec.data(), I haven't got a clue how to do this for multiple dimensions.

I think the best Idea I've got so far is to set the vector within the Model3D class as a pointer, then I can union it with a float pointer... Once I can guarantee the information is correct and complete, manually transfer the contents of the vectors into the float pointer...
(The union is to reduce memory needed instead of having the data repeated in vectors and arrays)

Any ideas how I could just pass these as arrays, or any other better ideas?
Unfortunately, you can't cast it to do what you want -- you have to either create one or change your datatype to something more useful.

See here for links:
http://www.cplusplus.com/forum/general/118325/#msg645528
I know this is nothing too special, but I had a little fun:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
// require c++11
#include <iostream>
#include <vector>
#include <iterator>
#include <algorithm>

template <typename T>
std::vector<T> flatten( const std::vector< std::vector<T> >& data )
{
  std::vector<T> ret;

  for ( auto &v : data )
  {
    const auto size = ret.size();
    ret.resize( size + v.size() );
    std::copy( v.begin(), v.end(), ret.begin() + size);
  }

  return ret;
}

int main(void)
{
  std::vector< std::vector<int> > data;
  data.push_back( { 1,2,3,4,5 } );
  data.push_back( { 6,7,8,9,0 } );

  std::vector< int > flat = flatten( data );

  std::copy( flat.begin(), flat.end(), std::ostream_iterator<int>( std::cout, " "));
  std::cout << '\n';
  return 0;
}


One thing to check out is to see if D3D defines a structure that is more suitable than a 3d vector.
Yeah I was thinking of something kinda like this, but outputting as an array to please D3D (although .data() would be fine)
And also I'd probably use the old fashioned for loop because for some reason I just don't get along with them XD

But I must say that the boost library does look wonderful... Especially since I already have the boost library installed and some parts are already in use by my engine.

Thanks for the tips.
And what would be nice is that the Boost library allows you to control storage. By default it behaves just like a regular C++ array in memory, so you don't actually have to copy or convert anything -- you can just pass the actual data to the functions.
Topic archived. No new replies allowed.