Dave's Brain

Browse - programming tips - append one stl byte vector to another

Date: 2009aug26
Language: C++

Q.  How can I efficiently append one byte vector to another?

A.  

// Here's a fast way to do it:

typedef std::vector<BYTE> BYTE_ARRAY;

void AppendMemoryToBa(BYTE_ARRAY &ba, const LPVOID in, const size_t size)
{
	size_t		before_size = ba.size();

	ba.resize(before_size + size);
	memcpy(ba.begin() + before_size, in, size);
}

// Append b to a
inline void AppendBaToBa(BYTE_ARRAY &a, const BYTE_ARRAY &b)
{
	AppendMemoryToBa(a, (LPVOID)b.begin(), b.size());
}

// Here's the slow way:
void SLOW_AppendBaToBa(BYTE_ARRAY &a, const BYTE_ARRAY &b)
{
	for (BYTE_ARRAY::const_iterator it = b.begin(); it != b.end(); it++)
	{
		a.push_back(*it);
	}
}
What this info useful to you? You can donate to say thanks

Add a comment

Sign in to add a comment
Copyright © 2008-2010, dave - Code samples on Dave's Brain is licensed under the Creative Commons Attribution 2.5 License. However other material, including English text has all rights reserved.