#### Archived

This topic is now archived and is closed to further replies.

# vb style arrays in c++

This topic is 5576 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

ok, i know visual basic sucks (unless you are making a really GUI enhanced program, such as a map editor or something), but if it has any good features, its gotta be the way arrays are handled. Ok in vb, here is a quick run down of how arrays are treated Public Whatever() as string this is a dynamic array, it has no size, cuz we dont know how big its going to be Redim Whatever(5) we just made the array have 6 parts Whatever(0) = "Yeah" Whatever(1) = "Uh huh" Whatever(2) = "Wowers!" Whatever(3) = "Coolio" Whatever(4) = "Yippy" Whatever(5) = "HOLY CRAP" Redim Preserve Whatever(4) we just made the array have 5 parts, and the preserve keywords lets us keep the memory of all the other parts we didnt get rid of, and we got rid of part 5 Redim Whatever(4) we just made the array hvae 5 parts, but we didnt preserve anything so it got completely erased, and now its 5 empty strings in the array Here is a common use that makes vb arrays so cool
Public Type item
name as string
id as integer
End Type
Public Items() as item

Redim Preserve Items(Ubound(Items)+1) //ubound gets the upper bound of the array
Items(Ubound(Items)) = i //ubound is now 1 more than it was before, cuz we added one, we set this to i
End Sub
Public Sub RemoveItem(name as string)
Dim del as integer
Dim i as integer
For i = 0 to Ubound(Items)
If Items(i).name = name then //if we found the name
del = i //this is the place we want to start at
Exit For
End If
Next
For i = del to Ubound(Items)-1 //start at the place, and iterate
Items(i) = Items(i+1) //set this item to the next one
Next
Redim Preserve Items(Ubound(Items)-1) //delete the last one
End Sub

So as you can see, i want to accomplish this same thing in C++. Also, I have not found a way to determine the UBOUND of an array in C++. The C++ equivalent of this would just be the number of parts in the array minus 1, because C++ doesnt let you start from 1 to 10, or 5 to 43 like vb does. C++ always starts at 0. So the upperbound would just be the number of parts minus 1. Any help will rock, thanks, --Fireking Owner/Leader Genetics 3rd Dimension Development

##### Share on other sites
std::vector will do 99% of that - everything except from a value other than 0 (which is of questionable value).

  #include <iostream> #include <vector> #include <string> using namespace std;int main(){vector<string> whatever;whatever.resize(6); //look, you actually tell it 6 to get 6 partswhatever[0] = "Yeah";//...whatever[5] = "HOLY CRAP";copy(whatever.begin(), whatever.end(), ostream_iterator<string>(cout, ", "));cout<<endl;system("pause");}

##### Share on other sites
this is not general enough for my purposes

i need to do this with any data type, struct, or class, regardless of its type. Vb allows you to do this with anything

Notice in my example i used Whatever() as Item, a struct i declared my self (a struct is a type in vb)

--Fireking

Genetics 3rd Dimension Development

##### Share on other sites
Well you see where it says vector<string> well that could just as easily be vector<whateverTypeOnEarth> and it would work the same. Now what you can''t do with Vectors is have a vector that holds both ints and strings.

##### Share on other sites
indeed, the whole point of the STL and the template system IS thats its very general

##### Share on other sites
so this isnt possible?
class myclass{public:    int wow;    char *neato;};vector< myclass > whatever;

if thats not possible, how can i do what im wanting to do?

also, why is this called a vector?

also, why doesnt C++ have that much support for arrays? I though it would have more than VB does! VB has LBound UBound Redim and Preserve, and tons of others!

--Fireking

Genetics 3rd Dimension Development

[edited by - fireking on September 11, 2002 11:38:14 PM]

[edited by - fireking on September 11, 2002 11:38:47 PM]

[edited by - fireking on September 11, 2002 11:39:22 PM]

##### Share on other sites

CEO Platoon Studios

##### Share on other sites
quote:
Original post by fireking
so this isnt possible?
class myclass{public:    int wow;    char *neato;};vector< myclass > whatever;

if thats not possible, how can i do what im wanting to do?

yes, it is possible.

quote:

also, why is this called a vector?

because it dynamical resizes its self as needed

quote:

