user3684792 user3684792 - 1 year ago 69
C++ Question

c++ vector concatenator to avoid copying

I have a situation where I have 2 vectors:

vector<char> v1 = {1,3,4}
vector<char> v2 = {4,5,6}

which in reality can grow very large.

What is required of these vectors is that they are each passed into a function with signature:

template<class supports_[]_operator>
double absolutely_awesome_function(const supports_[]_operator& v)

However, i also need the concatenated vector to be passed to this function.


v3= {1,3,4,4,5,6}

at which point it is never used again.

Now, there are obviously, a number of ways to simply make a new object and pass that in, however as I said the vectors are large and the 'absolutely_awesome_function' is very fast, so this is obviously not ideal.

I thought about making an object like so:

template<class T>
class AmIAVector{
AmIAVector(const T& vec_1, const T& vec_2)

auto operator [](int i) const {return 'an index into either vec1 or vec2'}

But is there a better way / does something already exist for this

Answer Source

There are several possible solutions to this.

Since absolutely_awesome_function specifically takes a vector you are stuck.

With a redesign, it could take iterators as most stl algorithms do, then you can provide suitable begin and end iterators.

Something like boost::join will just work.

boost::join(v1, v2)

Related questions:, or Writing an iterator that makes several containers look as one

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download