Textbook's original code
Last time we talked about the principle of generating permutation numbers. C++ Abstract Programming - Recursive Introduction (3) - Generating Permutation Number (1) So we're going to implement it. In the first article of this article, I specifically mentioned the foreach statement, because this article involves. So let's first look at the code in the book:
#include <iostream>
#include "set.h"
#include "simpio.h"
using namespace std;
/* Function prototypes */
Set<string> generatePermutations(string str);
/* Main program */
int main() {
string str = getLine("Enter a string: ");
cout << "The permutations of \"" << str << "\" are:" << endl;
foreach (string s in generatePermutations(str)) {
cout << " \"" << s << "\"" << endl;
}
return 0;
}
Set<string> generatePermutations(string str) {
Set<string> result;
if (str == "") {
result += "";
} else {
for (int i = 0; i < str.length(); i++) {
char ch = str[i];
string rest = str.substr(0, i) + str.substr(i + 1);
foreach (string s in generatePermutations(rest)) {
result += ch + s;
}
}
}
return result;
}
As I said before, we can't copy and paste the code directly in this book, because it contains too many library functions of their own school, such as "simpio.h", and then, like "simpio.h".
Set<string> result;
result += "";
There is no such operator in the set of STL. So what's the use of taking it out?
1. To verify the foreach statement in the previous article C++ Abstract Programming-Recursive Strategy (3) - Simple Implementation of foreach Statement
2. In this way, we can better understand how I rewrite programs that we can implement with standard C++ libraries.
If the above code runs correctly, that's the result:
Rewrite program
Let's use the last article. C++ Abstract Programming-Recursive Strategy (3) - Simple Implementation of foreach Statement The method mentioned in this paper is to rewrite the foreach statement and use the functions of our standard library to complete the program, which makes us look more familiar. I won't write the analysis. The code's comments are very detailed.
#include <iostream>
#include <set>
#include <string>
using namespace std;
/*Function prototype*/
set<string> generatePermutations(string str);
/*Principal function*/
int main(){
string line,elementOfPermutations;
set<string> permutations;//Store the resulting permutations here
set<string>::iterator it;
cout << "Enter a string ";
getline(cin, line);
cout << "The permutations of \"" << line << "\" are:" << endl;
/*The following steps correspond to the foreach statement*/
permutations = generatePermutations(line);
for (it = permutations.begin(); it != permutations.end(); it++){
elementOfPermutations = (*it); //Extract elements from containers one by one
cout << " \"" << elementOfPermutations << "\"" << endl;//output
}
return 0;
}
/*The type returned is the set < string > class, so you can call the method*/
set<string> generatePermutations(string str) {
set<string> result;
set<string> permutations;
set<string>:: iterator it;
string elementOfPermutations;
if (str == "") {
result.insert("");
} else { //Recursive call
for (int i = 0; i < str.length(); i++) {
char ch = str[i];//Elements that determine the beginning
/*To intercept other elements in addition to that element and reassemble them into a new string*/
string rest = str.substr(0, i) + str.substr(i + 1);
/*Call the function that generates the permutation number in the remaining elements and store it in permutations*/
permutations = generatePermutations(rest);
/*The number of permutations generated by traversal is then preceded by the opening element, which is loaded into the result for return.*/
for(it = permutations.begin(); it != permutations.end(); it++){
elementOfPermutations = (*it);
result.insert(ch + elementOfPermutations);
}
}
}
return result;
}
PS: When writing code, I felt that naming was really important. At that time, in order to debug, I randomly named it, but it was always confusing. Later, standard English came into being, which made it much better.
Let's take a look at the results.