also, why doesnt C++ have that much support for arrays? I though it would have more than VB does! VB has LBound UBound Redim and Preserve, and tons of others!

because those are simpley functions used on arrays and do practicaly the same thing as the STL vector above, pop_top() for example removes the top varible in the vector leaving the rest there (much like perserve seams to do)

basicaly, as the bloke above said, read about the STL and its functions and you''ll soon see you can do what you want to do (including the iteration in the first question), yes it will take a bit more code work than VB in some cases, but VB is MENT to be simple and hide things from people, thats why its basic, if you want a simple life where things are quick and easy I dont see C/C++ being the road for you to take

##### Share on other sites
quote:
Original post by elis-cool

CEO Platoon Studios

he said you couldnt use it with a string and an int, and my class contained both a string and an int, which is why it was in question...

i started c++ 5 days ago and i am moving a long really fast. Basically because i have experience in other programming languages, and most concepts in programming are the same, its just a matter of converting the syntax

--Fireking

Genetics 3rd Dimension Development

##### Share on other sites
quote:
Original post by fireking
i started c++ 5 days ago and i am moving a long really fast. Basically because i have experience in other programming languages, and most concepts in programming are the same, its just a matter of converting the syntax

Beware of trying too hard to map C++ concepts to VB. What is a concept in VB may be a procedure in C++ instead. The array thing is a case in point; VB allows you to not worry about arrays, whereas C++ forces you to. the STL vector class simplifies this somewhat, so it''s not the best example, but once you learn about dynamic arrays you''ll see what I mean.

Don''t listen to me. I''ve had too much coffee.

##### Share on other sites
your class can contain anything you like.

The vector object is a container for your class, your class holds the data in it, the vector of your class just holds references to your class in it.

The fact you didnt understand this concept and didnt read up on it shows your lack of understanding, I personaly know many programming langauages, however C/C++ are the most challanging i''ve personaly come across which is why after having known ''em both syntax wise for 2 years now I''m still learning new things about them all the time, just because you know syntax dont mean you know the langauge.

I surgest you slow down a bit and donr be in such a hurry learn, or you''ll end up doing a half arsed job.

Take more time to learn the basics of the language before worrying about things such as the STL and templates, because I think you still have a way to go with the basics based on your current display of knowlage and shouldnt be worrying about advanced stuff like this, stick to doing your own memory management etc and learn how to resize arrays your self and keep track of the size, then, once you have grasped the basics very firmly move on to the STL and templates (as these are BIG and complex subjects, and i dont know of a comparibly system in any other langauge)

##### Share on other sites
ah, i see part of the confusion, the AP said you cant have a vector which holds strings and ints

what he refered to is that fact its a one or the other situation, so you can do

vector
or
vector

but you cant mix and match at all (i dunno if VB allows you do to this, or type cast or something)

##### Share on other sites
quote:
Original post by _the_phantom_
ah, i see part of the confusion, the AP said you cant have a vector which holds strings and ints

what he refered to is that fact its a one or the other situation, so you can do

vector
or
vector

but you cant mix and match at all (i dunno if VB allows you do to this, or type cast or something)

vb has automatic type conversion, but only in certain cases

for instance

Blah as string
Boo as Boolean
Boo = True
Blah = Boo

Blah will now read "True", but consider the following

Blah as Integer
Boo as Boolean
Boo = True
Blah = Boo

Blah will now be 1 or -1 or whatever (I forget actually which is used)

##### Share on other sites
quote:
Original post by Sneftel
[quote]Original post by fireking
i started c++ 5 days ago and i am moving a long really fast. Basically because i have experience in other programming languages, and most concepts in programming are the same, its just a matter of converting the syntax

Beware of trying too hard to map C++ concepts to VB. What is a concept in VB may be a procedure in C++ instead. The array thing is a case in point; VB allows you to not worry about arrays, whereas C++ forces you to. the STL vector class simplifies this somewhat, so it''s not the best example, but once you learn about dynamic arrays you''ll see what I mean.

Don''t listen to me. I''ve had too much coffee.

I am really interested in Dynamic Arrays, have an article on it?

##### Share on other sites
quote:
Original post by _the_phantom_
your class can contain anything you like.

The vector object is a container for your class, your class holds the data in it, the vector of your class just holds references to your class in it.

