Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Feb 14th, 2006, 5:22 AM   #1
ivan
Professional Programmer
 
ivan's Avatar
 
Join Date: Sep 2005
Location: serbia & montenegro
Posts: 484
Rep Power: 4 ivan is on a distinguished road
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?
ivan is offline   Reply With Quote
Old Feb 14th, 2006, 5:43 AM   #2
grumpy
Programming Guru
 
grumpy's Avatar
 
Join Date: Jun 2005
Location: Adelaide, South Australia
Posts: 1,261
Rep Power: 5 grumpy will become famous soon enough
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;
}
grumpy is offline   Reply With Quote
Old Feb 14th, 2006, 6:03 AM   #3
nnxion
Programming Guru
 
nnxion's Avatar
 
Join Date: Jun 2005
Location: elemental plane
Posts: 1,429
Rep Power: 5 nnxion is on a distinguished road
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
nnxion is offline   Reply With Quote
Old Feb 14th, 2006, 9:53 AM   #4
Pizentios
Programming Guru
 
Pizentios's Avatar
 
Join Date: May 2004
Location: Brandon, Manitoba, Canada
Posts: 2,023
Rep Power: 7 Pizentios is on a distinguished road
Send a message via ICQ to Pizentios Send a message via MSN to Pizentios
Indeed, vectors are great!
__________________
Profanity is the one language that all programmers understand.

Check out my Blog <---updated Nov 30 2007!
Pizentios is offline   Reply With Quote
Old Feb 14th, 2006, 10:15 AM   #5
King
Professional Programmer
 
King's Avatar
 
Join Date: Jan 2006
Location: Ontario, Canada
Posts: 380
Rep Power: 3 King is on a distinguished road
lists would also work, but i think vectors would be better in this situation.
__________________
I am Addicted to Linux!
King is offline   Reply With Quote
Old Feb 14th, 2006, 10:32 AM   #6
nnxion
Programming Guru
 
nnxion's Avatar
 
Join Date: Jun 2005
Location: elemental plane
Posts: 1,429
Rep Power: 5 nnxion is on a distinguished road
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
nnxion is offline   Reply With Quote
Old Feb 14th, 2006, 1:26 PM   #7
ivan
Professional Programmer
 
ivan's Avatar
 
Join Date: Sep 2005
Location: serbia & montenegro
Posts: 484
Rep Power: 4 ivan is on a distinguished road
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?
ivan is offline   Reply With Quote
Old Feb 14th, 2006, 1:35 PM   #8
Klarre
Game engine designer
 
Klarre's Avatar
 
Join Date: May 2005
Location: Sweden
Posts: 314
Rep Power: 4 Klarre is on a distinguished road
Yes you can. I don't remember how you do it right now though. It is a little bit tricky, but it is possible!
Klarre is offline   Reply With Quote
Old Feb 14th, 2006, 1:37 PM   #9
nnxion
Programming Guru
 
nnxion's Avatar
 
Join Date: Jun 2005
Location: elemental plane
Posts: 1,429
Rep Power: 5 nnxion is on a distinguished road
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
nnxion is offline   Reply With Quote
Old Feb 14th, 2006, 1:52 PM   #10
ivan
Professional Programmer
 
ivan's Avatar
 
Join Date: Sep 2005
Location: serbia & montenegro
Posts: 484
Rep Power: 4 ivan is on a distinguished road
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.
ivan is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 12:45 PM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC