在unique_copy()中传入back_inserter为什么不行?
下面程序,我在unique_copy的第三个参数传入back_inserter迭代器为什么报错?
- C/C++ code
vector<string> svec; vector<string> svecResult; vector<string>::iterator svec_iter; svec.push_back("enough"); svec.push_back("is"); svec.push_back("enough"); svec.push_back("enough"); svec.push_back("is"); svec.push_back("good"); sort(svec.begin(),svec.end()); svec_iter=unique_copy(svec.begin(),svec.end(),back_inserter(svecResult)); for(vector<string>::iterator iter=svecResult.begin();iter!=svec_iter;++iter) cout<<*iter<<' ';
[解决办法]
back_inserter类型怎么能转化为vector<string>::iterator类型,不存在这种类型的转换
[解决办法]
unique_copy function template
template <class InputIterator, class OutputIterator>
OutputIterator unique_copy ( InputIterator first, InputIterator last,
OutputIterator result );
template <class InputIterator, class OutputIterator, class BinaryPredicate>
OutputIterator unique_copy ( InputIterator first, InputIterator last,
OutputIterator result, BinaryPredicate pred );
<algorithm>
Copy range removing duplicates
Copies the values of the elements in the range [first,last) to the range positions beginning at result, except for the duplicate consecutive elements, which are not copied
The elements not copies are those that compare equal to the element right preceding them (only the first element in each group of consecutive equal elements is copied).
The comparison between elements is performed by either applying the == comparison operator, or the template parameter comp (for the second version) between them.
The behavior of this function template is equivalent to:
template <class InputIterator, class OutputIterator>
OutputIterator unique_copy ( InputIterator first, InputIterator last,
OutputIterator result )
{
*result=*first;
while (++first != last)
{
if (!(*result == *first)) // or: if (!pred(*result,*first)) for the pred version
*(++result)=*first;
}
return ++result;
}
Parameters
first, last
Forward iterators to the initial and final positions of the sequence to be copied. The range used is [first,last), which contains all the elements between first and last, including the element pointed by first but not the element pointed by last.
result
Output iterator to the initial position of the range where the resulting range of values is stored.
pred
Binary predicate taking two elements as argument and returning the result of the comparison between them, with true (non-zero) meaning that they are to be considered equal, and false (zero) for not-equal. This can either be a pointer to a function or an object whose class overloads operator().
Return value
An output iterator pointing to the end of the copied range.
Example
// unique_copy example
#include <iostream>
#include <algorithm>
#include <vector>
using namespace std;
bool myfunction (int i, int j) {
return (i==j);
}
int main () {
int myints[] = {10,20,20,20,30,30,20,20,10};
vector<int> myvector (9); // 0 0 0 0 0 0 0 0 0
vector<int>::iterator it;
// using default comparison:
it=unique_copy (myints,myints+9,myvector.begin()); // 10 20 30 20 10 0 0 0 0
// ^
sort (myvector.begin(),it); // 10 10 20 20 30 0 0 0 0
// ^
// using predicate comparison:
it=unique_copy (myvector.begin(), it, myvector.begin(), myfunction);
// 10 20 30 20 30 0 0 0 0
// ^
myvector.resize( it - myvector.begin() ); // 10 20 30
// print out content:
cout << "myvector contains:";
for (it=myvector.begin(); it!=myvector.end(); ++it)
cout << " " << *it;
cout << endl;
return 0;
}
Output:
myvector contains: 10 20 30
Complexity
At most, performs as many assignments and comparisons or applications of pred as the number of elements in the range [first,last).