Sorting 2D Vector

I am trying to use the std::sort included in algorithm library but keep getting errors...

WBMovies.h
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
#ifndef WBMOVIES_H
#define WBMOVIES_H
#include <iostream>
#include <fstream>
#include <string>
#include <vector>
#include <algorithm>
#include <stdlib.h>
#include <iomanip>
using namespace std; 

class WBMovies {
public:
	WBMovies();
	void Read_In(string);
	void Print_Me(vector< vector<string> >);
	void Print_All();
	void Sort_Me(vector< vector<string> >);
	int compare(vector<string>, vector<string>);
private:
	string line;
	vector< vector<string> > WB_Movie;
	vector<string> Movies;
	vector< vector<string> > WB_Date;
	vector<string> Dates;
	vector< vector<string> > WB_Star;
	vector<string> Stars;
};
WBMovies::WBMovies() {
}
void WBMovies::Read_In(string filename) {
	fstream File; 
	File.open(filename);
	if(!File.good()) {
		cout << "Error! " << filename << "Is not found or is corrupt";
		cout << endl;
	}
	while(!File.eof()) {
		getline(File,line);
		Movies.push_back(line);		
		getline(File,line);
		Dates.push_back(line);		
		line.clear();
		if(line.empty()) {
			do {
				getline(File,line);
				Stars.push_back(line);
				
			} while(!line.empty());
		}
}
	WB_Movie.push_back(vector<string>(Movies));
	WB_Date.push_back(vector<string>(Dates));
	WB_Star.push_back(vector<string>(Stars));
	File.close();
}
void WBMovies::Print_Me(vector< vector<string> > myvector) { 
	for(vector< vector<string> >::const_iterator it_row = myvector.begin();
        it_row != myvector.end(); ++it_row ) {
        copy(it_row->begin(), it_row->end(), ostream_iterator<string>(cout, "\n"));
        cout << endl;
    }
} 
void WBMovies::Print_All() {
	WBMovies::Sort_Me(WB_Movie);
	WBMovies::Print_Me(WB_Movie); 
}
int compare(vector<string>& s1, vector<string>& s2) {
    return s1[0] < s2[0];
}
void WBMovies::Sort_Me(vector< vector<string> > myvector) {
	sort(WB_Movie.begin(),WB_Movie.end(),compare); 
}

#endif  


WBMovies.cpp

1
2
3
4
5
6
7
8
9
#include "WBMovies.h"

int main() {
	WBMovies New;
	New.Read_In("WBMovies.txt");
	New.Print_All();
	return 0;

}


error: error C3867: 'WBMovies::compare': function call missing argument list; use '&WBMovies::compare' to create a pointer to member
error: error C2780: 'void std::sort(_RanIt,_RanIt)' : expects 2 arguments - 3 provided
Last edited on
You may not specify such a way a member function.

sort(WB_Movie.begin(),WB_Movie.end(),compare);

A non-static member function requires that would be an object that will call the function.

You should declare function compare as a static member function.
Last edited on
I did as you said and changed compare to static member. also i relaized that I did not have the correct syntax for my compare declarration:

1
2
3
int WBMovies::compare(vector<string>& s1, vector<string>& s2) {
    return s1[0] < s2[0];
}


static int compare(vector<string>& s1, vector<string>& s2);

after making the above changes the program compiles and runs without errors but does not sort
Last edited on
It is not important that it does not sort. It is more important that it compiles and runs without errors.:)

As I see

void WBMovies::Sort_Me(vector< vector<string> > myvector) {
sort(WB_Movie.begin(),WB_Movie.end(),compare);
}


your function sorts vector WB_Movie not the vector myvector that is declared as the parameter. So what are you going to sort?
Last edited on
Great point I forgot i changed the sort function to WB_Movie. I switched it to the parameter "myvector," and then called the Sort_Me function in the Print_All function, with WB_Movie vector as the object to sort.

1
2
3
void WBMovies::Sort_Me(vector< vector<string> > myvector) {
	sort(myvector.begin(),myvector.end(),compare); 
}


 
WBMovies::Sort_Me(WB_Movie);
I only do not understand why did you declare the parameter when you are going to sort the member of the class which is acceptable directly? That is you could declare and define the member function as

1
2
3
void WBMovies::Sort_Me() {
	sort(WB_Movie.begin(),WB_Movie.end(),compare); 
}

Last edited on
I see what you're saying, I made it pass the "myvector" parameter so it would be easier to pass the remaining vectors through that instead of having to type the sort statement out for each one. But I see why you would question that. Do you have any suggestions for my sort issue?? I appreciate your help.
Well if you have several members of type std::vector<T> in your class then indeed the function we are speaking about shall use the parameter.
Topic archived. No new replies allowed.