STLVectorDemo

STLVectorDemo - cout <<...

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 DocumentRight 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 &lt;&lt; &quot;True!&quot; &lt;&lt; endl; } else { cout &lt;&lt; &quot;False!&quot; &lt;&lt; endl; } } } bool binarysearch(vector&lt;int&gt;&amp; v, int val) { int lower = 0; int upper = v.size() - 1; int middle = (lower + upper) / 2; while(lower &lt; upper &amp;&amp; v[middle] != val) { if(v[middle] &lt; val) { lower = middle + 1; } else { upper = middle - 1; } middle = (lower + upper) / 2; } return v[middle] == val; }...
View Full Document

This note was uploaded on 10/28/2010 for the course CSCI 1113 taught by Professor Staff during the Spring '08 term at Minnesota.

Page1 / 2

STLVectorDemo - cout &amp;amp;lt;&amp;amp;lt;...

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

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