Re: [Dev-C++] How to work with *.txt files??
Open Source C & C++ IDE for Windows
Brought to you by:
claplace
|
From: Per W. <pw...@ia...> - 2004-09-19 11:31:06
|
Well, don't blame yourself. The human life is meant for learning :) Logically, there isn't a difference between a vector and an array. Vector is the mathematical term for a one-dimensional array, i.e. an array with a single index. The important thing about the stl template class vector is that it doesn't grow by one every time you add one element, which would have resulted in O(n^2) complexity. A typical implementation doubles the length of the vector, whenever it needs to grow. This makes sure that the reallocation times - on average - may be ignored, and the inserts will then run in constant amortized time, i.e. the _average_ insert will consume constant time. If the vector is 1,000,000 elements long - and full - and you try to add one more entry, it grows to a length of 2,000,000 resulting in the next one million inserts not needing any reallocation. It really doesn't matter if a string assign if efficient or not. The important thing to avoid - when possible - is algorithsms with exponential growth, since the total run time will start to escalate even for extremely fast operations. /Per W On Sat, 18 Sep 2004, ZwiQ wrote: > Per Westermark wrote: > > > <>Ouch. > > > > Don't do that. > > > > There is the vector template class in the stl. > > (Hielke blames himself for not reading a good C++ book himself :o) > > Hm, you're right. Thanks! I said I would use a vector but in the code I > did use an simple array. Now I think of it, the code I wrote down there > without good thinking was ment to store an array of pointers to strings, > not strings themselves. Used it mainly when I had to reorganize the > order of the stored strings with manipulation members (eg. sort). > I assume that assigning pointers isn't as resourcesconsuming as > assigning strings? However, you're still right about the complexity. > Vector operations themselves all are lineair? So I should use a vector > in that case too as well? > > Regards, > Hielke > Zw...@he... |