我試圖在模板上應用stl算法remove_if,並遇到了一些麻煩。任何幫助表示讚賞!C++ STL算法remove_if與模板一起使用
template <class T> bool flag_delete(pair<T,int> a) {return (a.second == 1);}
template <class T> void fun_delete_by_flag(vector<T> &vec_data, ivec &vec_flag)
{
int n = vec_data.size();
vector< pair<T,int> > vec;
vec.resize(n);
for (int i = 0; i < n; i += 1) {
vec[i].first = vec_data[i];
vec[i].second = vec_flag[i];
}
typename vector< pair<T,int> >::iterator it;
it = remove_if(vec.begin(), vec.end(), flag_delete);
n = vec.size();
vec_data.resize(n);
for (int i = 0; i < n; i += 1) {
vec_data[i] = vec[i].first;
}
return;
}
我得到了以下信息:
guess_algo.h: In function ‘void fun_delete_by_flag(std::vector<T>&, ivec&) [with T = std::pair<int, std::basic_string<char> >, ivec = std::vector<int>]’:
user_time.h:63:34: instantiated from here
guess_algo.h:61:2: error: no matching function for call to ‘remove_if(std::vector<std::pair<std::pair<int, std::basic_string<char> >, int>, std::allocator<std::pair<std::pair<int, std::basic_string<char> >, int> > >::iterator, std::vector<std::pair<std::pair<int, std::basic_string<char> >, int>, std::allocator<std::pair<std::pair<int, std::basic_string<char> >, int> > >::iterator, <unresolved overloaded function type>)’