Generic function to flatten a container of containers

前端 未结 2 1741
予麋鹿
予麋鹿 2020-12-16 19:35

I am trying to get a better hold on iterators and generic functions. I thought it would be a useful exercise to write a function that converts container1 < contain

2条回答
  •  情书的邮戳
    2020-12-16 20:31

    std::accumulate can do it for you. You need to gather up the contents of each of the inside vectors into the outside vector.

    vector> v_of_v;
    vector v = std::accumulate(
        v_of_v.begin(), v_of_v.end(),
        vector(),
        [](vector a, vector b) {
            a.insert(a.end(), b.begin(), b.end());
            return a;
        });
    

提交回复
热议问题