강의

멘토링

커뮤니티

인프런 커뮤니티 질문&답변

Solioquies님의 프로필 이미지
Solioquies

작성한 질문수

홍정모의 따라하며 배우는 C++

9.4 비교 연산자 오버로딩 하기

random shuffle이 안되는데 이유를 모르겠습니다.

작성

·

1.2K

0

#include <iostream> 

#include <vector>

#include <algorithm>

using namespace std;

class Cents

{

private:

int m_cents;

public:

Cents(int cents = 0) { m_cents = cents; }

int getCents() const { return m_cents; }

int& getCents() { return m_cents; }

friend std::ostream& operator << (std::ostream& out, const Cents& cents)

{

out << cents.m_cents;

return out;

}

};

int main()

{

vector<Cents> arr(20);

for (unsigned i = 0; i < 20; ++i)

{

arr[i].getCents() = i;

}

std::random_shuffle(begin(arr), end(arr));

for (auto& e : arr)

{

cout << e << " ";

}

cout << endl;

return 0;

}

강의 내용 그대로 친 것 같은데 random 셔플부분만 std에 존재하지 않는다고 계속 에러나네요.

답변 2

0

홍정모님의 프로필 이미지
홍정모
지식공유자

안녕하세요?

random_shuffle은 seed를 지정해주지 않아도 된다는 점에서 shuffle보다 편리합니다만, C++ 17 이후에서는 사용할 수가 없습니다. shuffle 사용법은 아래 코드나 위 링크의 예제를 참고하시면 됩니다. (거의 동일합니다.)

추가로 본 질문은 영상 내부에서 자막으로 보충될 예정입니다. 

// shuffle algorithm example

#include <iostream>     // std::cout

#include <algorithm>    // std::shuffle

#include <array>        // std::array

#include <random>       // std::default_random_engine

#include <chrono>       // std::chrono::system_clock

using namespace std;

class Cents

{};

int main() {

vector<Cents> arr(20);

// ...

unsigned seed = std::chrono::system_clock::now().time_since_epoch().count();

std::shuffle(begin(arr), end(arr), std::default_random_engine(seed));

// ...

return 0;

}

0

https://en.cppreference.com/w/cpp/algorithm/random_shuffle
c++ 17 이후 부터는 사라졌다는군요.
우측의 solution explore 에서 project_name 우클릭-> properties-> C/C++ - > language -> C++ Language standard를 바꿔보세요.

Solioquies님의 프로필 이미지
Solioquies

작성한 질문수

질문하기