Mega Code Archive

 
Categories / C++ / Vector
 

Returns the positions of all values within a range

#include <iostream>  #include <vector>  using namespace std;  vector<int> find_all_between(vector<double> v, double low, double high)  {     vector<int> pos;    for (int i = 0; i < v.size(); i++)     {        if (low <= v[i] && v[i] <= high)           pos.push_back(i);     }     return pos;  }  int main()  {     vector<double> salaries(5);     salaries[0] = 3.0;     salaries[1] = 6.0;     salaries[2] = 4.0;     salaries[3] = 7.0;     salaries[4] = 5.0;         vector<int> matches = find_all_between(salaries, 4.0, 6.0);         for (int j = 0; j < matches.size(); j++)        cout << salaries[matches[j]] << "\n";     return 0;  }