我有这门课:
#include <string>
#include <vector>
class QueryStatistics
{
private:
std::vector < std::pair <std::string,int >> queries_title;
std::vector < std::pair <std::string,int >> queries_author;
std::vector < std::pair <std::string,int >> queries_phrase;
std::vector < std::pair <int,int >> queries_id;
public:
QueryStatistics();
virtual ~QueryStatistics();
void increase_freq_title (std::string & title);
void increase_freq_author (std::string & author);
void increase_freq_phrase (std::string & phrase);
void increase_freq_id (int id_doc);
};
这段代码重复了 4 次,但每次都使用不同的向量。
void QueryStatistics::increase_freq_title (std::string & title)
{
for (unsigned int i=0; i < queries_title.size(); i++)
{
if (queries_title[i].first == title)
{
queries_title[i].second += 1;
return;
}
}
}
我怎样才能重构它以避免重复相同的代码四次?