Dynamic array class template




















Adding an item to the top of the stack, or pushing it, requires us to track the current top of the stack. Our push method can then be implemented as follows. If the stack size is equal to the storage capacity, double the capacity.

Then add the new item and increment the item count. The pop method can be implemented in similar fashion. Get the item on the top of the stack and decrement the item count. If the new item count is greater than zero and equal to one-quarter of the storage capacity, halve the capacity.

This ensures that storage is never less than one-quarter full. Finally, return the popped item. Size information is easy to gather using the members we have already declared.

One frequent operation on collections is to iterate over each item in the collection. The private Iterator class should be defined within Stack as follows. Implementing an iterator can be a hairy task and it is beyond the scope of this post but maybe worth exploring in another one. The format of the. I want to fill a dynamic array with Book class objects as I read from the.

Because STL is not allowed for this project, I needed to write my own implementation. The processing of the file went alright, I was able to display the author and book of each entry on separate lines using cout.

Now I am stuck trying to fill my dynamic array elements with the entries. My class to hold the data is Book and contains a struct of data members. I am suspicious that the problem is with my function in my dynamic array template implementation. Right now my code appears to fill the array with the proper number of entries, but all but the last element are fill with what looks like Book objects from the default constructor.

Stack Overflow for Teams — Collaborate and share knowledge with a private group. Create a free Team What is Teams? Collectives on Stack Overflow. Learn more. And parameter count we will use to define number of elements at runtime. Less code and more readable as we planned! GetDataPtr returns pointer to actual data of the BaseT class and you can use that pointer in memcpy functions with result of GetByteSize function. And finally, GetArraySize returns count of elements in dynamic array.

I have to notice that there is one known restriction — we cannot construct BaseStruct with non-empty parameters list constructor. We've already seen global function ::operator new in our operator new.

But there we used "most common" form of that function. There is another one. And we will use it to invoke constructors for array members. New constructor implementation will look like that:. It's just construct DataT elements one by one on already allocated memory block. In destructor we have to call destructor for all DataT elements:.

It contains additionaly copy constructor and some primitive error processing in the code. We strongly recommend you sponsor your listings. Sponsoring one listing now costs only USD 29 per month.

If you want to make listing to your favorites, then you need to login into hostscripts. Click on login and proceed. Rated 2.



0コメント

  • 1000 / 1000