December 5th, 2013, 04:42 PM
How to dynamically store strings in a variable
I am getting results from some s/w as a list of strings. The total number of these strings are in millions. I am checking each of these string and if some conditions are met, I want to store that string in a variable ('listOfStrings'). The total number of strings will be in couple of hundreds, sometimes more or less. There is no way I can guess this number.
I was wondering, is there a way read the above strings one at a time and if the conditions are met, copy into 'listOfStrings'? I do not want to declare total number of elements in the begining. It can be assumed to be a growing list of strings.
December 5th, 2013, 05:05 PM
I'd go with (aside from choosing a programming language from a different branch of the programming language tree) C++. Use the vector template from the standard template library.
http://www.sgi.com/tech/stl/Vector.html and the internet is loaded with stl examples.
[/code] are essential for python code and Makefiles!
December 5th, 2013, 05:21 PM
I agree. Though if you need to stick with C, then look into realloc(). You can Google on man page realloc for the documentation.
December 6th, 2013, 02:46 PM
A naive implementation might thrash the memory manager a lot and be very slow (same issue with STL <vector>, although the implementation of <vector> uses methods to avoid that as far as possible). If the stored data is subsequently assessed sequentially rather than direct access, a std::list or linked list might be preferable.
Originally Posted by dwise1_aol
Either way, the C++ STL will likely have a suitable container. To help you decide which is appropriate this page describes the properties of various container classes.