2

I am a C++ beginner, and I have been working on a project in which you have to input some integers separated by a space and the program will have to output all possible arrangements of the integers. I know that in python, this could be done using [int(item) for item in input().split()], but I don't know how to do the same in C++. I want to use a easy method built-in in C++. Can anyone provide some opinion? Any help will be appreciated.

Ian Chiang
  • 69
  • 5
  • 1
    An input stream and the `>>` operator will make very short work of the reading portion of this problem. Consult your C++ programming text. It should be covered int he first few chapters. – user4581301 Sep 17 '20 at 04:56
  • 1
    What do you mean by *"all possible arrangements of the integers"*? Can you at least show some input/output examples? – cigien Sep 17 '20 at 04:56
  • 1
    My suggestion: Read the input line by line. Use a `std::istringstream` to read `int`s from each line. – R Sahu Sep 17 '20 at 04:57
  • Try to limit questions to one problem you need solved. As a question sprawls into many problems it becomes much less likely to be answered and much less likely to be useful to future askers if it is answered. – user4581301 Sep 17 '20 at 04:58
  • 3
    [The Definitive C++ Book Guide and List](https://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list). You do not want to learn C++ without a good set of reference materials because your progress will be too slow. Without a good understanding of the fundamentals you will not be able to understand many of the answers on Stack Overflow and you will fall prey to the poor tutorials that infest the Internet. C++ is a very complicated language, among the most complicated in regular use by professional programmers, and filled with booby traps for the unwary. – user4581301 Sep 17 '20 at 05:01
  • Does this answer your question? [Using istringstream in C++](https://stackoverflow.com/questions/53491404/using-istringstream-in-c) – kesarling He-Him Sep 17 '20 at 05:06
  • For the input please learn about [`std::string`](https://en.cppreference.com/w/cpp/string/basic_string) and [`std::getline`](https://en.cppreference.com/w/cpp/string/basic_string/getline). – Some programmer dude Sep 17 '20 at 05:09
  • For separating the number please learn about [`std::istringstream`](https://en.cppreference.com/w/cpp/io/basic_istringstream), [`while` loops](https://en.cppreference.com/w/cpp/language/while), the `>>` input operator, and [`std::vector`](https://en.cppreference.com/w/cpp/container/vector). – Some programmer dude Sep 17 '20 at 05:11
  • 2
    Finally to get all *permutations* please learn about [`std::next_permutation`](https://en.cppreference.com/w/cpp/algorithm/next_permutation). – Some programmer dude Sep 17 '20 at 05:13
  • @IanChiang If you read into integer variables, you'll get integers. – Barmar Sep 17 '20 at 05:14
  • 1
    I know of no good websites for learning C++ fundamentals that match a good book. I do know many that will make you a worse programmer, mostly because people trying to learn from them invariably wind up here getting help turning the batsmurf fantasy code they've been learning into a functioning program. Take my word for it: You want at least one good book. It's worth the money. – user4581301 Sep 17 '20 at 06:15

1 Answers1

0

You see, you create a vector of integers from the string and then simply permute the vector:

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

int main() {
    std::string str;
    std::getline(std::cin, str);
    std::istringstream iss(str);
    std::vector<int> vec;
    int temp = 0;
    while (iss >> temp) {
        vec.push_back(temp);
    }
    //you now have a vector of integers
    std::sort(vec.begin(), vec.end()); //this is a must as `std::permutations()` stops when the container is lexicographically sorted
    do {
        std::copy(vec.begin(), vec.end(), std::ostream_iterator<int>{std::cout, " "});
        std::cout << "\n";
    } while (std::next_permutation(vec.begin(), vec.end()));
    return 0;
}

To know how to output all permutations of all possible lengths, take a look at
How to create a permutation in c++ using STL for number of places lower than the total length

kesarling He-Him
  • 1,944
  • 3
  • 14
  • 39
  • Comments are not for extended discussion; this conversation has been [moved to chat](https://chat.stackoverflow.com/rooms/221661/discussion-on-answer-by-d4rk4ng31-is-there-a-simple-way-for-splitting-one-line-o). – Samuel Liew Sep 18 '20 at 01:16