是否可以在 STL 集 <t> 上实现 next_permutation()
Is it possiblle to Implement next_permutation() on a STL set<t>
给定这个集合
set<string> s = {"a","b","c"};
是否可以实施 next_permutation() 以获得所有组合,其中元素不重复且顺序很重要?
不,这是不可能的。 std::set
is an associative container and maintains an strict weak ordering. std::next_permutation
转换给定的范围,这会破坏排序。
如果你需要得到set
内容的排列,我建议你使用std::vector
。您可以将集合复制到向量中,然后从中获取排列。
std::set<int> set_data;
//fill set
std::vector<int> temp(set_data.begin(), set_data.end());
do
{
// code goes here
}
while(std::next_permutation(temp.begin(), temp.end()));
给定这个集合
set<string> s = {"a","b","c"};
是否可以实施 next_permutation() 以获得所有组合,其中元素不重复且顺序很重要?
不,这是不可能的。 std::set
is an associative container and maintains an strict weak ordering. std::next_permutation
转换给定的范围,这会破坏排序。
如果你需要得到set
内容的排列,我建议你使用std::vector
。您可以将集合复制到向量中,然后从中获取排列。
std::set<int> set_data;
//fill set
std::vector<int> temp(set_data.begin(), set_data.end());
do
{
// code goes here
}
while(std::next_permutation(temp.begin(), temp.end()));