我需要对列表进行循环排列,例如我有:(a,b,c,d,e)我想要(e,a,b,c,d)。但我没有成功,这里是我尝试过的:
#ifndef ALGORITHME_H
#define ALGORITHME_H
template<typename I>
void permutationCirculaire(I first, I last) {
typename std::iterator_traits<I>::value_type firstElement = *first;
typename std::iterator_traits<I>::value_type res;
I tmp = first;
for (++first; tmp != last; ++tmp) {
*first = *tmp;
std::cout << "tmp : " << *tmp << ", first : " << *first << std::endl;
++first;
}
}
#endif
我得到这个:tmp:a,first:a tmp:a,first:a tmp:a,first:a tmp:a,first:a tmp:a,first:a
我不知道为什么,我的主要:
#include <iostream>
#include <list>
#include "algorithme.h"
using namespace std;
int main() {
list<char> liste;
liste.push_back('a');
liste.push_back('b');
liste.push_back('c');
liste.push_back('d');
liste.push_back('e');
cout << "( ";
for (list<char>::iterator it = liste.begin(); it != liste.end(); ++it) {
cout << *it << " ";
}
cout << ") " << endl;
cout << "Permutation : " << endl;
permutationCirculaire(liste.begin(),liste.end());
cout << "( ";
for (list<char>::iterator it = liste.begin(); it != liste.end(); ++it) {
cout << *it << " ";
}
cout << ") " << endl;
return 0;
}
如果你知道为什么不犹豫...