random_shuffle (STL Samples)

사용 하는 방법을 보여 줍니다 있는 random_shuffle Visual C++에서 표준 템플릿 라이브러리 (STL) 함수입니다.

template<class RandomAccessIterator> inline
   void random_shuffle(
      RandomAccessIterator First,
      RandomAccessIterator Last
   )

설명

[!참고]

프로토타입에 클래스/매개 변수 이름은 헤더 파일에서 버전이 일치 하지 않습니다.일부 가독성을 높이기 위해 수정 되었습니다.

random_shuffle 알고리즘은 시퀀스의 요소를 섞어서 [First...Last)에서 임의의 순서로 합니다.Nonpredicate 버전은 내부 난수 생성기를 사용 하 여 스왑 요소의 인덱스를 생성 합니다.Nonpredicate 버전 random_shuffle 를 사용 하 여 operator= 교체를 수행 합니다.

예제

// random_shuffle.cpp
// compile with: /EHsc
// Illustrates how to use the random_shuffle
// function.
//
// Functions:
//     random_shuffle : Shuffle the elements in a random order.
//////////////////////////////////////////////////////////////////////

// disable warning C4786: symbol greater than 255 character,
// okay to ignore
#pragma warning(disable: 4786)

#include <iostream>
#include <algorithm>
#include <functional>
#include <vector>

using namespace std ;

int main() {
   const int VECTOR_SIZE = 8 ;

   // Define a template class vector of int
   typedef vector<int> IntVector ;

   //Define an iterator for template class vector of strings
   typedef IntVector::iterator IntVectorIt ;

   IntVector Numbers(VECTOR_SIZE) ;

   IntVectorIt start, end, it ;

   // Initialize vector Numbers
   Numbers[0] = 4 ;
   Numbers[1] = 10;
   Numbers[2] = 70 ;
   Numbers[3] = 30 ;
   Numbers[4] = 10;
   Numbers[5] = 69 ;
   Numbers[6] = 96 ;
   Numbers[7] = 100;

   // location of first element of Numbers
   start = Numbers.begin();

   // one past the location last element of Numbers
   end = Numbers.end();

   cout << "Before calling random_shuffle" << endl ;

   // print content of Numbers
   cout << "Numbers {" ;
   for (it = start; it != end; it++)
      cout << " " << *it;
   cout << " }" << endl ;

   // shuffle the elements in a random order
   random_shuffle(start, end) ;

   cout << "After calling random_shuffle" << endl ;

   cout << "Numbers {" ;
   for (it = start; it != end; it++)
      cout << " " << *it;
   cout << " }" << endl ;
}

샘플 출력

Before calling random_shuffle
Numbers { 4 10 70 30 10 69 96 100 }
After calling random_shuffle
Numbers { 10 10 96 70 4 69 100 30 }

요구 사항

헤더: <algorithm>

참고 항목

개념

표준 템플릿 라이브러리 샘플