That's quite simple to accomplish using nested range-based loops (introduced by C++11):
for(std::set<int> const &mySet : setVector){
for(const int i : mySet){
std::cout << i << " ";
}
}
Pergunta
Is there anyway to print a vector of sets easily?
#include <iostream>
#include <set>
#include <vector>
int main ()
{
std::set<int> myset;
std::vector<std::set<int> > setVector;
int myints[] = {5,10,15};
int elementCount = sizeof(myints) / sizeof(myints[0]);
myset.insert(myints, myints + elementCount);
setVector.push_back(myset);
std::cout << "Elements in vector: " << setVector.size() << " \n";
}
I have added the set to the vector, is there anyway to print it out?
This is just a proof of concept, eventually I will add many more sets to this vector, so ideally I will need to print from the beginning of the vector to the end
Thank you
Solução
That's quite simple to accomplish using nested range-based loops (introduced by C++11):
for(std::set<int> const &mySet : setVector){
for(const int i : mySet){
std::cout << i << " ";
}
}
Outras dicas
You can have a look to my (header only) to_string library.
std::ostream& operator<<(std::ostream& os, const std::set<int>& s) {
std::copy(s.begin(), s.end(),
std::ostream_iterator<int>(os,","));
return os;
}
template<typename T>
std::ostream& operator<<(std::ostream& os, const std::vector<T>& s) {
os << "{";
typename std::vector<T>::const_iterator it = s.begin();
for( ; it != s.end(); ++it)
os << *it;
os << "}";
return os;
}
Then you can do
std::cout << setVector;
You can print them directly as :
for(int i=0;i<setVector.size();i++)
{
for(set<int>::iterator iter=setVector[i].begin();iter!=setVector[i].end();iter++)
}