【发布时间】:2014-12-17 18:07:51
【问题描述】:
我需要对列表进行循环排列,例如我有:(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,第一:a tmp:a,第一:a tmp:a,第一:a tmp:a,第一:a tmp:a,第一: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;
}
如果你知道为什么不犹豫......
【问题讨论】:
-
(e,a,b,c,d,e)真的是(a,b,c,d,e)的循环排列吗?无论如何,请参阅std::rotate。 -
是的 :)。我需要实施我的解决方案,但我没有成功 ><.>
-
我不知道排列可以添加元素。
-
哦,对不起,我的错:(e,a,b,c,d)更好,我将编辑我的第一篇文章。我感觉我用的是同一个指针……
标签: c++ iterator iterator-traits