Find over an Unsorted vector
I need to apply a FIND functionality over vector elements (similar to the
MATLAB FIND command) returning all ocurrences. While being unable to find
this using the STL functions with iterators, i concocted this function:
vector<int> find(vector<int> v, int value,vector<int>*ive)
{
//Generic Find
vector<int> ve;
for (int i=0;i<v.size();i++)
{
if (v[i]==value)
{
ive->push_back(i);
ve.push_back(v[i]);}
}
return ve;
}
called with:
//Values
vector<int> v1 = {1,3,3,4,5,2,3,4,6,7,7,8,1,2,2,3,2,2,3,2};
vector<int> iRange,vRange;
int val=2;
//Manual FIND
vRange=find(v1,val,&iRange);
PrintArray(vRange);
PrintArray(iRange);
Returning the correct result:
*vRange: 2 2 2 2 2 2
iRange: 5 13 14 16 17 19*
WHich of course do not use the pair object, the sort and equal_range
function, which would be the ideal:
pair<vector<int>::iterator,vector<int>::iterator> Range;
sort(v1.begin(),v1.end());
Range=equal_range(v1.begin(),v1.end(),val);
Returning the absolutely proper, but -so far- totally useless result, if
one wishes the vector unsorded:
*Range Iters: 2 2 2 2 2 2
Range: 2 3 4 5 6 7*
What is the solution for this?
Thanks, hyp
No comments:
Post a Comment