Monday, February 23, 2009

c++ stl - vector concatenation (use std::copy or vector::insert)

My friend Saulius showed me a new way of concatenation of two vectors in c++:
//Author: Darius Kucinskas (c) 2008-2009
//Email: d[dot]kucinskas[eta]gmail[dot]com
//Blog: http://blog-of-darius.blogspot.com/
//License: GPL

std::copy(inVec.begin(), inVec.end(), std::back_inserter(outVec));
or:
//Author: Darius Kucinskas (c) 2008-2009
//Email: d[dot]kucinskas[eta]gmail[dot]com
//Blog: http://blog-of-darius.blogspot.com/
//License: GPL

outVec.insert(outVec.end(), inVec.begin(), inVec.end());
Oh, it is simple when you know it but you would never guess if you didn't know! :)