473,508 Members | 2,130 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

similiar class to one in java

112 New Member
i was wondering if there is anything in c++ that resembles a java 'arraylist'

and arraylist works exactly like an array excpet you can keep changing its size without causing any problems


thanks,
ken
Oct 23 '07 #1
12 1400
sicarie
4,677 Recognized Expert Moderator Specialist
I would suggest looking into vectors.
Oct 23 '07 #2
Cucumber
90 New Member
i was wondering if there is anything in c++ that resembles a java 'arraylist'

and arraylist works exactly like an array excpet you can keep changing its size without causing any problems


thanks,
ken
Sounds like a C++ vector to me.

#include <vector>
...
vector< int > foo;
foo.push_back(10);
int bar = foo[0];

Edit: Sorry, I wasnt aware it was already answered
Oct 23 '07 #3
sicarie
4,677 Recognized Expert Moderator Specialist
Edit: Sorry, I wasnt aware it was already answered
I think I snuck it in right before you did :)
Oct 23 '07 #4
drsmooth
112 New Member
i looked into vectors in a book i borrowed from one of my teachers, is it so that you can only remove or things from the end?

i need to be able to remove pieces from the middle of the list...
Oct 23 '07 #5
Ganon11
3,652 Recognized Expert Specialist
A vector allows random access (e.g. getting stuff from the middle of the list) and random removal (e.g. deleting stuff from the middle of the list). Just get an iterator to a certain point in the list, the call the vectors .erase method.
Oct 23 '07 #6
drsmooth
112 New Member
would the .erase method be like

vct.erase(index);

??
Oct 24 '07 #7
sicarie
4,677 Recognized Expert Moderator Specialist
The documentation for the erase method can be found in the link I posted...

And the erase method could have been found there as well...
Oct 24 '07 #8
drsmooth
112 New Member
ok, that was my bad lol i didnt notice that there was a link there.

nevertheless, i still have a question:
could someone post and example of adding a class that i created into a vector?

lets say a "Civilian class into a vector<Civilian>

Civilian c();
people.push_back(c);

that is what i tried whereby i got:

[C++ Error] Keep.cpp(49): E2285 Could not find a match for 'vector<Civilian,allocator<Civilian> >::push_back(Civilian (*)())'
Oct 24 '07 #9
Ganon11
3,652 Recognized Expert Specialist
When you instantiate an object without parameters, do not use parentheses. The compiler thought you were declaring a function that returned a Civilian. Instead use:

Expand|Select|Wrap|Line Numbers
  1. Civilian c;
Oct 24 '07 #10
drsmooth
112 New Member
ok thank you.

i havd one more side question along with this one:

in java concatenateing an int to a string was a as simple as like:
string s;
int num = 0;
s = "num: "+num;

when i do that in c++ and try to cout the string it comes up with a wierd picture like a smiley face or something...

is it something i am doing wrong?
Oct 24 '07 #11
Ganon11
3,652 Recognized Expert Specialist
It probably interprets the integer value as an ASCII value and tries to append that character. Try:

Expand|Select|Wrap|Line Numbers
  1. s = "num= " + static_cast<char>(num);
Oct 24 '07 #12
drsmooth
112 New Member
now i get even stranger resutls:

this line:
Expand|Select|Wrap|Line Numbers
  1. name = "Unnamed Civilian: "+ static_cast<char>(myID);
appears in a civilian class and name is a string.
when i cout the strings of successive civilians, i get these results:

Unnamed Civilian
nnamed Civilian
named Civilian
amed Civilian
med Civilian
ed Civilian
d Civilian
ect...

the value in myID comes from a static int, nextID which starts at 0 and then nextID++ every time a number is used...

i assume it has something to do with how i concatenate the string because a cout of the myID values yields:

0
1
2
3
4
5
6
7
8
9
Oct 24 '07 #13

Sign in to post your reply or Sign up for a free account.

Similar topics

8
10811
by: Fu Bo Xia | last post by:
the java.lang.Object.forName method takes a java class name and returns a Class object associated with that class. eg. Class myClass = Object.forName("java.lang.String"); by if i only know the...
0
1354
by: news | last post by:
My question is for the software, IDE and programming in general. Is there an add-in or a program that is similiar to the search bar function in the Office Xp software where you type in any...
19
105744
by: Dave | last post by:
Hi, I have done some research, trying to Clear The Screen in java code. The first option was the obv: system.out.print("\n\n\n\n\n\n\n\n\n\n\n\n"); then i heard about this method:...
4
3409
by: angel | last post by:
A java runtime environment includes jvm and java class (for example classes.zip in sun jre). Of course jython need jvm,but does it need java class. Thanx
1
3856
by: aapexclient | last post by:
I have a problem with Java finding my custom class directory. I have searched the forums and tried everything, but nothing seems to work. The <javapgm>.java compiled cleanly and created...
2
2950
by: inetjack | last post by:
Hi, This is a little test application, generating and compiling code at runtime. The loadClassLoader() method of the Factory Object suppose to unload all class previously loaded. It does not...
1
13478
by: sandhyabhavani | last post by:
This article is used to zip a file or directory using vb.net. The classes and method to zip a file is availale in java.io, java.util, java.util.zip class library.To import these you have to add a...
5
1542
by: Ayebaleet | last post by:
Hello everybody, I understand the basics of Java, but not such features as are helpful in an applet namely Listeners. I wanted to create a feature like this: A program would be displayed in...
0
10707
Debadatta Mishra
by: Debadatta Mishra | last post by:
Introduction In this article I will provide you an approach to manipulate an image file. This article gives you an insight into some tricks in java so that you can conceal sensitive information...
0
7123
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7326
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
7383
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
7046
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
4707
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3194
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
3182
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1557
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
0
418
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.