Does it hold the address and keep track of the length of the variable in question? Thats what I thought it did in the first place...

quote:
Original post by _the_phantom_
The fact you didnt understand this concept and didnt read up on it shows your lack of understanding, I personaly know many programming langauages, however C/C++ are the most challanging i've personaly come across which is why after having known 'em both syntax wise for 2 years now I'm still learning new things about them all the time, just because you know syntax dont mean you know the langauge.

I dont have lack of understand, its lack of researching. I simply did not look it up. Also, the syntax confused me, as a begginner i've never seen the < whatever > used before

quote:
Original post by _the_phantom_

I surgest you slow down a bit and donr be in such a hurry learn, or you'll end up doing a half arsed job.

Take more time to learn the basics of the language before worrying about things such as the STL and templates, because I think you still have a way to go with the basics based on your current display of knowlage and shouldnt be worrying about advanced stuff like this, stick to doing your own memory management etc and learn how to resize arrays your self and keep track of the size, then, once you have grasped the basics very firmly move on to the STL and templates (as these are BIG and complex subjects, and i dont know of a comparibly system in any other langauge)

Im going slow, what I mean by fast is that Ive grasped most of the basic concepts of converting programming ideas and concepts from other languages to c++ and vise versa. I understand most of the syntax, and how to create classes, and how to minipulate them. There are just gaps or holes in the knowledge that i have of C++. Such as function pointers, I dont know exaclty what they do. Things like that, and several others I am missing.

The reason why I had so much trouble with the current situation is strictly syntax based. In vb, it was easy get change the size of an array with ReDim. In C++, there is no syntax to do that, you must actually use the STL or implement your own method (moving memory around to change the array size and dimensions). Thats why it was so confusing, because C++ didnt have direct syntax support for changing array sizes, and getting array sizes such as Ubound.

--Fireking

Genetics 3rd Dimension Development

[edited by - fireking on September 11, 2002 12:42:42 AM]

##### Share on other sites
quote:
Original post by fireking
[quote]Original post by _the_phantom_
your class can contain anything you like.

The vector object is a container for your class, your class holds the data in it, the vector of your class just holds references to your class in it.

Does it hold the address and keep track of the length of the variable in question? Thats what I thought it did in the first place...

the vector object holds the location of each instance of your class, and internaly tracks its length and the number of things it holds

quote:

I dont have lack of understand, its lack of researching. I simply did not look it up. Also, the syntax confused me, as a begginner i''ve never seen the used before

If you dont understand it (i.e you was confused) then you had a lack of understanding, the stuff is todo with templates, an advanced subject someone who is a beginner shouldnt be worrying about yet as in the book I read about it it recomened you have very good grasp of templates, polymophisim, references and a few other subjects before worrying about them.

quote:

Im going slow, what I mean by fast is that Ive grasped most of the basic concepts of converting programming ideas and concepts from other languages to c++ and vise versa. I understand most of the syntax, and how to create classes, and how to minipulate them. There are just gaps or holes in the knowledge that i have of C++. Such as function pointers, I dont know exaclty what they do. Things like that, and several others I am missing.

The reason why I had so much trouble with the current situation is strictly syntax based. In vb, it was easy get change the size of an array with ReDim. In C++, there is no syntax to do that, you must actually use the STL or implement your own method (moving memory around to change the array size and dimensions). Thats why it was so confusing, because C++ didnt have direct syntax support for changing array sizes, and getting array sizes such as Ubound.

those gaps need to be filled before advancing, the learning the basics about moving memory around and new/delete memory will give you a greater understanding of the whole language.

You problem wasnt syntax based, it was a lack of understanding of how VB hides the manipulation of arrays from you, VB doesnt have some magical means of working out array lenghts or changing them, it uses the same methods we use with C or C++ just hidden from the user (ie tracking length in a varible, resizing RAM and releasing it), which is what you would expect from a high level language, where as C/C++ are more low level thus you have to do more tasks for yourself, such as this one with the arrays or use a premade system if one exists like the STL.

##### Share on other sites
the phantom said:

those gaps need to be filled before advancing, the learning the basics about moving memory around and new/delete memory will give you a greater understanding of the whole language.

