Skip to content

Instantly share code, notes, and snippets.

@EmmanuelPL
Last active July 18, 2016 18:32
Show Gist options
  • Save EmmanuelPL/93e15bb7097a01481168ab6eb891834c to your computer and use it in GitHub Desktop.
Save EmmanuelPL/93e15bb7097a01481168ab6eb891834c to your computer and use it in GitHub Desktop.
Example of the use of std vector, set, stringstream and for_each
#include <iostream>
#include <algorithm>
#include <set>
#include <vector>
#include <string>
#include <sstream>
std::string set_to_string(std::set<int> set) {
std::stringstream out;
out << '{';
if (!set.empty())
{
for_each(
set.begin(),
--set.end(),
[&out](const int &value)
{
out << value << ',';
}
);
out << *(--set.end());
}
out << '}';
out.flush();
return out.str();
}
int main()
{
std::vector<std::set<int>> vector;
std::set<int> aux = { 0 };
vector.push_back(aux);
aux = {1,2,4,8,16};
vector.push_back(aux);
aux = {};
vector.push_back(aux);
for_each(
vector.begin(),
vector.end(),
[](const auto value)
{
std::cout << set_to_string(value) << "\n";
}
);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment