I'm not sure about interleaving but you can use for_each
to deinterleave. You would still want to add additional error checking (that the source size is correct etc):
#include <iostream>
#include <vector>
typedef std::vector<double> dVector;
typedef dVector::iterator dVectorIT;
class DeInterleave
{
public:
DeInterleave(std::vector<dVector>& output) : output_(output), currentIndex_(0) { }
void operator()(const dVector::value_type& item)
{
output_[currentIndex_].push_back(item);
currentIndex_ = (currentIndex_ + 1) % output_.size();
}
private:
std::vector<dVector>& output_;
int currentIndex_;
};
int main()
{
dVector source;
source.push_back(5.0);
source.push_back(6.0);
source.push_back(8.0);
source.push_back(2.0);
source.push_back(2.0);
source.push_back(1.0);
std::vector<dVector> dest(3);
std::for_each(source.begin(), source.end(), DeInterleave(dest));
std::cout << dest[0].size() << " " << dest[0][0] << ":" << dest[0][1] << std::endl;
std::cout << dest[1].size() << " " << dest[1][0] << ":" << dest[1][1] << std::endl;
std::cout << dest[2].size() << " " << dest[2][0] << ":" << dest[2][1] << std::endl;
return 0;
}
Mark B
2010-08-31 16:23:05