I am looking for an elegant way to pivot a vector of vector prefarably using STL algorithms or boost Sample data looks like this
vector<vector<int> > vm;
vector<int> v;
v.push_back(1);
v.push_back(2);
vm.push_back(v);
v.clear();
v.push_back(3);
v.push_back(4);
vm.push_back(v);
v.clear();
v.push_back(5);
v.push_back(6);
vm.push_back(v);
1 2
3 4
5 6
I want to get a vector of vectors of ints like this
1 3 5
2 4 6
I would introduce a wrapper that converts row -> col and col -> row. This will prevent you having to copy all the data.
#include <iostream>
#include <vector>
template<typename T>
class TwoDPivotWrapper
{
public:
// These two typedef's were done with std::vector
// in mind. But with a small amount of effort I am
// sure they can be generalized. They are solely to define
// value_type (the data stored in the 2-D array).
typedef typename T::value_type OneDType;
typedef typename OneDType::value_type value_type;
// A constructor that wraps a 2-D structure.
TwoDPivotWrapper(T& o)
: object(o)
{}
// A helper class used to store the row after the first array accesses.
class Row
{
friend class TwoDPivotWrapper;
Row(TwoDPivotWrapper& w, size_t r)
: wrapper(w)
, row(r)
{}
TwoDPivotWrapper& wrapper;
size_t row;
public:
value_type operator[](size_t col)
{
return wrapper.get(row,col);
}
};
// The operator [] returns a Row object that overloads
// the operator[] for the next dimension.
Row operator[](size_t row) {return Row(*this, row);}
// Generic get function used to access elements.
// Notice we swap the row/col around when accessing
// the underlying object.
value_type get(size_t row, size_t col) {return object[col][row];}
private:
T& object;
};
Typical usage would be:
int main()
{
typedef std::vector<std::vector<int> > TwoDVector;
TwoDVector data(3,std::vector<int>(2,0));
data[0][0] = 1; data[0][1] = 2;
data[1][0] = 3; data[1][1] = 4;
data[2][0] = 5; data[2][1] = 6;
TwoDPivotWrapper<TwoDVector> wrapper(data);
std::cout << wrapper[0][0] << wrapper[0][1] << wrapper[0][2] << "\n";
std::cout << wrapper[1][0] << wrapper[1][1] << wrapper[1][2] << "\n";
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With