Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
512 views
in Technique[技术] by (71.8m points)

c++ - Merge two STL vectors with an alternation pattern

I have two STL vectors A and B and need to merge them into a third one, where the elements should be ordered in a way, that every nth element in the output vector should be of vector B. My current code looks something like this:

std::vector<int> a(10, 4);
std::vector<int> b(10, 8);
std::vector<int> c;
static const std::size_t STEP(3);

std::vector<int>::const_iterator bIt = b.begin();
for(std::vector<int>::const_iterator aIt = a.begin();
    aIt != a.end(); ++aIt)
{
    c.push_back(*aIt);
    if((c.size() + 1) % STEP == 0)
    {
        c.push_back(*bIt);
        ++bIt; //assume b is large enough
    }
}

The vector c now looks like: 4 4 8 4 4 8 ...

This works fine, but I'm curious if there isn't a more elegant solution. Is there any way use a STL algorithm instead of my hand-written loops?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

I must admit I quite like Potatoswatter solution... quite.

As he demonstrated, this is not an issue of algorithm, but of iteration. However his solution does not quite fit the bill because testing for the end of the iteration is very difficult: it requires much care in the preparation of the containers and the creation of the iterators to avoid undefined behavior.

I think the problem could be much better expressed using a concept that is quite similar to iterators: views.

A view is an Adapter interface over one or several containers. It doesn't actually contain anything itself (that's the important part). It does however exposes an interface similar to that of a container so that you can code with the usual idioms.

The beautiful things about views, is that you can often compose them.

For example, one very simple view:

/// Only allow to see a range of the container:
/// std::vector<int> v(40, 3); // { 3, 3, 3, ... }
/// auto rv = make_range_view(v, 4, 5);
/// rv exposes the elements in the range [4,9)
/// in debug mode, asserts that the range is sufficiently large
template <typename Container>
class range_view
{
};

For your question, you would rather create an interleave_view:

/// Allow to interleave elements of 2 containers each at its own pace
/// std::vector<int> v(40, 3);
/// std::set<int> s = /**/;
/// 
/// auto iv = make_interleave_view(v, 3, s, 1);
/// Takes 3 elements from v, then 1 from s, then 3 from v, etc...
template <typename C1, typename C2>
class interleave_view
{
};

Here the issue of the end iterator is somehow alleviated because we know both containers and thus we are able to create the iterators ourselves, ensuring we pass the proper parameters.

Of course it can become a bit more tedious if the containers do not offer an efficient "size" member (list doesn't, it's O(n)).

The general principle however is that a view gives you more control (and allows more checks), and is safer to use because you control precisely the creation of the iterators.

Note that in C++0x the interleave_view would typically accomodate an unbounded number of sequences.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

2.1m questions

2.1m answers

60 comments

57.0k users

...