{[ promptMessage ]}

Bookmark it

{[ promptMessage ]}

STLVectorDemo

STLVectorDemo - cout<<"True"<< endl else...

Info iconThis preview shows pages 1–2. Sign up to view the full content.

View Full Document Right Arrow Icon
#include <iostream> #include <fstream> #include <cmath> #include <vector> using namespace std ; bool binarysearch(vector<int>& v, int val); int main () { vector<int> l; cout << "Enter some numbers: "; while(true) { int val; cin >> val; if(!cin) break; l.push_back(val); } sort(l.begin(), l.end()); cin.clear(); cin.ignore(132, '\n'); cout << "Enter a number to query: "; while(true) { int val; cin >> val; if(!cin) break; if(binarysearch(l, val)) {
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
Background image of page 2
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: cout << "True!" << endl; } else { cout << "False!" << endl; } } } bool binarysearch(vector<int>& v, int val) { int lower = 0; int upper = v.size() - 1; int middle = (lower + upper) / 2; while(lower < upper && v[middle] != val) { if(v[middle] < val) { lower = middle + 1; } else { upper = middle - 1; } middle = (lower + upper) / 2; } return v[middle] == val; }...
View Full Document

{[ snackBarMessage ]}

Page1 / 2

STLVectorDemo - cout<<"True"<< endl else...

This preview shows document pages 1 - 2. Sign up to view the full document.

View Full Document Right Arrow Icon bookmark
Ask a homework question - tutors are online