You problem wasnt syntax based, it was a lack of understanding of how VB hides the manipulation of arrays from you, VB doesnt have some magical means of working out array lenghts or changing them, it uses the same methods we use with C or C++ just hidden from the user (ie tracking length in a varible, resizing RAM and releasing it), which is what you would expect from a high level language, where as C/C++ are more low level thus you have to do more tasks for yourself, such as this one with the arrays or use a premade system if one exists like the STL.
============================================

well thats what im saying. At first I thought that C++ would have means in which to accomplish memory management such as VB does. I do understand that VB is hiding it from me, and its not magical. I was trying to say that I thought C++ had some sort of memory management involving arrays, like VB does, but now I know, thanks to this thread, that it does not. The only memory management(or the most commonly used) methods are new and delete, which just gets an address in the memory and stores your value there. The variable name (if not a pointer) represents the value, and &variablename represents the address. Pointers are different where *variablename represents the value, and variablename represents the address.

So anyways, VB just does a lot for you, hiding it from you. Such as the Redim Statement, which basically takes your array, and your other parameters, and returns the final outcome back to the variable or array.

So it was syntax based, in a way, because all these years vb has been telling me that Redim will resize my array. I was simply trying to find the c++ equivalent to redim. Well there is no C++ equivalent to Redim, which is part of the syntax. (well actually, its a built in function in vb, so its not really part of the syntax, but they do a good job of making it look like it is).

Anyways, thanks for all the help and knowledge,

--Fireking

Genetics 3rd Dimension Development

##### Share on other sites
quote:
Original post by fireking
So it was syntax based, in a way, because all these years vb has been telling me that Redim will resize my array. I was simply trying to find the c++ equivalent to redim. Well there is no C++ equivalent to Redim, which is part of the syntax.

Now that you "know" that, just to make things a bit more complex: there is a C function called realloc() which works a heck of a lot like ReDim. But try not to use it in C++, unless you have a good grasp of how to use C memory management functions in C++.

Don''t listen to me. I''ve had too much coffee.

##### Share on other sites
is this good enough memory management skills for C?

int *c = (int *)malloc(sizeof(int));
*c=5;
free(c);
//make a pointer c, allocate the memory and give it a value of 5 then free the memory

there are other memory functions, but i didnt read up on them..

please explain why i should not use them in C++? where is the concern?

--Fireking

Genetics 3rd Dimension Development

##### Share on other sites
syntax = layout, keywords, blocking etc

redim = language feature not part of the syntax

As for C++ having advanced memory management I think a spot of reading about it would have corrected that assumption for you, but thats a minor point now

btw, one point I''ll make, arrays are just pointers in effect, example :

int wibble[10];
and
int *wibble = new int[10];

can be accessed the same way either
wibble[0]
or
*wibble

as the array name is just a pointer to an address of memory and the [] tells the compiler to derefence it to get the values, which is the whole reason why arrays ALWAYS start at 0, VB is just playing tricks to make you think otherwise

##### Share on other sites
quote:
Original post by fireking
there are other memory functions, but i didnt read up on them..

please explain why i should not use them in C++? where is the concern?

It is good practice to advocate the use of C++ features exclusively (at least until you know better) when using a C++ compiler because of better interoperation of the features. The Standard C Library memory management functions do not know about the behaviors of classes (and structs; they''re functionally near-equivalent, only difference is inheritance/access specification), meaning they do not call constructors or destructors and other such nuances. The Standard C++ operators new and delete are not only aware of C++ features, but are user-extensible and fully support all C memory operations. That''s just one of the instances where modern C++ features are to be preferred.

Prefer std::string over null-terminated character strings.
Prefer std::vector over type *, dynamically allocated arrays.

Naturally, there are times when the right-hand constructs are logically equivalent - and occasionally even superior - but they are relatively few.

##### Share on other sites
What you need to realize, fireking, is that VB does a TON of shit for you (meaning you don''t have to worry about it).

About the only thing that c++ does for you is maintain the program stack.

That''s it. Everything else, you''re on your own. That means that you have to explicitly tell your program exactly what you want it to do 99% of the time.

Now, you can simplify that by using C++ features, such as classes, templates, operator overloading, etc.. but the fact remains that you have to explicitly define the behavior somewhere before it can be used.

You''re a lot safer in C++ if you just basically assume that you (the programmer) have to tell the language what to do, throughout the entirety of the program.