T
tuo_pe
Hello!
I took the code example from <http://www.cplusplus.com/reference/
algorithm/random_shuffle.html> and tried to apply it to a list. But I
can't get it to work. g++ gives me a mysterious complaint.
Here's my code:
#include <iostream>
#include <algorithm>
#include <functional>
#include <list>
#include <ctime>
#include <cstdlib>
using namespace std;
// random generator function:
ptrdiff_t myrandom (ptrdiff_t i) { return rand()%i;}
// pointer object to it:
ptrdiff_t (*p_myrandom)(ptrdiff_t) = myrandom;
int main () {
srand ( unsigned ( time (NULL) ) );
int myints[] = {75,23,65,42,13,59,36};
list<int> mylist (myints,myints+7);
list<int>::iterator itl;
// using built-in random generator:
random_shuffle ( mylist.begin(), mylist.end(), p_myrandom );
cout << "Mylist contains: ";
for (itl = mylist.begin(); itl != mylist.end(); itl++)
cout << *itl << ", ";
cout << endl;
return 0;
}
This is what g++ outputs:
/usr/include/c++/4.2/bits/stl_algo.h: In function ‘void
std::random_shuffle(_RandomAccessIterator, _RandomAccessIterator,
_RandomNumberGenerator&) [with _RandomAccessIterator =
std::_List_iterator<int>, _RandomNumberGenerator = ptrdiff_t (*)
(ptrdiff_t)]’:
list.cc:24: instantiated from here
/usr/include/c++/4.2/bits/stl_algo.h:2013: error: no match for
‘operator+’ in ‘__first + 1’
list.cc:24: instantiated from here
/usr/include/c++/4.2/bits/stl_algo.h:2014: error: no match for
‘operator-’ in ‘__i - __first’
What could be the problem here? I can't decipher the error message. :-
(
tuomas
I took the code example from <http://www.cplusplus.com/reference/
algorithm/random_shuffle.html> and tried to apply it to a list. But I
can't get it to work. g++ gives me a mysterious complaint.
Here's my code:
#include <iostream>
#include <algorithm>
#include <functional>
#include <list>
#include <ctime>
#include <cstdlib>
using namespace std;
// random generator function:
ptrdiff_t myrandom (ptrdiff_t i) { return rand()%i;}
// pointer object to it:
ptrdiff_t (*p_myrandom)(ptrdiff_t) = myrandom;
int main () {
srand ( unsigned ( time (NULL) ) );
int myints[] = {75,23,65,42,13,59,36};
list<int> mylist (myints,myints+7);
list<int>::iterator itl;
// using built-in random generator:
random_shuffle ( mylist.begin(), mylist.end(), p_myrandom );
cout << "Mylist contains: ";
for (itl = mylist.begin(); itl != mylist.end(); itl++)
cout << *itl << ", ";
cout << endl;
return 0;
}
This is what g++ outputs:
/usr/include/c++/4.2/bits/stl_algo.h: In function ‘void
std::random_shuffle(_RandomAccessIterator, _RandomAccessIterator,
_RandomNumberGenerator&) [with _RandomAccessIterator =
std::_List_iterator<int>, _RandomNumberGenerator = ptrdiff_t (*)
(ptrdiff_t)]’:
list.cc:24: instantiated from here
/usr/include/c++/4.2/bits/stl_algo.h:2013: error: no match for
‘operator+’ in ‘__first + 1’
list.cc:24: instantiated from here
/usr/include/c++/4.2/bits/stl_algo.h:2014: error: no match for
‘operator-’ in ‘__i - __first’
What could be the problem here? I can't decipher the error message. :-
(
tuomas