Permutations of Names

I'm fairly new to C++ programming and am facing quite an issue with writing a method that with create and output all permutations of a list of names that the user inputs. The output works okay with three names input for every other line, but won't hold the first name down. Then with more names added the issue is much worse. I honestly have no idea how to fix this or what even is wrong with it so any help would be much appreciated. Thanks! Code below:

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

using namespace std;



// TODO: Write method to create and output all permutations of the list of names.
void AllPermutations(const vector<string> &permList, const vector<string> &nameList) {

vector <string> tempVec;
tempVec.resize(nameList.size());
vector <string> tempPerm;

unsigned int i;

string tmpString;


if (nameList.size() == 0){
for (i = 0;i < permList.size(); i++){
cout << endl;

}
}
else {
for (i = 0; i < nameList.size(); i++){
tempVec = nameList;
tmpString = tempVec.at(i);
cout << tmpString<< " " ;
tempPerm.push_back(tmpString);
tempVec.erase(tempVec.begin() + i);
AllPermutations(tempPerm, tempVec);
}

}
}

int main(int argc, char* argv[]) {
vector<string> nameList;
vector<string> permList;
string name;

// TODO: Read in a list of names; stop when -1 is read. Then call recursive method.

cin >> name;
while (name != "-1"){
nameList.push_back(name);
cin >> name;
}
AllPermutations(permList, nameList);

return 0;
}
I am not sure what you are doing, either :)

I always think of them like counting in binary.
0000
0001
0010
0011
...
you make loops that change just 1 item per iteration.
so if you have w a s d f g and need 3 of them:
w a s
w a d
w a f
w a g
that is all the w a set.
so have to change the a:
w s d
w s f
w s g
... and so on. after a bit the w changes and you go again. Its just pure iteration, you don't need all that copying and memory abuse (push/pop/erase/ movements and copies).
if you need to KEEP each one instead of print each one, you DO need to store them, but even so all the erase and such seem overkill. I would consider just keeping an index into the original container for each (pointer or array index) rather than copy and store all that data so many times.

these get large moderately quickly, and printing is slow. I would not advise testing 100 names choose 50 or something :P
Last edited on
Well, I do need to print them. Here’s what my output is meant to look like with just three names input:

Julia Lucas Mia
Julia Mia Lucas
Lucas Julia Mia
Lucas Mia Julia
Mia Julia Lucas
Mia Lucas Julia

But here’s what my output looks like instead:

Julia Lucas Mia
Mia Lucas
Lucas Julia Mia
Mia Julia
Mia Julia Lucas
Lucas Julia

So I more so mean it’s not printing out that first name again on the second loop for the output, if that makes sense? It gets even worse when the input is more names, each line out output dropping a name practically unless it is that initial loop for each group.
http://www.cplusplus.com/reference/algorithm/next_permutation/

1
2
3
4
5
6
7
8
9
10
11
12
13
14
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;

int main ()
{
   vector<string> names = { "Julia", "Lucas", "Mia" };
   sort( names.begin(), names.end() );
   do {
      for ( string s : names ) cout << s << ' ';
      cout << '\n';
   } while ( next_permutation( names.begin(), names.end() ) );
}
Last edited on
A brute force solution with minimal effort applied, but showing that you only need iteration:
if you get more than 6 names you will want to rewrite it more intelligently. (and if you change the number you have to fix the hard coded parts... its just a silly example ).
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22

int main()
{
  vector<string> s{ {"Julia"}, {"Lucas"}, {"Mia"}, {"Timmy"}};
  vector<int> dx(s.size());
  std::iota(dx.begin(), dx.end(), 0);
  string n,x;
  for(int i = 123; i < 3211; i++)
  {
	n = "";
    if(i < 1000) n+='0';
	n += to_string(i);
    x = n;
	std::sort(x.begin(), x.end());
	if(x=="0123")
	{
	for(auto a:n)
	 cout << s[a-'0'] << " ";
    cout << endl;
    }
  } 
}



1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
Julia Lucas Mia Timmy
Julia Lucas Timmy Mia
Julia Mia Lucas Timmy
Julia Mia Timmy Lucas
Julia Timmy Lucas Mia
Julia Timmy Mia Lucas
Lucas Julia Mia Timmy
Lucas Julia Timmy Mia
Lucas Mia Julia Timmy
Lucas Mia Timmy Julia
Lucas Timmy Julia Mia
Lucas Timmy Mia Julia
Mia Julia Lucas Timmy
Mia Julia Timmy Lucas
Mia Lucas Julia Timmy
Mia Lucas Timmy Julia
Mia Timmy Julia Lucas
Mia Timmy Lucas Julia
Timmy Julia Lucas Mia
Timmy Julia Mia Lucas
Timmy Lucas Julia Mia
Timmy Lucas Mia Julia
Timmy Mia Julia Lucas
Timmy Mia Lucas Julia
Last edited on
Topic archived. No new replies allowed.