Computing the number of permutations of n consecutive numbers I'm just starting to learn C++ and have been experimenting with various functions. Here is one that computes the number of permutations of n consecutive numbers as well as all possible permutations provided n. I'd love to receive any form of tips and criticisms on improving this function.

#include &lt;string&gt;
#include &lt;iostream&gt;
#include &lt;vector&gt;
#include &lt;sstream&gt;
#include &lt;fstream&gt;

using namespace std;

string rotateRt(string input);
vector&lt;string&gt; permute(int num);

int main(){
    cout &lt;&lt; "n: ";
    int n;
    cin &gt;&gt; n;
    vector&lt;string&gt; permutations(permute(n));
    ofstream myfile;
    myfile.open("rosalind_perm.txt");
    myfile &lt;&lt; permutations.size() &lt;&lt; endl;
    for(int i = 0; i &lt; permutations.size(); i++){
        myfile &lt;&lt; permutations.at(i) &lt;&lt; endl;
    }   
    myfile.close();
}

/* Rotate input string right by 1 character */
string rotateRt(string input){
    int len = input.length();
    string a, b;
    if(len == 0){
        return "";
    }
    else if(len == 1){
        return input;
    }
    else{
        a = input.substr(0, len - 2);
        b = input.substr(len - 1) + " ";
        return b + a;
    }   
}

/* To compute permutations we compute the permutations of num - 1,
append num to the front of each permutation, and find every cyclic
rotation. */
vector&lt;string&gt; permute(int num){
    vector&lt;string&gt; permutations;
    ostringstream convert;
    if(num == 1){
        permutations.push_back("1");
    }
    else{
        vector&lt;string&gt; prevPermutations(permute(num - 1));
        int size = prevPermutations.size();
        string prefix, suffix, word;
        convert &lt;&lt; num;
        prefix = convert.str();
        convert.str("");
        for(int i = 0; i &lt; size; i++){
            suffix = prevPermutations.at(i);
            word = prefix +  " " + suffix;
            int length = word.length();
            for(int j = 0; j &lt; length / 2 + 1; j++){
                permutations.push_back(word);
                word = rotateRt(word);
            }
        }
    }
    return permutations;
}
In addition to Jamal's good points:

1: Consider sorting your #includes alphabetically. It's not a big issue, but I think it looks more tidy, and it's easier to see if something is already included or not.

2: Consider typedef vector&lt;string&gt; StringCollection (or some other meaningful name). This makes it easier to change the underlying data structure later, and makes the code look cleaner.

3: If you are using C++11, I'd write the for loop like this:

for (auto const&amp; number : permutations) {
    myfile &lt;&lt; number &lt;&lt; endl;
}


Since you are learning, I see no reason not to get into C++11 right away.

4: Tutorials and the like often use mySomething as identifiers. my is a bad identifier prefix. Instead, write what role it has or what it represents. outputFile is much better than myFile.

5: Initialize variables right away. The std::ofstream constructor takes a filename, so there is no need to first initialize and then call open().

6: Instantiate variables as late as possible. In your rotateRt() (which should be called rotateRight(), by the way), you don't have to create your strings unless the last else triggers. Change it to this:

else {
    string a = input.substr(0, len - 2);
    string b = input.substr(len - 1) + " ";
    return b + a;
}


The same applies to for example ostringstream convert.

When you instantiate a variable as late as possible, you avoid constructing it unless you really have to. "As late as possible" implies as close as possible to the relevant context, which makes the code easier to read.

7: Consider rewriting permute() to reduce nesting.

8: Your code would normally be better suited as a class. You don't have to think too much about this yet, since you just started with C++. It's a good exercise to refactor this to use classes later.

9: In the case of int len = input.length();, you should use std::size_t instead of int.

10: Prefer C++-style comments: // (Effective C++ explains why, so I won't repeat it here.)

11: Use more whitespace. Especially more vertical whitespace. Insert an empty line in logical places to make your code much more readable.

12: You don't modify or copy the string you pass to rotateRt, so it should be reference-to-const instead: rotateRt(string const&amp; input). (If you prefer, you can write const string&amp;, it's the same thing.)

13: Consider rewriting permute() so that it's not recursive. Recursive functions that create a couple of objects can quickly chew up a lot of memory, and are often not very efficient. In short, your permute() function can be improved a great deal.

14: Consider refactoring the file name out into a constant:

const string filename = "rosalind_perm.txt";


This way it's easier to find if you have to change it, and by using the constant you are guaranteed you will only have to change it in one location.