-1

Hi I am doing an assignment for C++ and my teacher unfortunately can't teach me anything about C++ so I have to resort to the internet to help me out.

I'd appreciate a simple, easy way to output all of the permutations of a list, for example, if list x has 3 different values of [1,2,3], output=[1,2,3],[1,3,2],[2,1,3],[2,3,1],[3,1,2],[3,2,1] etc... the list does not necessarily have to be in that order, just needs to output all of the different combinations of 1, 2, 3 in a list. that's all. I've looked online to figure out how to do this with no luck understanding much. I've seen people use vectors for this and a bunch of other things that I have about 0% understanding of. I'd love to just copy and paste others' codes but that won't help get anywhere for this class, or anywhere with C++ since I am the only one voluntarily learning this language. So please try to explain it in layman's terms as much as possible. I am using C++11 if that helps.

This is about as far as I got myself

std::cout << "\nPart 2: Enter a list of 3 numbers, press enter after each value: \n";
std::list<int> mylist1;
int v1;
int v2;
int v3;
std::cin >> v1 >> v2 >> v3; //input that will go into the list
mylist1.push_front(v1);
mylist1.push_back(v2);
mylist1.push_back(v3);
Jive Dadson
  • 15,176
  • 9
  • 47
  • 65
  • 2
    All you have done is push some integral values into a list. You've shown no effort at generating combinations of those values. This isn't a "tell me how to do this because I haven't worked out how" forum. You tend to be rewarded if you show an effort, and run into some specific problem. You haven't done that, so voting to close. – Peter Feb 27 '18 at 04:47
  • 2
    [std::next_permutation](http://en.cppreference.com/w/cpp/algorithm/next_permutation). Enough said. – PaulMcKenzie Feb 27 '18 at 05:02
  • Be aware that combinations are not the same as permutations. You are looking for permutations. +1 for @PaulMcKenzie – Dúthomhas Feb 27 '18 at 05:04
  • @Peter calm down im genuinely just trying to learn the language, i thought this community will help but apparently few people will... i cant put effort into a language i have little to no understanding of, it's like making a student do a presentation in a foreign language they know nothing about. – Krutik Shah Feb 27 '18 at 05:55
  • @KrutikShah - people here tend to help people who demonstrate some effort to solve their problem, and have run into some difficulty in that.. You haven't shown any effort to generate permutations. – Peter Feb 27 '18 at 09:31
  • Possible duplicate of [Generating combinations in c++](https://stackoverflow.com/questions/9430568/generating-combinations-in-c) – jotik Feb 28 '18 at 12:28

1 Answers1

0

There's a function for that, it's called next_permutation.

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

void show(const std::vector<int> &vtr) {
    for (auto val : vtr)  std::cout << val << " ";
    std::cout << "\n";
}

int main() {
    std::vector<int> vec{ 12,1,6,4,0 };
    std::sort(vec.begin(), vec.end());
    show(vec);
    while (std::next_permutation(vec.begin(), vec.end())) {
        show(vec);
    }
}
Jive Dadson
  • 15,176
  • 9
  • 47
  • 65
  • what is a vector? I'm sorry im new to C++, i have no idea what any of your code means – Krutik Shah Feb 27 '18 at 05:28
  • @KrutikShah You are already using `std::list` but not know what `std::vector` is?? The first container you should have encountered is `std::vector`. Who taught you `std::list` before `std::vector`? – PaulMcKenzie Feb 27 '18 at 05:30
  • Krutik, a vector is somewhat like a list, but implemented differently. It is all in one chunk, rather than being linked together. You are not going to be able to learn C++ by asking questions on StackOverflow. (@PaulMcKenzie) – Jive Dadson Feb 27 '18 at 05:31
  • If you want to know what the code "means", try running it. It is a complete answer to your question. – Jive Dadson Feb 27 '18 at 05:41
  • alright I'll try this out. seems like I'll have to do some more research on vectors and lists after this, but I have a general understanding, thanks. – Krutik Shah Feb 27 '18 at 05:47
  • 1
    @KrutikShah One of my favourite go-to places is http://en.cppreference.com it has documentation on just about everything complete with usage examples. For example: http://en.cppreference.com/w/cpp/container/vector – user4581301 Feb 27 '18 at 05:49
  • @user4581301 alright thanks, looks quite useful – Krutik Shah Feb 27 '18 at 06:00
  • @KrutikShah *i looked up how to create a list and i went from there* -- Doing things this way is not a good method to learn C++ properly. Every good C++ book or tutorial introduces `std::vector` first. The `std::list` is basically second-tier or even third-tier. So the question is how you came across `std::list`, when it is basically a container that is not often used. Is it that you come from Java or another language where "List" means something, so you looked up `std::list`? If so, this is the danger of leveraging what you learned from one language and attempting to learn C++ this way. – PaulMcKenzie Feb 27 '18 at 13:26