关于remove_if

来源:互联网 发布:运用思维导图学编程 编辑:程序博客网 时间:2024/05/01 03:02

今天试验了一下remove_if,发现只是把符合条件的元素移到前面,而容器的长度是没有变化的

//输出容器中元素

template <class T>
void printout(const vector<T>& v)
{
 
  vector<T>::const_iterator it = v.begin();
  while (it != v.end())
   cout << *it++ << endl;
 
}

//比较谓词

bool compare (double x)
{
 return x == 0;
}
int main()
{

double x;
 vector<double> v;
 while (cin >> x)
  v.push_back(x);
 remove_if(v.begin(), v.end(), compare);
 printout(v);
 cout << v.size() << endl;
 return 0;
}

另外使用remove_copy只是把不符合条件的移到另一个容器,对本身的容器是没有影响的

原创粉丝点击