How to order a vector?

I have read in a file and it counts the frequencey of the lettters and grammar used as I wanted. But this waa using a map which I can't order and so I have tried to transfer it over to a vector to order it in order of which letters/grammar are most frequent. I am having a problem in ordering this and tried to use the function().

// SymbolFreq.cpp : Defines the entry point for the console application.
//

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
#include "stdafx.h"
#include <iostream>
#include <fstream>
#include <string>
#include <map>
#include <process.h>
#include <vector>
#include <algorithm>

using namespace std;

bool function(const pair<int, char>& i, const pair<int, char>& j)
{
  if (i.first < j.first) return false;
  if (j.first < i.first) return true;
  return j.second < i.second;
}

int main()
{
	map<char, int> letters;   
	map<char, int>::iterator i;
	string input;
	ifstream file;

	file.open("ToCompress.txt");

	if (file.fail())
	{
		printf("Error opening file");
		exit(1);
	}

	while (!file.eof())
	{
		getline(file, input);
		cout << input << endl;
	}

	for (auto c : input)
	{
		++letters[c];

	}

	vector<pair<int, char> > maptovector(letters.begin(), letters.end());

	sort(maptovector.begin(), maptovector.end(), function);


	for (i = letters.begin(); i != letters.end(); ++i)
	{
		cout << "\n" << i->first << " occurs " << i->second << " times \n ";
	}
	
	file.close();
	return 0;
}
Last edited on
I am having a problem in ordering this and tried to use the function().

Could you state what problem you are having. For example incorrect output, or program doesn't compile or something else?


I see you are still presenting the same erroneous code as before:
1
2
3
4
5
6
7
8
9
10
    while (!file.eof())
    {
        getline(file, input);
        cout << input << endl;
    }

    for (auto c : input)
    {
        ++letters[c];
    }


http://www.cplusplus.com/forum/general/207441/#msg979136

How many lines are in the file? Which of those lines do you want to process?
The code runs fine and prints all of the letters and frequencies but they are unordered. So I have tried to convert the map into a vector to order it. I think the method I am using could work its just Im unsure how to code the function() correctly.

There is only one line in the file.

Last edited on
Why do you need this part? If you are ordering them by frequency (i.e the value, not the key), those won't help you at all, they will hinder you from sorting by values every-time, only exception being when they are equal (that means never because you got unique keys)

1
2
 if (i.first < j.first) return false;
  if (j.first < i.first) return true;


I think you want something like this:
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
// Example program
#include <iostream>
#include <map>
#include <algorithm>
#include <vector>

int main()
{
  std::map<char, int> brt;
  brt['a']=10;
  brt['b']=30;
  brt['d']=6;
  brt['c']=0;
  
  std::vector<std::pair<char,int>> vec;
  
  for(auto & i: brt)
     vec.push_back(i);
     
   
   std::sort(vec.begin(),vec.end(), [](std::pair<char,int> a,
                                       std::pair<char,int> b)
                                       {
                                          return a.second<b.second;    
                                       });
   for(auto & i: vec)
      std::cout<<i.first<<char(0x20)<<i.second<<'\n';
  
  return EXIT_SUCCESS;
}


Output:
c 0
d 6
a 10
b 30
Ok I see what you mean by your code but my data is being read in as opposed to being hardcoded. How would alter it for that ?
I just initialized those as an example, the logic is the same for data files too. Just read the file buffer and insert it into a map.
One other problem in the original code is here:
1
2
3
4
5
6
    map<char, int>::iterator i;

    for (i = letters.begin(); i != letters.end(); ++i)
    {
        cout << "\n" << i->first << " occurs " << i->second << " times \n ";
    }

It should be:
1
2
3
4
5
6
    vector<pair<char, int> >::iterator i;

    for (i = maptovector.begin(); i != maptovector.end(); ++i)
    {
        cout << "\n" << i->first << " occurs " << i->second << " times \n ";
    }
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
#include <iostream>
#include <fstream>
#include <string>
#include <map>
#include <vector>
#include <algorithm>

using namespace std;

bool function(const pair<char, int> & i, const pair<char, int> & j)
{

    if (i.second > j.second)
        return true;
    if (i.second == j.second)
        if (i.first < j.first) 
            return true;         
    return false;            
}

int main()
{
    map<char, int> letters;

    string input;
    ifstream file("ToCompress.txt");

    if (!file)
    {
        cout << "Error opening file\n";
        return 1;
    }

    while (getline(file, input))
    {
        cout << input << endl;
        for (auto c : input)
        {
            ++letters[c];
        }        
    }


    vector<pair<char, int> > maptovector(letters.begin(), letters.end());

    sort(maptovector.begin(), maptovector.end(), ::function);

    vector<pair<char, int> >::iterator i;
    for (i = maptovector.begin(); i != maptovector.end(); ++i)
    {
        cout << "\n" << i->first << " occurs " << i->second << " times \n ";
    }

} 

I am trying to order code (from Chervil) into classes but I am having trouble successfully doing it and was hoping somebody would be able to point me in the right direction?
sorry for jumping in late:
But this waa using a map which I can't order

if you're using ascii you can continue to use the set with a custom compare based on the ascii value of the char's
Topic archived. No new replies allowed.