Thursday, July 30, 2009

Large amounts of data in c++?

If I don't know how much data the user will enter, what data structure should be used? For example, if a program finds the mean of the numbers the user enters, but does not know how many numbers the user will enter, it can't use an array. The best solution I can come up with is to declare an array, and when it isn't big enough, declare another and transfer the data between them. I have heard about vectors, and I think I should use those.





I have only programmed in c++ for six months or so, and I have constantly been plagued with this problem. What should I do?





Thanks!

Large amounts of data in c++?
If you have no problem using STL (Standard Template Library), that's exactly what vectors do. Check out this page of commands with vectors:





http://www.fredosaurus.com/notes-cpp/stl...





So yours would be something like...





#include %26lt;vector%26gt;


...


int input;


vector%26lt;int%26gt; v;


cin %26gt;%26gt; input;


v.push_back(input);


...


No comments:

Post a Comment