How can I combine pairs of elements from a std::set?
I have a set<string>
from "one", "two", and "three".
How can I get all p开发者_StackOverflow中文版airs from it?
- one - two
- one - three
- two - three
Use a two-level loop:
// Loop over all members.
for (set<string>::iterator j = s.begin(); j != s.end(); ++j)
{
// Loop over all members up to, but excluding, the current outer-loop member.
for (set<string>::iterator i = s.begin(); i != j; ++i)
{
do_something_with(*i, *j);
}
}
精彩评论