其實,無需複雜的模板參數,只是簡單的
#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:
template<typename T, typename U>
void increase(T& query, U const& para)
{
for (unsigned int i=0; i < query.size(); i++)
{
if (query[i].first == para)
{
query[i].second += 1;
return;
}
}
}
void increase_freq_title (std::string & title)
{
increase(queries_title,title);
}
void increase_freq_author (std::string & author)
{
increase(queries_author,author);
}
void increase_freq_phrase (std::string & phrase)
{
increase(queries_phrase,phrase);
}
void increase_freq_id (int id_doc)
{
increase(queries_id,id_doc);
}
};
你爲什麼不建立一個功能,並添加參數,需要一個載體? – OGH
使用'template void increase_freq(std :: vector >&v,T const&k)',''''''就像'樣本代碼'在'queries_title'上執行操作。我會讓它成爲一個免費的功能。 –
Yakk