Go to the documentation of this file.00001 #include <iostream>
00002 #include <vector>
00003
00004
00005
00006
00007
00008
00009 int getIndex(const std::vector<double> &array, const double a)
00010 {
00011 int count;
00012 count = array.size();
00013 int step;
00014
00015 int i = 0;
00016 int i_smaller = 0;
00017 while (count > 0) {
00018 i = i_smaller;
00019 step = count / 2;
00020 i += step;
00021 if (!(a < array[i]))
00022 {
00023 i_smaller = ++i;
00024 count -= step + 1;
00025 }
00026 else
00027 {
00028 count = step;
00029 }
00030 }
00031 return i;
00032 }
00033
00034 int main(int argc, char** argv)
00035 {
00036 std::vector<double> v{0, 1, 2, 3, 4, 5};
00037
00038 double a = -1;
00039 std::cout << "With " << a << ": " << getIndex(v, a) << std::endl;
00040 a = 0;
00041 std::cout << "With " << a << ": " << getIndex(v, a) << std::endl;
00042 a = 0.5;
00043 std::cout << "With " << a << ": " << getIndex(v, a) << std::endl;
00044 a = 4.5;
00045 std::cout << "With " << a << ": " << getIndex(v, a) << std::endl;
00046 a = 5.0;
00047 std::cout << "With " << a << ": " << getIndex(v, a) << std::endl;
00048 a = 6;
00049 std::cout << "With " << a << ": " << getIndex(v, a) << std::endl;
00050 }