pr1302.cpp

来自「《C++编程习题与解答》书中所有例题与习题的源代码」· C++ 代码 · 共 36 行

CPP
36
字号
//  Programming with C++, Second Edition, by John R. Hubbard
//  Copyright McGraw-Hill 2000
//  Problem 13.2, page 319
//  Search Function

#include<iostream>
using namespace std;
#include <string>

template <class T>
int search(T a[], T key, int first, int last)
{ 
  while ( first <= last )
    {
      int mid = (first+last)/2;
      if (key<a[mid]) last = mid-1;
      else if (key > a[mid]) first = mid+1;
      else return mid;
    }
  return -1;
}

string names[] = {"Adams", "Black", "Cohen", "Davis", "Evans", "Frost",
                  "Green", "Healy", "Irwin", "Jones", "Kelly", "Lewis"};

int main()
{
  string name;
  while (cin>>name)
  {
    int location = search(names, name, 0, 9);
    if (location == -1) cout << name << " is not in the list.\n";
    else cout << name << " is in position " << location << endl;
  }
}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?