vector<pair> problem

Hi all ,i want to read from file vector of strings and write in another file the number of any word's frequency ,but before that i want to sort my vector;
here is my code
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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
#include<iostream>
#include<string>
#include <vector>
#include<fstream>
using namespace std;
struct pair1
{
	string st;
	int num;


};


vector<pair1> myfunc(vector<string>&);
ofstream& operator<< (ofstream&,vector<pair1>);
void sort(vector<pair1>&);

int main()
{
	ifstream input("in.txt");
	ofstream output("out.txt");
	vector<string>vs;
	string word;
	while(input>>word)
	{
		vs.push_back(word);
		
	}

	


		


	vector<pair1> vp=myfunc(vs);
	sort(vp);
	output<<vp;


	return 0;

}


vector<pair1> myfunc(vector<string>& myvec1)
{
	

	vector<pair1> mypair;
	for(int g=0;g<myvec1.size();g++)
	{
		pair1 p={myvec1[g],0};
		mypair.push_back(p);
	
	}
	
	for(int i=0;i<mypair.size();i++)
		for(int j=0;j<mypair.size();j++)
		{
			if(mypair[i].st==mypair[j].st)
				mypair[i].num++;



		}
return mypair;

};


ofstream& operator<< (ofstream& ofs,vector<pair1> myvec1)
{
	for(int i=0;i<myvec1.size();i++)
	{
		
		ofs<<myvec1[i].num<<"\t"<<myvec1[i].st<<'\n';
	}

	return ofs;

};



void sort(vector<pair1>& m)
{
	pair1 temp;
	temp.num=0;
	temp.st="";
for(int i=0;i<m.size()-1;i++)
for (int j=0;j<m.size()-1;j++)
{
if(m[j+1].num>=m[j].num)
{
temp=m[j+1];
m[j+1]=m[j];
m[j]=temp;
}


}





}


when i enter "a a a c c v f d s as fds "
it prints
3 a
3 a
3 a
2 c
2 c
1 as
1 fds
1 v
1 f
1 d
1 s


------

who can say whats my problem?
Well if its the multiple a's your getting the error is in my func. You never check your vector if the data is already inside one of the spots.

Also, I think you could write a better code with a std::map.

Here's what I think will fix the multiple count problem.
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
vector<pair1> myfunc(vector<string>& myvec1)
{
  vector<pair1> mypair;
  for(int i = 0; i < myvec1.size(); ++i)
  {
  
    pair1 temp = {myvec1[i], 1};
    // You're already adding a new item.

    // Before you pushback, shouldn't you check if there is a copy?
    bool found = false;
    for (int j = 0; j < mypair.size(); ++j)
      if (p == mypair[j])
      {
        ++(mypair[j].num);
        found = true;
      }
    if ( !found ) // If not found. Found is still false
      mypair.push_back(p); // add new item.
      
  } // END FOR LOOP

  // Just took care of this in above code.
  //	for(int i = 0; i < mypair.size();i++)
  //		for(int j=0;j<mypair.size();j++)
  //		{
  //			if(mypair[i].st==mypair[j].st)
  //				mypair[i].num++;
  //
  //
  //
  //		}

  return mypair;
}//; // You do not need semicolons at the end of functions.
// ONLY structs, protypes, and classes. 
Last edited on
OK ,thanks
Topic archived. No new replies allowed.