Useful C++ helper functions

My brain sucks. It can remember the lyrics to songs or the power draw of a electronic component, but I keep forgetting those useful helper function you sometimes need. This sends me off Google again, searching through excellent sites like StackOverflow etc. Time to make a list...

Remove characters from a string:

#include <algorithm>
#include <iterator>
std::string result;
std::remove_copy(input.begin(), input.end(), std::back_inserter(result), 'a');

Replace characters in a string:

std::replace(input.begin(), input.end(), 'a', 'b');

Convert integer to std::string without the << operator:

std::to_string((long long)number);

Create a shared_ptr managing an array:

std::shared_ptr<unsigned char> data = std::shared_ptr<unsigned char>(new unsigned char[size], [](unsigned char *p) { delete[] p; });
(You must use a custom deleter, because by default delete is called, not delete[]. Here a lambda function is passed to the shared_ptr constructor)

Advance an iterator by a number:

std::vector<int> list;
std::vector<int>::const_iterator iter = list.cbegin();
advance(iter, 5);

Append std::vector elements from one vector to another:

std::vector<int> destination;
std::vector<int> source;
destination.insert(destination.end(), source.cbegin(), source.cend());

(Why there is no push_back() overload for this is beyond me...)

Append std::vector elements from one vector to another (without duplicates):

std::vector<int> destination;
std::vector<int> source;
std::copy_if(
source.cbegin(), source.cend(), std::back_inserter(destination), [&](const int & element) { return std::find(destination.cbegin(), destination.cend(), element) == destination.cend(); });

For loops with iterator and auto:

std::vector<int> foo;
for (auto fooIt = foo.cbegin(); fooIt != foo.cend(); ++fooIt) {
... }

Use this when you need an iterator.

For each with auto:

std::vector<int> foo;
for (const auto & oneOfFoo : foo) {
... }

Make sure oneOfFoo is the type you want. Be careful when using auto-pointers etc.

Output all content of a std::vector to a stream:

std::vector<std::string> words; //contains some strings
std::stringstream ss;
std::copy(words.begin(), words.end(), std::ostream_iterator(ss));

Comments