Skip to main content
gamedev.net
🔒 Locked

resizing an array?

Started by Stashi Nov 27, 2001 at 4:05 PM 7 replies 650+ views
Original Post
Stashi
Stashi
if i create a normal array, like this.. int nums[10]; how would i go about resizing that array, but without losing all the values i may have set in it so far? _NO_ this is not for homework! its for a personal project im doing. i am really stumped and would appreciate any help. should i create a class for the array? is there a simple redeclare statement i can use? or can a normal function be used? please help (by the way i do not want to use apmatrix, etc)
Urea
Urea
Well, first...

int aArray[10];

resize you say?

int bArray[20];

for(int i = 0;i < 10;++i)
{
bArray = aArray[i];
}
//notice bArray has the same contents, but a different size?
--------------or-----------------
int * aArray = new int[10];
int * tmpArray = new int[10];
for(int i = 0;i < 10;++i)
{
tmpArray[i] = aArray[i];
}

delete [] aArray;
aArray = new int[20];

for(int i = 0;i < 10;++i)
{
aArray[i] = tmpArray[i];
}
delete [] tmpArray;
//notice, the original array, with a new size?

maybe this will help?

love,
Daniel

Urea
Urea
sorry...on the first example...

the line that looks like:

bArray = aArray;

should look like:

bArray[i] = aArray[i];
ju2wheels
ju2wheels
If you create the array on the stack you might be able to do the following:

//....
int *array= new int[10];
int *arrayTwo;

for(int i = 0; i < 9; i++)
array= i*2;
//....
//void resize(array, arrayTwo);


//since we did not use offset 9 we can resize the array:
void resize(int *pArray, int *pArrayTwo)
{
for(int i = 0; i < 10; i++)
{
if(pArray[i] == " ")
{
pArrayTwo = new int [i-1];

for(int j = 0; j <= i; j++)
pArrayTwo[j]= pArray[j];

delete array;//if global
array = new int[10];//reset to empty if global
i = 10;
}
}
}


obviously this is not the best way, but it might work, I don''t have time to check if it does... I assumed you would assign values to the array in order(0 to n-1).
Stashi
Stashi
thanks guys!

for Urea''s second suggestion (the one with aArray and tmpArray, etc), wouldnt that be a practical way to do it also? it sounds really good without having to start using STD''s etc..

but on the other hand, if i do decide to use STD, why wouldnt this work?
  
#include <vector>
using namespace std;
vector<string> strArray;


AnonPost said to do std::vector, but is there a difference?

also, how come if i use for instance,

  
#include <string>


i have to not include the .h in my iostream includes as well? does this also apply for every header file i may want to open for my entire project?
ju2wheels
ju2wheels
you don't include the .h in your include files when you are going to use the now standard stl files for C++. The ones with the .h like iostream.h are the old standard compared to the new and better iostream.

Edited by - ju2wheels on November 28, 2001 4:46:04 PM

Edited by - ju2wheels on November 28, 2001 4:47:12 PM
Oluseyi
Oluseyi
quote:
Original post by Stashi
...if i do decide to use STD...

STL. S tandard T emplate L ibrary.

quote:
AnonPost said to do std::vector, but is there a difference?

No. Everything in the STL (and the Standard C++ Library) is located within a namespace (kinda like a big struct) named "std". The reason we have namespaces is to allow different people write functions and data structures (classes) with the same names without having them collide. You can access a member of a namespace by qualifying it with the namespace identifier (std::vector, std::endl), or by importing either the member alone or the entire namespace. That''s what the keyword "using" does; it imports a namespace or namespace member (which means you don''t have to use the qualifier every time).

using std::string; // only imports string
using namespace std; // imports entire std namespace


You can create your own namespaces and use names that are logical for what you want to do. For example, if I want to write a 3d vector class and name it "vector" (which would collide with std::vector), then I can place it in a namespace of my own and qualify them both:

namespace three_d
{
class vector
{
...
};
};
.
std::vector<three_d::vector> vectors;


quote:
...how come ...i have to not include the .h in my iostream includes as well?

The use of the .h extension is considered the "old" way, and does not use namespaces. This means that there is a possibility that someone out there may write a function with the same name, resulting in a collision. Many of the new header files take this form, effectively just including the old headers in the std namespace:
// <header>
namespace std
{
#include <header.h>
}


quote:
does this also apply for every header file i may want to open for my entire project?

Headers are text files. They have no special properties or attributes. That means that whether your own headers use the .h extension or not is totally up to you. The C++ Standards Committee felt it was a good thing for them, so users could distinguish between new and old library headers. You don''t have that problem, so you don''t have to bother.


Start Here!

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.