<分区>
有没有办法在保持顺序的同时从包含字符串元素的 vector 容器中删除重复元素。
到现在为止我一直使用set方法,但是它不保留顺序。
关于这个问题,我不知道如何使用 remove_if。
<分区>
有没有办法在保持顺序的同时从包含字符串元素的 vector 容器中删除重复元素。
到现在为止我一直使用set方法,但是它不保留顺序。
关于这个问题,我不知道如何使用 remove_if。
最佳答案
如何使用临时容器:
std::vector< int >::iterator i , j ;
std::set< int > t_set;
for( i = v.begin() , j = v.begin() ; i != v.end() ; ++i )
if( t_set.insert( *i ).second)
*j++ = *i ;
v.erase( j , v.end() );
使用 std::remove_if
,我可以想到这个:
std::set<int> t_set;
std::vector<int> res; //Resultant vector
remove_copy_if(v.begin(), v.end(), std::back_inserter(res),
[&t_set](int x){
return !t_set.insert(x).second;
} );
关于c++ - 在保持顺序的同时从 std::vector 中删除/删除重复元素的方法?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18033715/