Returns all values within a range : vector indexer « vector « C++ Tutorial






#include <iostream>
#include <vector>

using namespace std;

vector<double> between(vector<double> v,
    double low, double high)
{  
   vector<double> result;
   for (int i = 0; i < v.size(); i++)
      if (low <= v[i] && v[i] <= high)
         result.push_back(v[i]);
   return result;
}

int main()
{  
   vector<double> salaries(5);
   salaries[0] = 35.0;
   salaries[1] = 63.0;
   salaries[2] = 48.0;
   salaries[3] = 78.0;
   salaries[4] = 51.0;

   vector<double> midrange_salaries = between(salaries, 45.0, 65.0);

   for (int i = 0; i < midrange_salaries.size(); i++)
      cout << midrange_salaries[i] << "\n";

   return 0;
}








16.17.vector indexer
16.17.1.Use indexer to update element in the vector
16.17.2.Use toupper function to convert all elements in a char vector to upper case
16.17.3.Returns all values within a range
16.17.4.Find element in a vector using a linear search