Getting all possible letter combinations of a word I wrote a simple algorithm for finding all possible letter combinations of a single word. I want to know if my code can be improved in any way, although I'm mostly interested in efficiency.
You can ignore my notes if you wish, they are mostly for myself and the questions in them are not the ones I ask answers for here^^

The code:

#include &lt;iostream&gt;
#include &lt;algorithm&gt;
#include &lt;vector&gt;
#include &lt;fstream&gt;
#include &lt;unordered_set&gt;

/**
    1. Used std::vector to store words, then sorted it and removed duplicates.
        &gt;4s for 10 letters.
    2. Moved to std::unordered_set. Time improved to &gt;2s.

    Notes:
        I used [i] instead of at(i) for efficiency in some cases (at(i) does bound checking
        (right?), and I do that myself, so it's unnecessary
        (or is the performance hit too small to matter?)).
**/

const char* outFile = "wordCombos.txt";

// Only this function should be used in main()
// Gets all possible letter combinations from a single word, prints them to a file
// Does not modify the original string
void unscramble(std::string str);

int main()
{
    // Opening the file to clear it
    std::ofstream out(outFile, std::ios::trunc);
    out.close();

    // Test values
    unscramble("aaa");
    unscramble("     Amazing \t\t\t\n  \n");
    unscramble("   \t\t\n  \n   \n \t\t");
    // This last one already takes 4 seconds. 10 letters
    // More than 3.5 million words
    unscramble("lakdjflakk");
    // 11 letters - 27 seconds
    //unscramble("lakdjlakkpp");
}

// Remove tabs, newlines, spaces
void trimSpaces(std::string&amp; a)
{
    if(a.find_first_not_of(" \n\t") == std::string::npos){
        a = "";
        return;
    }
    int begin = a.find_first_not_of(" \n\t");
    int length = a.find_last_not_of(" \n\t") - begin + 1;
    a = a.substr(begin, length);
}

// Process a string, add all possible letter combos to word
// Assumes that a valid letters vector is given (size &gt; 0)
// Uses recursion
void getLetterCombos(std::unordered_set&lt;std::string&gt;&amp; words,
                     const std::vector&lt;char&gt;&amp; letters,
                     std::string curWord)
{
    if(letters.size() == 1){
        words.insert(curWord + letters[0]);
        return;
    }

    for(int i = 0; i &lt; letters.size(); ++i){
        std::vector&lt;char&gt; temp = letters;
        std::string tWord = curWord + temp[i];
        temp.erase(temp.begin() + i);
        getLetterCombos(words, temp, tWord);
    }
}

void unscramble(std::string str)
{
    trimSpaces(str);

    if(str == "" || str.find_first_of(" \t\n") != std::string::npos){
        std::cout &lt;&lt; "Error: Invalid input: no word or more than a single word given.\n";
        return;
    }

    // All combos should be in uppercase
    std::transform(str.begin(), str.end(), str.begin(), toupper);

    std::unordered_set&lt;std::string&gt; words;
    std::vector&lt;char&gt; letters;
    letters.reserve(str.size());
    for(int i = 0; i &lt; str.size(); ++i){
        letters.push_back(str[i]);
    }
    getLetterCombos(words, letters, "");

    std::ofstream out(outFile, std::ios::app);
    out &lt;&lt; "Initial word: " &lt;&lt; str &lt;&lt; ",\nAll possible letter combinations: \n";
    for(std::unordered_set&lt;std::string&gt;::iterator it = words.begin(); it != words.end(); ++it){
        out &lt;&lt; *it &lt;&lt; "\n";
    }
    out &lt;&lt; "\n";
}
I see some things that may help you improve your code.

Consider separating input and output

Right now, the unscramble function does both the permutation and the printing of both outputs and errors. A more modular (and likely more maintainable) approach would separate these into separate functions.

Return something useful from functions

Every single one of the routines is declared as returning void.  Something is wrong there.  For example, the unscramble routine could return the number of permutations or 0 on error.

Throw errors rather than printing error messages

The user of your code may be creating a GUI with no command line available and will not appreciate having your code printing instead of indicating an error to the calling code.  The two ways that C++ programs generally signal an error are either by throwing an exception (if the circumstance really is exceptional) or by returning a value that indicates an error. 

Eliminate global variables

Eliminating global variables allows your code to be more readable and maintainable, both of which are important characteristics of well-written code.  Global variables introduce messy linkages that are difficult to spot and error prone.  Either the file name or better, a std::ostream reference, should be passed rather than having the filename be a global variable.  Better still, have subroutines simply operate by passing a string in or out and collect the I/O elsewhere.

Use standard algorithms

You might want to use std::next_permutation to accomplish this.  It would make the code much simpler, and you're already including the required header.