[英]back_insert_iterator with remove_copy_if
我正在嘗試使用向量將back_insert_iterator
與remove_copy_if
使用,但是我有編譯錯誤。
你知道為什么下面的代碼是錯誤的嗎?
#include <iostream>
#include <string>
#include <algorithm>
#include <cassert>
#include <vector>
#include <iterator>
#include <functional>
struct checkRem : std::unary_function<int,bool> {
int _val;
checkRem( int val ): _val(val){}
bool operator()(int aVal){ return aVal > _val;}
};
int main( int argc, char** argv )
{
int _vIn[] = {1,2,3,4,2,3,4,3,6,7};
std::vector< int > vIn( _vIn, _vIn + (sizeof( _vIn )/sizeof(_vIn[0])));
// remove with copy
std::vector<int>vOut;
std::back_insert_iterator< std::vector<int> > bit( vOut );
std::vector< int >::iterator new_end =
std::remove_copy_if(
vIn.begin(),
vIn.end(),
bit,
checkRem(2)
);
}
back_insrt_iter.cpp: In function ‘int main(int, char**)’:
back_insrt_iter.cpp:30: error: conversion from
‘std::back_insert_iterator<std::vector<int, std::allocator<int> > >’ to non-scalar
type ‘__gnu_cxx::__normal_iterator<int*, std::vector<int, std::allocator<int> > >’
requested
std::remove_copy_if()
返回與輸出迭代器類型相同的迭代器。 在這種情況下,它是一個std::back_inserter_iterator
。 您無需更改輸入容器,但會將謂詞不包含的元素復制到輸出容器中。
簡而言之,如果要更改輸入容器,請使用std::remove_if()
。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.