Hi, I'm a thai student ... now i have a premiership project but i can't pass this problem that is , How to random matching football team ?

example I have 4 team in vector<string> name

[0]=a
[1]=b
[2]=c
[3]=d

I want to matching them such as

week 1 : a VS b
c VS d
week 2 : a VS c
b VS d
week 3 : a VS d
b VS c

so how i can design my code to random an element for this result

this is my original code

// Loop for matching name
for(int i=0;i<=vc_size-2;i++)
{
for(int j=i;j<=vc_size-2;j++)
{
cout<<nam<<" VS "<<nam[j+1]<<" : ";
}
}

but it result is:

a VS b
a VS c
a VS d
b VS c
b VS d

..... <-- I don't want it .

Please, help me .........

Member Avatar for Siersan

If possible, it's a good idea to use existing libraries to do your work. For example, the C++ library supports next_permutation that can be used to get the result that you want:

#include <algorithm>
#include <iostream>
#include <string>
#include <vector>

using namespace std;

void pair_teams(vector<string>& v)
{
  next_permutation(v.begin(), v.end());
  next_permutation(v.begin(), v.end());
}

void show_games(vector<string>& v)
{
  cout<< v.at(0) <<" vs "<< v.at(1) <<endl;
  cout<< v.at(2) <<" vs "<< v.at(3) <<endl;
}

int main()
{
  string init[] = {"a", "b", "c", "d"};
  vector<string> v(init, init + 4);

  for (int i = 0; i < 3; i++) {
    show_games(v);
    pair_teams(v);
  }
}

It's easier to write correct, safe code that way. I can't say the same about calculating a permutation manually. ;)

thx. very much

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.