Skip to content

Instantly share code, notes, and snippets.

@DForshner
Created June 29, 2013 18:21
Show Gist options
  • Save DForshner/5892128 to your computer and use it in GitHub Desktop.
Save DForshner/5892128 to your computer and use it in GitHub Desktop.
static const int values[] = {0,1,2,3,4,5,6,7,8,9};
vector<int> v (values, values + sizeof(values) / sizeof(values[0]) );
// Using C++11 ranged-based for loop
std::stringstream ss;
for (int n : v)
ss << n;
Assert::IsTrue(ss.str() == "0123456789");
ss.str(""); ss.clear(); // clear string stream
// Using iterators
for (auto n = v.begin(); n != v.end(); ++n)
{
ss << (int)*n;
}
Assert::IsTrue(ss.str() == "0123456789");
ss.str(""); ss.clear(); // clear string stream
// Using std::for_each with lambda
std::for_each(v.begin(), v.end(), [&ss] (int val)
{
ss << val;
});
Assert::IsTrue(ss.str() == "0123456789");
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment