problem with random_shuffle() and list

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
 
T

tuo_pe

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&)

This function signature should give you an idea. It spells it out
pretty clear. *Random access iterator*. What kind of iterator
does std::list<>::begin() return?

An educated guess: list.begin() returns a bidirectional, sequential
iterator. By comparison, vector.begin() returns "random access
iterator." I got it. So should I first copy my list into a vector,
then shuffle, and copy vector to a list?
 
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;
}
An educated guess: list.begin() returns a bidirectional, sequential
iterator. By comparison, vector.begin() returns "random access
iterator." I got it. So should I first copy my list into a vector,
then shuffle, and copy vector to a list?

Oh well, I will first shuffle the array, then create the list...
Thanks for the help (or hint)! :)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,065
Latest member
OrderGreenAcreCBD

Latest Threads

Top