Sorting alphabetically

I am not sure how to sort output in alphabetical order. It took me awhile to get the code to work how it should, reading from an input.txt and then outputting to an outout.txt. Basically the code is outputting all possible combinations of a string (ex: input is mn, output is mn nm), but when the .txt file has "abc", the last two line should be cab cba, but my code gives me cba cab, so I just need help finding a way to sort them alphabetically.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
  #include<iostream>
#include<string>
#include<vector>
#include<fstream>
#include "ArgumentManager.h"
 
using namespace std;
 
void permute(string a, int l, int r);
ofstream output;

vector<string> list;
 
int main(int argc, char* argv[])
{
  ArgumentManager am(argc, argv);
  ifstream input;
  
  string infileName = am.get("input");
  string outfileName = am.get("output");
  input.open(infileName);
  output.open(outfileName);
 
  string str;
  input >> str;
  if (str == ""){
    output << "no permutation";
  }
  else {
    int n = str.size();
    permute(str, 0, n - 1);
  }
  for (int i = 0;i<list.size();i++)
  {
    if (i == list.size()-1)
      output << list[i];
    else
      output << list[i] << endl;
  }
  return 1;
}

void permute(string a, int l, int r)
{
  if (l == r)
    list.push_back(a);
  else
  {
    for (int i = l; i <= r; i++)
    {
      char c = a[l];
      a[l] = a[i];
      a[i] = c;
      permute(a, l + 1, r);
      c = a[l];
      a[l] = a[i];
      a[i] = c;
    }
  }
}
 
    std::sort (list.begin(), list.end());


edit: fixed typo
Last edited on
where would I add that line?
Stick it between where you generate the list and where you print it.
(AbstractionAnon made a typo: it should be std::osrt(list.begin(), list.end());.)

However, don't do that.

(Unless, of course, your assignment specifically asks you to output the strings in alphabetical order.)


A few notes

output should not be a global object. Make it local just like input.

You never use the filenames outside of opening the files. You can make your code simpler and shorter with just:
13
14
15
16
17
18
int main(int argc, char* argv[])
{
  ArgumentManager am(argc, argv);
  ifstream input(am.get("input"));
  ofstream output(am.get("output"));


You should be returning zero from main. Non-zero values indicate that something went wrong.
1
2
  return 0;
}


If you are allowed, you can also get rid of that global list by making it a reference argument to permute. Then you can use it as a local variable as well (local to main). For now, though, don't worry about that stuff.

Also, people will give you grief about using namespace std. There are only a few places that is really ever reasonable. You should make it a habit now, early, to be clear about what namespace you are pulling things from:
1
2
3
4
  std::vector<std::string> list;
  std::ifstream input(am.get("input"));
  std::string str;
  std::sort(list.begin(), list.end());

Then you can get rid of the 'evil' using namespace std clause.

Nicely done on the permutation!

Hope this helps.
Registered users can post here. Sign in or register to post.