Watch out, I'm about to go nuts...
Code:
#include <fstream>
#include <iostream>
#include <iterator>
#include <algorithm>
#include <list>
#include <vector>
#include <cstdlib>
#include <ctime>
int main()
{
// Seed random number generator
srand((unsigned)time(NULL));
// Generate 10 random numbers, stuff into a vector container.
std::vector<int> intVect;
std::generate_n(std::back_inserter(intVect),10, rand);
// Open output file and write the 10 random numbers stored in vector container to the file
std::ofstream output("file.txt");
std::copy(intVect.begin(),intVect.end(),
std::ostream_iterator<int>(output," "));
// Close the output file so we can open it below
output.close();
// Open an input file and create a list container
std::ifstream input("file.txt");
std::list<int> intList;
// Read random ints from file into list container
std::copy(std::istream_iterator<int>(input),std::istream_iterator<int>(),
std::back_inserter(intList));
// Output contents of vector to cout
std::cout << "Vector: ";
std::copy(intVect.begin(),intVect.end(),
std::ostream_iterator<int>(std::cout," "));
// Output contents of list to cout
std::cout << "\nList : ";
std::copy(intList.begin(),intList.end(),
std::ostream_iterator<int>(std::cout," "));
std::cout << std::endl;
}
Sample output:
Code:
Vector: 1646 15613 7177 3498 1960 31613 20293 2054 15271 22660
List : 1646 15613 7177 3498 1960 31613 20293 2054 15271 22660