Archived

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

vb style arrays in c++

This topic is 5879 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.

1. 1
2. 2
3. 3
Rutin
22
4. 4
5. 5

• 11
• 18
• 14
• 9
• 9
• Forum Statistics

• Total Topics
632929
• Total Posts
3009281
• Who's Online (See full list)

There are no registered users currently online

×