How to randomize STL list using std::random_shuffle??

Using Visual Studio 2005. I have the following bit of code which generates a bunch of baloney errors... ideas :

#include <stdio.h>
#include <list>
#include <windows.h>
#include "DiskAccess.h"
#include "resource.h"
#include <algorithm>

// by the way somewhere <string> gets included too... don't know if that matters

list<MySlide*> slides;
// fill the slides up dynamically here
list<MySlide*>::iterator begin, end;
begin = slides.begin();
end = slides.end();
std::random_shuffle(begin, end); /* shuffle elements */

I get the stupid errors:
ERRORS:
c:\Program Files\Microsoft Visual Studio 8\VC\include\algorithm(1509) : error C2784: 'std::_Vb_iterator<_MycontTy> std::operator +(_Vb_iterator<_MycontTy>::difference_type,std::_Vb_iterator<_MycontTy>)' : could not deduce template argument for 'std::_Vb_iterator<_MycontTy>' from '__w64 int'
c:\Program Files\Microsoft Visual Studio 8\VC\include\vector(1800) : see declaration of 'std::operator +'
c:\Program Files\Microsoft Visual Studio 8\VC\include\algorithm(1517) : see reference to function template instantiation 'void std::_Random_shuffle<std::list<_Ty>::_Iterator<_Secure_validation>,__w64 int>(_RanIt,_RanIt,_Diff *)' being compiled
with
[
_Ty=MySlide *,
_Secure_validation=false,
_RanIt=std::list<MySlide *>::_Iterator<false>,
_Diff=__w64 int
]
.\DiskAccess.cpp(83) : see reference to function template instantiation 'void std::random_shuffle<std::list<_Ty>::_Iterator<_Secure_validation>>(_RanIt,_RanIt)' being compiled
with
[
_Ty=MySlide *,
_Secure_validation=true,
_RanIt=std::list<MySlide *>::_Iterator<true>
]
c:\Program Files\Microsoft Visual Studio 8\VC\include\algorithm(1509) : error C2784: 'std::_Vb_const_iterator<_MycontTy> std::operator +(_Vb_const_iterator<_MycontTy>::difference_type,std::_Vb_const_iterator<_MycontTy>)' : could not deduce template argument for 'std::_Vb_const_iterator<_MycontTy>' from '__w64 int'
c:\Program Files\Microsoft Visual Studio 8\VC\include\vector(1695) : see declaration of 'std::operator +'
c:\Program Files\Microsoft Visual Studio 8\VC\include\algorithm(1509) : error C2784: 'std::_Vector_iterator<_Ty,_Alloc> std::operator +(_Vector_iterator<_Ty,_Alloc>::difference_type,std::_Vector_iterator<_Ty,_Alloc>)' : could not deduce template argument for 'std::_Vector_iterator<_Ty,_Alloc>' from '__w64 int'
c:\Program Files\Microsoft Visual Studio 8\VC\include\vector(396) : see declaration of 'std::operator +'
c:\Program Files\Microsoft Visual Studio 8\VC\include\algorithm(1509) : error C2784: 'std::_Vector_const_iterator<_Ty,_Alloc> std::operator +(_Vector_const_iterator<_Ty,_Alloc>::difference_type,std::_Vector_const_iterator<_Ty,_Alloc>)' : could not deduce template argument for 'std::_Vector_const_iterator<_Ty,_Alloc>' from '__w64 int'
c:\Program Files\Microsoft Visual Studio 8\VC\include\vector(264) : see declaration of 'std::operator +'
c:\Program Files\Microsoft Visual Studio 8\VC\include\algorithm(1509) : error C2784: 'std::basic_string<_Elem,_Traits,_Alloc> std::operator +(const std::basic_string<_Elem,_Traits,_Alloc> &,const _Elem)' : could not deduce template argument for 'const std::basic_string<_Elem,_Traits,_Alloc> &' from 'std::list<_Ty>::_Iterator<_Secure_validation>'
with
[
_Ty=MySlide *,
_Secure_validation=false
]
c:\Program Files\Microsoft Visual Studio 8\VC\include\string(61) : see declaration of 'std::operator +'
c:\Program Files\Microsoft Visual Studio 8\VC\include\algorithm(1509) : error C2784: 'std::basic_string<_Elem,_Traits,_Alloc> std::operator +(const std::basic_string<_Elem,_Traits,_Alloc> &,const _Elem)' : could not deduce template argument for 'const std::basic_string<_Elem,_Traits,_Alloc> &' from 'std::list<_Ty>::_Iterator<_Secure_validation>'
with
[
_Ty=MySlide *,
_Secure_validation=false
]
c:\Program Files\Microsoft Visual Studio 8\VC\include\string(61) : see declaration of 'std::operator +'
c:\Program Files\Microsoft Visual Studio 8\VC\include\algorithm(1509) : error C2784: 'std::basic_string<_Elem,_Traits,_Alloc> std::operator +(const std::basic_string<_Elem,_Traits,_Alloc> &,const _Elem)' : could not deduce template argument for 'const std::basic_string<_Elem,_Traits,_Alloc> &' from 'std::list<_Ty>::_Iterator<_Secure_validation>'
with
[
_Ty=MySlide *,
_Secure_validation=false
]
c:\Program Files\Microsoft Visual Studio 8\VC\include\string(61) : see declaration of 'std::operator +'
c:\Program Files\Microsoft Visual Studio 8\VC\include\algorithm(1509) : error C2784: 'std::basic_string<_Elem,_Traits,_Alloc> std::operator +(const std::basic_string<_Elem,_Traits,_Alloc> &,const _Elem)' : could not deduce template argument for 'const std::basic_string<_Elem,_Traits,_Alloc> &' from 'std::list<_Ty>::_Iterator<_Secure_validation>'
with
[
_Ty=MySlide *,
_Secure_validation=false
]
c:\Program Files\Microsoft Visual Studio 8\VC\include\string(61) : see declaration of 'std::operator +'
c:\Program Files\Microsoft Visual Studio 8\VC\include\algorithm(1509) : error C2784: 'std::basic_string<_Elem,_Traits,_Alloc> std::operator +(const std::basic_string<_Elem,_Traits,_Alloc> &,const _Elem *)' : could not deduce template argument for 'const std::basic_string<_Elem,_Traits,_Alloc> &' from 'std::list<_Ty>::_Iterator<_Secure_validation>'
with
[
_Ty=MySlide *,
_Secure_validation=false
]
c:\Program Files\Microsoft Visual Studio 8\VC\include\string(51) : see declaration of 'std::operator +'
c:\Program Files\Microsoft Visual Studio 8\VC\include\algorithm(1509) : error C2784: 'std::basic_string<_Elem,_Traits,_Alloc> std::operator +(const std::basic_string<_Elem,_Traits,_Alloc> &,const _Elem *)' : could not deduce template argument for 'const std::basic_string<_Elem,_Traits,_Alloc> &' from 'std::list<_Ty>::_Iterator<_Secure_validation>'
with
[
_Ty=MySlide *,
_Secure_validation=false
]
c:\Program Files\Microsoft Visual Studio 8\VC\include\string(51) : see declaration of 'std::operator +'
c:\Program Files\Microsoft Visual Studio 8\VC\include\algorithm(1509) : error C2784: 'std::basic_string<_Elem,_Traits,_Alloc> std::operator +(const std::basic_string<_Elem,_Traits,_Alloc> &,const _Elem *)' : could not deduce template argument for 'const std::basic_string<_Elem,_Traits,_Alloc> &' from 'std::list<_Ty>::_Iterator<_Secure_validation>'
with
[
_Ty=MySlide *,
_Secure_validation=false
]
c:\Program Files\Microsoft Visual Studio 8\VC\include\string(51) : see declaration of 'std::operator +'
c:\Program Files\Microsoft Visual Studio 8\VC\include\algorithm(1509) : error C2784: 'std::basic_string<_Elem,_Traits,_Alloc> std::operator +(const std::basic_string<_Elem,_Traits,_Alloc> &,const _Elem *)' : could not deduce template argument for 'const std::basic_string<_Elem,_Traits,_Alloc> &' from 'std::list<_Ty>::_Iterator<_Secure_validation>'
with
[
_Ty=MySlide *,
_Secure_validation=false
]
c:\Program Files\Microsoft Visual Studio 8\VC\include\string(51) : see declaration of 'std::operator +'
c:\Program Files\Microsoft Visual Studio 8\VC\include\algorithm(1509) : error C2784: 'std::basic_string<_Elem,_Traits,_Alloc> std::operator +(const _Elem,const std::basic_string<_Elem,_Traits,_Alloc> &)' : could not deduce template argument for 'const std::basic_string<_Elem,_Traits,_Alloc> &' from '__w64 int'
c:\Program Files\Microsoft Visual Studio 8\VC\include\string(41) : see declaration of 'std::operator +'
c:\Program Files\Microsoft Visual Studio 8\VC\include\algorithm(1509) : error C2784: 'std::basic_string<_Elem,_Traits,_Alloc> std::operator +(const _Elem *,const std::basic_string<_Elem,_Traits,_Alloc> &)' : could not deduce template argument for 'const _Elem *' from 'std::list<_Ty>::_Iterator<_Secure_validation>'
with
[
_Ty=MySlide *,
_Secure_validation=false
]
c:\Program Files\Microsoft Visual Studio 8\VC\include\string(31) : see declaration of 'std::operator +'
c:\Program Files\Microsoft Visual Studio 8\VC\include\algorithm(1509) : error C2784: 'std::basic_string<_Elem,_Traits,_Alloc> std::operator +(const std::basic_string<_Elem,_Traits,_Alloc> &,const std::basic_string<_Elem,_Traits,_Alloc> &)' : could not deduce template argument for 'const std::basic_string<_Elem,_Traits,_Alloc> &' from 'std::list<_Ty>::_Iterator<_Secure_validation>'
with
[
_Ty=MySlide *,
_Secure_validation=false
]
c:\Program Files\Microsoft Visual Studio 8\VC\include\string(21) : see declaration of 'std::operator +'
c:\Program Files\Microsoft Visual Studio 8\VC\include\algorithm(1509) : error C2784: 'std::basic_string<_Elem,_Traits,_Alloc> std::operator +(const std::basic_string<_Elem,_Traits,_Alloc> &,const std::basic_string<_Elem,_Traits,_Alloc> &)' : could not deduce template argument for 'const std::basic_string<_Elem,_Traits,_Alloc> &' from 'std::list<_Ty>::_Iterator<_Secure_validation>'
with
[
_Ty=MySlide *,
_Secure_validation=false
]
c:\Program Files\Microsoft Visual Studio 8\VC\include\string(21) : see declaration of 'std::operator +'
c:\Program Files\Microsoft Visual Studio 8\VC\include\algorithm(1509) : error C2784: 'std::basic_string<_Elem,_Traits,_Alloc> std::operator +(const std::basic_string<_Elem,_Traits,_Alloc> &,const std::basic_string<_Elem,_Traits,_Alloc> &)' : could not deduce template argument for 'const std::basic_string<_Elem,_Traits,_Alloc> &' from 'std::list<_Ty>::_Iterator<_Secure_validation>'
with
[
_Ty=MySlide *,
_Secure_validation=false
]
c:\Program Files\Microsoft Visual Studio 8\VC\include\string(21) : see declaration of 'std::operator +'
c:\Program Files\Microsoft Visual Studio 8\VC\include\algorithm(1509) : error C2784: 'std::basic_string<_Elem,_Traits,_Alloc> std::operator +(const std::basic_string<_Elem,_Traits,_Alloc> &,const std::basic_string<_Elem,_Traits,_Alloc> &)' : could not deduce template argument for 'const std::basic_string<_Elem,_Traits,_Alloc> &' from 'std::list<_Ty>::_Iterator<_Secure_validation>'
with
[
_Ty=MySlide *,
_Secure_validation=false
]
c:\Program Files\Microsoft Visual Studio 8\VC\include\string(21) : see declaration of 'std::operator +'
c:\Program Files\Microsoft Visual Studio 8\VC\include\algorithm(1509) : error C2784: 'std::_String_iterator<_Elem,_Traits,_Alloc> std::operator +(_String_iterator<_Elem,_Traits,_Alloc>::difference_type,std::_String_iterator<_Elem,_Traits,_Alloc>)' : could not deduce template argument for 'std::_String_iterator<_Elem,_Traits,_Alloc>' from '__w64 int'
c:\Program Files\Microsoft Visual Studio 8\VC\include\xstring(438) : see declaration of 'std::operator +'
c:\Program Files\Microsoft Visual Studio 8\VC\include\algorithm(1509) : error C2784: 'std::_String_const_iterator<_Elem,_Traits,_Alloc> std::operator +(_String_const_iterator<_Elem,_Traits,_Alloc>::difference_type,std::_String_const_iterator<_Elem,_Traits,_Alloc>)' : could not deduce template argument for 'std::_String_const_iterator<_Elem,_Traits,_Alloc>' from '__w64 int'
c:\Program Files\Microsoft Visual Studio 8\VC\include\xstring(298) : see declaration of 'std::operator +'
c:\Program Files\Microsoft Visual Studio 8\VC\include\algorithm(1509) : error C2784: 'std::reverse_iterator<_RanIt> std::operator +(_Diff,const std::reverse_iterator<_RanIt> &)' : could not deduce template argument for 'const std::reverse_iterator<_RanIt> &' from '__w64 int'
c:\Program Files\Microsoft Visual Studio 8\VC\include\xutility(1809) : see declaration of 'std::operator +'
c:\Program Files\Microsoft Visual Studio 8\VC\include\algorithm(1509) : error C2676: binary '+' : 'std::list<_Ty>::_Iterator<_Secure_validation>' does not define this operator or a conversion to a type acceptable to the predefined operator
with
[
_Ty=MySlide *,
_Secure_validation=false
]


Answer this question

How to randomize STL list using std::random_shuffle??

  • RHTCJohn

    Actually changing the STL container class to <vector>, worked great... Thanks so much for your help!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!11

  • ursus zeta

    I see....

    Is there an alternate solution that you see What would be a better solution

  • pr0gr4m3r

    I believe that the problem here is that random_shuffle requires an iterator that is a random_iterator and std::list iterators are not random_iterators.

  • Chris McCabe

    I have now:
    std::random_shuffle(slides.begin(), slides.end()); // which yields:
    .\DiskAccess.cpp(89) : error C2664: 'void std::random_shuffle(_RanIt,_RanIt)' : cannot convert parameter 1 from 'std::list<_Ty>::_Iterator<_Secure_validation>' to 'MySlide *'
    with
    [
    _RanIt=MySlide *
    ]
    and
    [
    _Ty=MySlide *,
    _Secure_validation=true
    ]
    No user-defined-conversion operator available that can perform this conversion, or the operator cannot be called

    and when I try changing it to:
    std::random_shuffle(slides.begin(), slides.end()); // which yields:
    .\DiskAccess.cpp(89) : error C2059: syntax error : '>'

    I also tried casting but that failed badly. Any ideas

  • How to randomize STL list using std::random_shuffle??