![]() |
|
![]() |
|
|
Thread Tools | Display Modes |
|
|
#1 |
|
Professional Programmer
Join Date: Sep 2005
Location: serbia & montenegro
Posts: 484
Rep Power: 3
![]() |
Dynamic array
Hello,
I need to build some kind of dynamic array for my project but I am not very good at memory menagment. It should become bigger everytime I add an object and realocate more memory. Can you recommend any tips, tutorials or something? |
|
|
|
|
|
#2 |
|
Programming Guru
![]() Join Date: Jun 2005
Location: Adelaide, South Australia
Posts: 1,207
Rep Power: 5
![]() |
Look up the standard vector class, which is accessed by #include <vector>.
A sample usage is; #include <vector>
#include <iostream>
// shorthand for an iterator that allows us to work with elements of
// a vector<int> without changing that vector
typedef std::vector<int>::const_iterator IVEC;
int main()
{
std::vector<int> ivec(0); // initial size of vector is zero
ivec.push_back(5);
ivec.push_back(6);
// output the elements
for (int i = 0, size = ivec.size(); i < size; ++i)
std::cout << ' ' << ivec[i];
std::cout << '\n';
ivec.resize(1); // resize vector to 1 elements (incidentally discard last one)
ivec.push_back(5); // vector size will now be 2
ivec.push_back(6); // vector size will now be 3
// another way to output it
for (IVEC i = ivec.begin(), end = ivec.end(); i != end; ++i)
std::cout << ' ' << *i;
std::cout << '\n';
return 0;
} |
|
|
|
|
|
#3 |
|
Programming Guru
![]() Join Date: Jun 2005
Location: elemental plane
Posts: 1,429
Rep Power: 5
![]() |
Vectors are excellent. A list of functions which are available for the standard vector class.
__________________
"Employ your time in improving yourself by other men's writings, so that you shall gain easily what others have labored hard for." -- Socrates |
|
|
|
|
|
#4 |
|
Programming Guru
![]() ![]() |
Indeed, vectors are great!
__________________
Profanity is the one language that all programmers understand. Check out my Blog <---updated Nov 30 2007! |
|
|
|
|
|
#5 |
|
Professional Programmer
Join Date: Jan 2006
Location: Ontario, Canada
Posts: 372
Rep Power: 0
![]() |
lists would also work, but i think vectors would be better in this situation.
__________________
I am Addicted to Linux! |
|
|
|
|
|
#6 |
|
Programming Guru
![]() Join Date: Jun 2005
Location: elemental plane
Posts: 1,429
Rep Power: 5
![]() |
Double-ended Queues work too, but I'd stick to vectors.
__________________
"Employ your time in improving yourself by other men's writings, so that you shall gain easily what others have labored hard for." -- Socrates |
|
|
|
|
|
#7 |
|
Professional Programmer
Join Date: Sep 2005
Location: serbia & montenegro
Posts: 484
Rep Power: 3
![]() |
Ok thank you I will take a look int vector class. Can vectors remove an element at a specific location? For example, if I have a vector with 100 ints, can I remove the 50th element?
|
|
|
|
|
|
#8 |
|
Game engine designer
Join Date: May 2005
Location: Sweden
Posts: 297
Rep Power: 4
![]() |
Yes you can. I don't remember how you do it right now though. It is a little bit tricky, but it is possible!
|
|
|
|
|
|
#9 |
|
Programming Guru
![]() Join Date: Jun 2005
Location: elemental plane
Posts: 1,429
Rep Power: 5
![]() |
Why didn't you take a look at the functions? There is one called erase for example.
__________________
"Employ your time in improving yourself by other men's writings, so that you shall gain easily what others have labored hard for." -- Socrates |
|
|
|
|
|
#10 |
|
Professional Programmer
Join Date: Sep 2005
Location: serbia & montenegro
Posts: 484
Rep Power: 3
![]() |
Yes I saw it, but in my c++ manual ( Thinking in c++ ) I have read that this is not a good idea and that I should only remove elements from the end.
|
|
|
|
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|