example of producing,
std::list<someclass*> products[4]; // styleproducts, itemproducts, ... defined before. products[1] = styleproducts; products[2] = itemproducts; std::list<someclass*> allproducts; // how products ?
description :
the code here simple can use iterators merge both 1, 2 indexes 1 list, problem here, 1000 products lists, how can all!
problem :
i want merge indexes of array merged 1 list.
try this:
std::vector< std::list<someclass*>* > products; products.push_back(&styleproducts); products.push_back(&itemproducts); // etc... std::list<someclass*> allproducts; (std::vector< std::list<someclass*>* >::iterator iter = products.begin(); iter != products.end(); ++iter) { allproducts.merge(*iter); }
alternatively:
#include <algorithm> struct mergewith { std::list<someclass*> &m_dest; mergewith(std::list<someclass*> &dest) : m_dest(dest) {} void operator()(std::list<someclass*> *l) { m_dest.merge(*l); } }; std::vector< std::list<someclass*>* > products; products.push_back(&styleproducts); products.push_back(&itemproducts); // etc... std::list<someclass*> allproducts; std::for_each(products.begin(), products.end(), mergewith(allproducts));
if using c++11 or later:
std::vector< std::list<someclass*>* > products; products.push_back(&styleproducts); products.push_back(&itemproducts); // etc... std::list<someclass*> allproducts; (auto l: products) { allproducts.merge(*l); }
alternatively:
#include <algorithm> std::vector< std::list<someclass*>* > products; products.push_back(&styleproducts); products.push_back(&itemproducts); // etc... std::list<someclass*> allproducts; std::for_each(products.begin(), products.end(), [&](std::list<someclass*> *l) { allproducts.merge(*l); } ); // if using c++14 or later, can replace // "std::list<someclass*> *l" in lambda "auto l"...