C++ plz read the question carefully, use the main function provided in the follo
ID: 3720750 • Letter: C
Question
C++ plz
read the question carefully, use the main function provided in the following image do the extra credit part that is highlighted in the PDF, I will rate ur answer, ty!
Description: This assignment is to write a program that reads a set of strings from the standard input (keyboard), generates all permutations of that the strings, and then display the results to the standard output (screen) You may use any of the standard library types (e.g., the containers that we learned in the class) in your implementation, including library functions from the library that generate permutations. You will need to study functions related to permutation in the library by yourself if you decide to do so. Or you may write code to generate permutations on your own. Be sure to include the reference(s) that you learn the library or how to_generate ermutations A permutation is defined as an ordered arrangement of a set of objects. For example, the following set of three distinct strings would have six permutations: Strings: "ab" "c" "de" Permutations: 1. ab c de 2. ab de c 3. c ab de 4. c de ab 5. de abc 6. de c alb Here is some information about the algorithm library http://www.cplusplus.com/reference/algorithm/ You may use the following code as a starting point. You'll need to implement generate_permutations and print_permutations functionsExplanation / Answer
The code below will generate all unique permutations of the set of input strings, after removing duplicate values from them (extra credit part). Used different built in library functions to achieve this. Everything is explained clearly using comments. Thanks.
//code.cpp
#include<iostream>
#include<list>
#include<sstream>
#include <algorithm>
using namespace std;
//method to generate a list of possible permutations of given set of strings
list<string> generate_permutations(list<string> input){
list<string> permutations;//creating an empty list of string
input.sort();//sorting the input list
input.unique();//removing duplicates (extra credit part)
do{
//defining a string
string data="";
//defining a list iterator
list<string>::iterator it;
//appending all elements of the current list to the string
for (it = input.begin(); it != input.end(); ++it){
data+= it->c_str();
data+=" ";
}
//adding this string to the list of permutations
permutations.push_back(data);
}while(next_permutation(input.begin(),input.end()));//generating next permutation
return permutations;
}
//method to print permutations
void printPermutations(list<string> permutations){
//defining a list iterator
list<string>::iterator it;
int i=1;
//printing permutations one by one, including a numerical index
for (it = permutations.begin(); it != permutations.end(); ++it){
cout<<i<<") "<<it->c_str()<<endl;
i++;
}
}
int main(){
list<string> inputList;
string aLine,aString;
cout<<"Enter strings, seperated by a space: ";
getline(cin,aLine);
istringstream iss(aLine);
while(iss>>aString){
inputList.push_back(aString);
}
//generating and printing the permutations
auto permutations=generate_permutations(inputList);
printPermutations(permutations);
return 0;
}
//OUTPUT1
Enter strings, seperated by a space: hello my dear
1) dear hello my
2) dear my hello
3) hello dear my
4) hello my dear
5) my dear hello
6) my hello dear
//OUTPUT2
Enter strings, seperated by a space: 1 22 1
1) 1 22
2) 22 1
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.