|
-
June 8th, 2009, 11:33 PM
#1
Anyone have a better way to flatten a vector?
Hello. This is my first time posting. I have written a recursive template function to "flatten" a vector. I've just started reading up on recursive templates, and was wondering if anyone had some advice on a better way to do this (more efficient / easier to follow). When I say "flatten", I mean that I want to reduce a multidimensional vector to one-dimension, such as the following:
[[[1.0, 2.0], [3.0, 4.0]], [[5.0, 6.0], [7.0, 8.0]]] -> [1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0]
If you respond, could you please explain what your code does, because I'm essentially just a beginner at C++. Here's a working version of what I have come up with:
Code:
#include <iostream>
#include <vector>
#include <cassert>
using namespace std;
template<typename T> vector<double> flatten(const vector<T>& vec);
template<typename T> vector<T> flatten(const T& value);
template<typename T>
vector<double> flatten(const vector<T>& vec) {
assert(!vec.empty());
vector<double> flatVec;
vector<double> tempVec;
for (unsigned int i = 0; i < vec.size(); ++i) {
tempVec = flatten(vec[i]);
flatVec.insert(flatVec.end(), tempVec.begin(), tempVec.end());
}
return flatVec;
}
template<typename T>
vector<T> flatten(const T& value) {
return vector<T>(1, value);
}
int main() {
typedef vector<double> V1;
typedef vector<vector<double> > V2;
typedef vector<vector<vector<double> > > V3;
V3 vec3D(1, V2(2, V1(3, 0.0))); // Create a 3-D vector
V1 vec1D = flatten(vec3D); // Flatten the vector to 1-D
for (unsigned int i = 0; i != vec1D.size(); ++i) {
cout << vec1D[i] << endl;
}
return 0;
}
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|