Here is the code(NextPermutation.cpp):
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
template<typename Iter>
bool next_permutation(Iter first, Iter last) {
auto rfirst = reverse_iterator<Iter>(last);
auto rlast = reverse_iterator<Iter>(first);
auto pivot = next(rfirst);
while (pivot != rlast && prev(pivot) <= pivot) pivot++;
if (pivot == rlast) {
reverse(rfirst, rlast);
return true;
}
auto change = find_if(rfirst, pivot, bind1st(less<int>(), *prev(pivot)));
swap(*change, *prev(pivot));
reverse(rfirst, pivot);
return false;
}
void nextPermutation(vector<int> &num) {
next_permutation(num.begin(), num.end());
for (auto i : num)
cout << i << " ";
cout << endl;
}
int main() {
vector<int> num = {6, 8, 7, 4, 3, 2};
nextPermutation(num);
return 0;
}
then I run the command: clang++ NextPermutation.cpp -std=c++11 output is:
NextPermutation.cpp:22:5: error: call to 'next_permutation' is ambiguous
next_permutation(num.begin(), num.end());
^~~~~~~~~~~~~~~~
/usr/bin/../lib/gcc/x86_64-redhat-linux/4.8.2/../../../../include/c++/4.8.2/bits/stl_algo.h:3654:5: note: candidate function [with
_BidirectionalIterator = __gnu_cxx::__normal_iterator<int *, std::vector<int, std::allocator<int> > >]
next_permutation(_BidirectionalIterator __first,
^
NextPermutation.cpp:6:6: note: candidate function [with Iter = __gnu_cxx::__normal_iterator<int *, std::vector<int,
std::allocator<int> > >]
bool next_permutation(Iter first, Iter last) {
^
1 error generated.
As above shows the problem.
Aucun commentaire:
Enregistrer un commentaire