Warning: Function ereg() is deprecated in ..../includes/class_postbit.php(345) : eval()'d code on line 4

Warning: Function split() is deprecated in ..../includes/class_postbit.php(345) : eval()'d code on line 19

Warning: Function ereg() is deprecated in ..../includes/class_postbit.php(345) : eval()'d code on line 4

Warning: Function split() is deprecated in ..../includes/class_postbit.php(345) : eval()'d code on line 19

Warning: Function ereg() is deprecated in ..../includes/class_postbit.php(345) : eval()'d code on line 4

Warning: Function split() is deprecated in ..../includes/class_postbit.php(345) : eval()'d code on line 19

Warning: Function ereg() is deprecated in ..../includes/class_postbit.php(345) : eval()'d code on line 4

Warning: Function split() is deprecated in ..../includes/class_postbit.php(345) : eval()'d code on line 19

Warning: Function ereg() is deprecated in ..../includes/class_postbit.php(345) : eval()'d code on line 4

Warning: Function split() is deprecated in ..../includes/class_postbit.php(345) : eval()'d code on line 19
Changing size of arrays in C++
Results 1 to 5 of 5

Thread: Changing size of arrays in C++

Hybrid View

  1. #1
    Senior Member
    Join Date
    Apr 2002
    Posts
    417

    Changing size of arrays in C++

    Hi,

    This question has plagued me for some time now.

    In C++, is there a way to change the size of an array, add to the end of one, subtract an element from one(shorten by an index)?

    In perl you use, pop, splice,push

    What happens when you don't know how much storage to set aside?

    Do you just keep track of how many elements you store then if it over runs the array you write them to file and re-initialize. That sounds too complicated and slow.

    Thanx,

  2. #2

    Re: Changing size of arrays in C++


    Hi,

    This question has plagued me for some time now.

    In C++, is there a way to change the size of an array, add to the end of one, subtract an element from one(shorten by an index)?

    In perl you use, pop, splice,push

    What happens when you don't know how much storage to set aside?

    Do you just keep track of how many elements you store then if it over runs the array you write them to file and re-initialize. That sounds too complicated and slow.

    Thanx,
    Actually, arrays don't really have this functionality. For something like this, I would suggest a vector. Vectors are like a dynamic array of any size, of a given object.

    In a app I'm working on, I've got a vector of network profiles. Each new profile gets added to the vector, and I have a member of the profile as a search key, like profile number or something like that. This way, I don't have to know the exact position of each object, just iterate for the number. Now, if done right, the number should match the position in the vector.

  3. #3
    Senior Member
    Join Date
    Apr 2002
    Posts
    417

    Re: Changing size of arrays in C++

    could you please give me an example?

    thanx,

  4. #4

    Re: Changing size of arrays in C++

    You could use any of the containers in the STL(vector,list...).
    Check out:
    http://www.sgi.com/tech/stl/

    The introduction has examples of usage of vector.

    Cheers,
    T.

  5. #5
    Moderator
    Advisor
    redhead's Avatar
    Join Date
    Jun 2001
    Location
    Copenhagen, Denmark
    Posts
    811

    Re: Changing size of arrays in C++

    What happens when you don't know how much storage to set aside? *

    Do you just keep track of how many elements you store then if it over runs the array you write them to file and re-initialize. That sounds too complicated and slow.
    Well in C I would do it like:
    Code:
    typedef (what ever) Item;
    Item* ItemList;
    int numItems = 0;
    int maxItems = 0;
    
    int addItem (Item item) {
     *if (numItems >= maxItems -1) {
     * *maxItems += 10;
     * *ItemList = (Item*)realloc (ItemList, maxItem * sizeof (Item));
     * *if (!ItemList) * 
     * * *return -1;
     *}
     *ItemList[numItems] = item;
     *numItems++;
     *return 0;
    }
    But since this is C++ I would use the <vector> facility of STL, then you have your pop/push functions like:
    Code:
    vector <what ever> my_vector_array;
    my_vector_array.push(new_item);

Similar Threads

  1. directory size
    By pansarevaibhav in forum Linux - General Topics
    Replies: 2
    Last Post: 02-12-2006, 10:52 PM
  2. avatar size
    By Waka-Gashira in forum Announcements and Suggestions
    Replies: 7
    Last Post: 01-02-2005, 09:23 AM
  3. avatar size?
    By Xamindar in forum Announcements and Suggestions
    Replies: 7
    Last Post: 09-19-2004, 11:52 PM
  4. Super Size Me
    By gorn in forum General Chat
    Replies: 12
    Last Post: 05-10-2004, 09:03 PM
  5. I need help with char arrays in c
    By t048 in forum Linux - Software, Applications & Programming
    Replies: 6
    Last Post: 02-12-2002, 11:49 PM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •