themmfa
themmfa

Reputation: 539

How random_shuffle from vector in function?

Here is my code;

This is my vector:

vector<string>day1{"Monday","Tuesday","Wednesday","Thursday","Friday"};

I am using the vector in this code:

void Course::addCourse()
{
    string a;
    int b;
    int total;
    int courseAdd;
    cout<<"How many course you want to add?"<<endl;
    cin>>courseAdd;
    cout<<"Enter the name of course and number of hours.\nWARNING:For now just enter max 6 course hours"<<endl;
    for(int i=0;i<courseAdd;++i)
    {
        setCourseName(a);
        setHours(b);
        getCourseList().insert({a, b});
        total += getHours();
        getMatchMap().insert({make_pair(a,b),make_pair(day1[i],dayHours)});
    }

I want to shuffle the days before inserting the map.I mean here:

getMatchMap().insert({make_pair(a,b),make_pair(day1[i],dayHours)});

How can i random_shuffle the vector elements or where do i need to do this?

Upvotes: 0

Views: 63

Answers (1)

Andreas DM
Andreas DM

Reputation: 10998

Before the loop, you could do:

std::random_device rd;
std::mt19937 g(rd());
std::shuffle(day1.begin(), day1.end(), g);

To shuffle your vector.

Upvotes: 3

Related Questions