<<< Another function example | Index | Vector data type >>> |
To do just about anything of interest, we need a collection of data to work on. We can store this collection in a vector. For example:
//#include "std_lib_facilities.h" // -or- #include <iostream> #include <vector> using namespace std; // read some temperatures into a vector: int main() { vector< double > temps; // declare a vector of type double to store // temperatures are values like 62.4 double temp; // a variable for a single temperature value while ( cin >> temp ) { // cin reads a value and stores it in temp // store the value of temp in the vector temps.push_back( temp ); } //... }
Note that cin >> temp returns true until we reach the end of file or encounter something that isn't a double: like the word "end".
<<< Another function example | Index | Vector data type >>> |