Visual C++에서 random_shuffle STL 함수 사용

이 문서에서는 Visual C++에서 STL(표준 템플릿 라이브러리) 함수를 사용하는 random_shuffle 방법을 설명합니다.

원래 제품 버전: Visual C++
원래 KB 번호: 156994

필수 헤더

<algorithm>

프로토 타입

template<class RandomAccessIterator, class Predicate> inline
void random_shuffle(RandomAccessIterator first,
                    RandomAccessIterator last,
                    Predicate pred)

참고

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

설명

알고리즘은 random_shuffle 시퀀스의 요소를 순서 섞습니다(먼저). last)을 임의 순서로 표시합니다.

조건자 버전은 pred 함수를 사용하여 교환할 요소의 인덱스를 생성합니다. pred는 매개 변수 n을 사용하고 0에서 ( n - 1) 범위의 정수 난수를 반환하는 함수 개체여야 합니다.

의 조건자 버전은 random_shuffle 를 사용하여 operator= 교환을 수행합니다.

샘플 코드

//////////////////////////////////////////////////////////////////////
// Compile options needed: /GX
// random_shuffle.cpp: Illustrates how to use the predicate version
//                      of the random_shuffle function.
// Functions:
//    random_shuffle: Shuffle the elements in a random order.
//     Rand: Given n, generates an integral random number in the
//          in the range 0 - (n - 1).
// of Microsoft Product Support Services,
// Software Core Developer Support.
// Copyright (c) 1996 Microsoft Corporation. All rights reserved.
//////////////////////////////////////////////////////////////////////

// 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;

// return an integral random number in the range 0 - (n - 1)
int Rand(int n)
{
    return rand() % n ;
}

void main()
{
    const int VECTOR_SIZE = 8 ;

    // Define a template class vector of int
    typedef vector<int, allocator<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;

    start = Numbers.begin();   // location of first
                                // element of Numbers
    end = Numbers.end();       // one past the location
                                // last element of Numbers
    cout << "Before calling random_shuffle:\n" << endl;

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

    // shuffle the elements in a random order.
    // the pointer_to_unary_function adapter converts a function to a
    // function object.
    random_shuffle(start, end, pointer_to_unary_function<int, int>(Rand));
    cout << "After calling random_shuffle:\n" << endl;
    cout << "Numbers { ";
    for(it = start; it != end; it++)
        cout << *it << " ";
    cout << " }\n" << endl;
}

프로그램 출력:


Before calling random_shuffle

Numbers { 4 10 70 30 10 69 96 100  }

After calling random_shuffle

Numbers { 10 30 4 70 96 100 69 10  }

참조

함수에 random_shuffle 대한 자세한 내용은 random_shuffle(STL 샘플)를 참조하세요.