If it's a set, and the type of the elements has an appropriate output operator<< then you can just iterate through and print.
Code: Select all
#include <iostream>
#include <set>
int main()
{
std::set<int> used = {5, 3, 8};
for (const auto& iter : used) {
std::cout << iter << std::endl;
}
return 0;
}
If the set's element type doesn't have an appropriate output operator you'll need to call something to generate an output-able version of the
iter variable with something like (assuming the type has a function
to_string() which returns a printable string representation)
Code: Select all
#include <iostream>
#include <set>
#include <string>
// MyClass orders the set based on value.
class MyClass {
int value;
std::string name;
public:
MyClass(int v, std::string n) : value(v), name(n) {};
std::string to_string() const
{
return "Value:" + std::to_string(value) + "Name:" + name;
}
struct comp {
bool operator() (const MyClass &lhs, const MyClass &rhs) const
{ return lhs.value < rhs.value;}
};
};
int main()
{
std::set<MyClass,MyClass::comp> used = {{3,"Three"}, {1,"One"}, {5,"Five"}};
for (const auto& iter : used) {
std::cout << iter.to_string() << std::endl;
}
return 0;
}
Or just output the parts of the element if you can access them :-
Code: Select all
#include <iostream>
#include <set>
#include <string>
struct MyClass {
int value;
std::string name;
MyClass(int v, std::string n) : value(v), name(n) {};
struct comp {
bool operator() (const MyClass &lhs, const MyClass &rhs) const
{ return lhs.value < rhs.value;}
};
};
int main()
{
std::set<MyClass,MyClass::comp> used = {{3,"Three"}, {1,"One"}, {5,"Five"}};
for (const auto& iter : used) {
std::cout << iter.name << " (" << iter.value << ")" << std::endl;
}
return 0;
}