天天看點

C++ primer 第五版 練習9.4答案

#include<iostream>

#include<vector>

using std::cout;

using std::endl;

using std::cin;

using std::vector;

bool findvalue(vector<int>::iterator begin,vector<int>::iterator end,int input)

{

    while (begin!=end)

    {

        if (*begin == input)

            return true;

        begin++;

    }

    return false;

}

int main()

{

    vector<int> a;

    for (int i = 0; i < 5; i++)      

    {

        int temp;

        cin >> temp;

        a.push_back(temp);

    }

    for (auto &temp : a)

        cout << temp << " ";

    cout << endl;

    int input = 0;

    cin >> input;

    if (findvalue(a.begin(), a.end(), input))

        cout << "Find the input value successfully";

    else

        cout << "No value here";

    return 0;

}