title: Permutations of letters and numbers in a phone number
body: <p>For my computer science class, we need to write a program (in C++) that takes an input of characters and outputs the possible permutations of it according to the dial pad on a phone, leaving non-digit characters in place.</p>
  
  <p>For example, inputing 2 outputs 2, A, B, C. Inputing 23 outputs 23, A3, B3, C3, 2D, 2E, 2F, AD, AE, AF, BD, BE, BF, etc...</p>
  
  <p>The application provided for this program is finding permutations of "vanity" phone numbers for a given phone number.</p>
  
  <p>Currently, the program I have written doesn't even compile, and I'm afraid the algorithm I'm using is incorrect:</p>
  
  <pre><code>#include &lt;iostream&gt;
  #include &lt;multimap.h&gt;
  #include &lt;vector&gt;
  using namespace std;
  
  // Prototypes
  void initLetterMap(multimap&lt;char,char&gt; &amp;lmap);
  void showPermutations(const vector&lt;string&gt; &amp;perms);
  vector&lt;string&gt; getPermutations(const string &amp;phoneNumber,const multimap&lt;char,char&gt; &amp;lmap);
  vector&lt;char&gt; getLetters(char digit, const multimap&lt;char,char&gt; &amp;lmap);
  
  
  // Declarations
  void initLetterMap(multimap&lt;char,char&gt; &amp;lmap) {
      lmap.insert(pair&lt;char,char&gt;('1','1'));
      lmap.insert(pair&lt;char,char&gt;('2','2'));
      lmap.insert(pair&lt;char,char&gt;('2','A'));
      lmap.insert(pair&lt;char,char&gt;('2','B'));
      lmap.insert(pair&lt;char,char&gt;('2','C'));
      lmap.insert(pair&lt;char,char&gt;('3','3'));
      lmap.insert(pair&lt;char,char&gt;('3','D'));
      lmap.insert(pair&lt;char,char&gt;('3','E'));
      lmap.insert(pair&lt;char,char&gt;('3','F'));
      // ...
  }
  
  vector&lt;char&gt; getLetters(char digit, const multimap&lt;char,char&gt; &amp;lmap) {
      multimap&lt;char,char&gt;::iterator it;
      pair&lt;multimap&lt;char,char&gt;::iterator,multimap&lt;char,char&gt;::iterator&gt; range;
      vector&lt;char&gt; result;
  
      if (isdigit(digit)) {
          range = lmap.equal_range(digit);
          for (it=range.first;it!=range.second;++it) {
              result.push_back((*it).second);
          }
      } else {
          result.insert(result.end(),digit);
      }
  
      return result;
  }
  
  void showPermutations(vector&lt;string&gt; &amp;perms) {
      vector&lt;string&gt;::iterator it;
      for (it = perms.begin(); it != perms.end(); it++) {
          cout &lt;&lt; *it &lt;&lt; endl;
      }
  }
  
  
  vector&lt;string&gt; getPermutations(const string &amp;phoneNumber,const multimap&lt;char,char&gt; &amp;lmap) {
      vector&lt;string&gt; results;
  
      string number = phoneNumber;
      vector&lt;char&gt;::iterator vcit;
      vector&lt;char&gt; letters;
      unsigned int i;
  
      for (i=0;i&lt;phoneNumber.length();i++) {
          letters = getLetters(number[i],lmap);
          for (vcit=letters.begin();vcit!=letters.end();vcit++) {
              number[i] = *vcit;
              results.push_back(number);
          }
      }
  
  
      return results;
  }
  
  int main() {
  
      multimap&lt;char,char&gt; lmap;
      initLetterMap(lmap);
  
      string input;
  
      cout &lt;&lt; "Enter a phone number to get all possible vanity numbers" &lt;&lt; endl;
      cout &lt;&lt; "&gt; "; getline(cin,input);
  
      showPermutations(getPermutations(input,lmap));
  
  
      return 0;
  }
  </code></pre>
  
  <p>I get a whole slew of build issues when I try to build this, and am not sure how to resolve most of them:</p>
  
  <pre><code>In file included from /usr/include/c++/4.0.0/backward/multimap.h:59,
  from phone02.cpp:18:
  /usr/include/c++/4.0.0/backward/backward_warning.h:32:2: warning: #warning This file includes at least one deprecated or antiquated header. Please consider using one of the 32 headers found in section 17.4.1.2 of the C++ standard. Examples include substituting the &lt;X&gt; header for the &lt;X.h&gt; header for C++ includes, or &lt;iostream&gt; instead of the deprecated header &lt;iostream.h&gt;. To disable this warning use -Wno-deprecated.
  /usr/include/c++/4.0.0/bits/stl_pair.h: In constructor 'std::pair&lt;_T1, _T2&gt;::pair(const std::pair&lt;_U1, _U2&gt;&amp;) [with _U1 = std::_Rb_tree_const_iterator&lt;std::pair&lt;const char, char&gt; &gt;, _U2 = std::_Rb_tree_const_iterator&lt;std::pair&lt;const char, char&gt; &gt;, _T1 = std::_Rb_tree_iterator&lt;std::pair&lt;const char, char&gt; &gt;, _T2 = std::_Rb_tree_iterator&lt;std::pair&lt;const char, char&gt; &gt;]':
  phone02.cpp:75: instantiated from here
  /usr/include/c++/4.0.0/bits/stl_pair.h:90: error: no matching function for call to 'std::_Rb_tree_iterator&lt;std::pair&lt;const char, char&gt; &gt;::_Rb_tree_iterator(const std::_Rb_tree_const_iterator&lt;std::pair&lt;const char, char&gt; &gt;&amp;)'
  /usr/include/c++/4.0.0/bits/stl_tree.h:167: note: candidates are: std::_Rb_tree_iterator&lt;_Tp&gt;::_Rb_tree_iterator(std::_Rb_tree_node&lt;_Tp&gt;*) [with _Tp = std::pair&lt;const char, char&gt;]
  /usr/include/c++/4.0.0/bits/stl_tree.h:164: note: std::_Rb_tree_iterator&lt;_Tp&gt;::_Rb_tree_iterator() [with _Tp = std::pair&lt;const char, char&gt;]
  /usr/include/c++/4.0.0/bits/stl_tree.h:152: note: std::_Rb_tree_iterator&lt;std::pair&lt;const char, char&gt; &gt;::_Rb_tree_iterator(const std::_Rb_tree_iterator&lt;std::pair&lt;const char, char&gt; &gt;&amp;)
  /usr/include/c++/4.0.0/bits/stl_pair.h:90: error: no matching function for call to 'std::_Rb_tree_iterator&lt;std::pair&lt;const char, char&gt; &gt;::_Rb_tree_iterator(const std::_Rb_tree_const_iterator&lt;std::pair&lt;const char, char&gt; &gt;&amp;)'
  /usr/include/c++/4.0.0/bits/stl_tree.h:167: note: candidates are: std::_Rb_tree_iterator&lt;_Tp&gt;::_Rb_tree_iterator(std::_Rb_tree_node&lt;_Tp&gt;*) [with _Tp = std::pair&lt;const char, char&gt;]
  /usr/include/c++/4.0.0/bits/stl_tree.h:164: note: std::_Rb_tree_iterator&lt;_Tp&gt;::_Rb_tree_iterator() [with _Tp = std::pair&lt;const char, char&gt;]
  /usr/include/c++/4.0.0/bits/stl_tree.h:152: note: std::_Rb_tree_iterator&lt;std::pair&lt;const char, char&gt; &gt;::_Rb_tree_iterator(const std::_Rb_tree_iterator&lt;std::pair&lt;const char, char&gt; &gt;&amp;)
  make: *** [phone02.o] Error 1
  </code></pre>
  
  <p>The line numbers are a bit off, but the important ones that I can see are the two about <code>no matching function for call to 'std::_Rb_tree_iterator&lt;std::pair&lt;const char, char&gt; &gt;::_Rb_tree_iterator(const std::_Rb_tree_const_iterator&lt;std::pair&lt;const char, char&gt; &gt;&amp;)'</code></p>
  
  <p>Besides the errors, I also believe I am heading in the wrong direction with my algorithm.</p>
  
  <p>So I have 2 questions here:</p>
  
  <ol>
  <li>Why am I getting these build errors, and how do I fix them?</li>
  <li>How would you suggest going about solving this problem? Am I on the right track or no?</li>
  </ol>
  
  <p>For question #2, <strong>I would prefer to not get solutions</strong>, just advice or pointers in the right direction.</p>
  
  <p>Thanks!</p>
  
  <p>PS: I am building this on Mac OS X 10.5.8 with gcc, using QtCreator 1.2.1</p>
  
  <p><strong>UPDATE:</strong></p>
  
  <p>I have successfully compiled a solution program. I will post the source code to those who are curious.</p>
  
  <pre><code>#include &lt;iostream&gt;
  #include &lt;map&gt;
  #include &lt;vector&gt;
  #include &lt;string&gt;
  
  using namespace std;
  
  void initLetterMap(map&lt;char,string&gt; &amp;lmap);
  vector&lt;string&gt; getMapped(const string &amp;phoneNumber, map&lt;char,string&gt; &amp;lmap);
  vector&lt;string&gt; getPermutations(vector&lt;string&gt; number);
  unsigned long int countPermutations(vector&lt;string&gt; number);
  
  void initLetterMap(map&lt;char,string&gt; &amp;lmap) {
      lmap['0'] = "0";
      lmap['1'] = "1";
      lmap['2'] = "2ABC";
      lmap['3'] = "3DEF";
      lmap['4'] = "4GHI";
      lmap['5'] = "5JKL";
      lmap['6'] = "6MNO";
      lmap['7'] = "7PQRS";
      lmap['8'] = "8TUV";
      lmap['9'] = "9WXYZ";
  }
  
  unsigned long int countPermutations(vector&lt;string&gt; number) {
      long int fold = 1;
      int vals = 0;
      vector&lt;string&gt;::iterator it;
      for (it=number.begin();it!=number.end();it++) {
          vals = (*it).length();
          fold *= vals;
      }
      return fold;
  }
  
  vector&lt;string&gt; getMapped(const string &amp;phoneNumber, map&lt;char,string&gt; &amp;lmap) {
      unsigned int i;
      vector&lt;string&gt; out;
      char digit;
      string temp;
      for (i=0;i&lt;phoneNumber.length();i++) {
          digit = phoneNumber.at(i);
          if (isdigit(digit)) {
              out.push_back(lmap[digit]);
          } else {
              temp = string(1,digit);
              out.push_back(temp);
          }
      }
      return out;
  }
  
  vector&lt;string&gt; getPermutations(vector&lt;string&gt; number) {
      vector&lt;string&gt; results;
  
      unsigned long int i,j,k;
      unsigned long int perms = countPermutations(number);
  
      vector&lt;string&gt;::reverse_iterator numit;
      string temp,temp2;
  
  
      vector&lt;int&gt; state = vector&lt;int&gt;(number.size(), 0);
      vector&lt;int&gt;::reverse_iterator stateit;
  
      for (i=0;i&lt;perms;i++) {
          j=i;
          temp = "";
          for (stateit=state.rbegin(), numit=number.rbegin();stateit!=state.rend();stateit++, numit++) {
              *stateit = j % (*numit).length();
              j /= (*numit).length();
              temp.insert(temp.begin(),(*numit)[*stateit]);
          }
          results.push_back(temp);
      }
  
  
      return results;
  }
  
  int main() {
      map&lt;char,string&gt; lettermap;
      initLetterMap(lettermap);
  
      string input;
  
      cout &lt;&lt; "&gt; "; getline(cin,input);
  
      vector&lt;string&gt; perms = getPermutations(getMapped(input,lettermap));
      vector&lt;string&gt;::iterator it;
      for (it=perms.begin();it!=perms.end();it++) {
          cout &lt;&lt; *it &lt;&lt; endl;
      }
  }
  </code></pre>
  
  <p>The code is probably more complicated than it has to be, but my goal was to just get it to work. It seems to run fairly quickly for 10 digit phone numbers, so I guess it's not too bad.</p>
  
  <p>Thanks to Jacob and ShreevatsaR for getting me pointed in the right direction!</p>