how to sort it using STL

C

CFAN

I have a program,first it will read text from a text line by line and
then caculate the rank of each line. given word "good" it will give
the rank 50,
and "very good" rank 80.
and the declaration is something like the following,

class XXX{

std::vector<string> word_lists;
}
the word_lists contains the text in each line,and also i have defined
such type to contain the result of parsing
std::vector< <std::pair<int,int> > word_list_rank_table;
the first element in the pair indicates the subscript of the string in
the word_list,the second element indicates
the rank caculated,
and I need to sort the word_list_rank_table by the rank

the first question is does the std::vector< <std::pair<int,int> >
word_list_rank_table; suitable for this purpose
and the second question is how to sort it using STL algorithmn?

thanks
 
B

Barry

CFAN said:
I have a program,first it will read text from a text line by line and
then caculate the rank of each line. given word "good" it will give
the rank 50,
and "very good" rank 80.
and the declaration is something like the following,

class XXX{

std::vector<string> word_lists;
}
the word_lists contains the text in each line,and also i have defined
such type to contain the result of parsing
std::vector< <std::pair<int,int> > word_list_rank_table;
the first element in the pair indicates the subscript of the string in
the word_list,the second element indicates
the rank caculated,
and I need to sort the word_list_rank_table by the rank

the first question is does the std::vector< <std::pair<int,int> >
word_list_rank_table; suitable for this purpose
and the second question is how to sort it using STL algorithmn?

Why not
std::vector<std::pair<std::string, int> > word_rank;

step 1: read each line
step 2: calculate the rank for each line
step 3: sort word_rank by the second element of each pair<string, int>

struct Compare {
bool operator() (pair<string, int> const& lhs,
pair<string, int> const& rhs) const {
return lhs.second > rhs.second;
}
};

HTH.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,261
Messages
2,571,040
Members
48,769
Latest member
Clifft

Latest Threads

Top