Computer Forums

Member Login

Remember Me? Sign Up! | Forgot Password
 
Slogan
 
Closed Thread
Old 09-05-2006, 10:44 AM   #1 (permalink)
 
True Techie

Join Date: May 2006

Posts: 184

ssjheero_yuy99

Default C++ Code and Arrays

Hello,

I am just learning C++, I have already learned the concepts through Object Pascal and am now moving onto C++.

My question deals with arrays, specifically dynamic arrays. (please note I might be using Pascal terms).

I have a program that takes and inputs integers from the command line, however, this input stream can be as large or as small as the user wants it to be.

What I want to do is create a dynamic array for the stream to drop the values into so that I can remember the values that were entered into the input stream for later manipulation.

I know in Pascal I could create a dynamic array with no size or any size, then later in the program extend or decrease the length of the array at will. using the setlength(<arrayname>, <arraysize>); command.

What I am wondering is if there is the a way to do this same thing in C++, so that I don't have to have an array that is infinitly big to start with.

Thanks for your help and for reading!
ssjheero_yuy99 is offline  
Old 09-05-2006, 10:47 PM   #2 (permalink)
 
Software Developer

Join Date: Mar 2006

Location: Columbus, OH

Posts: 569

jaeusm is on a distinguished road

Default

Vectors are dynamic arrays in C++, and are part of the standard template library. Just include the <vector> header file.
jaeusm is offline  
Old 09-07-2006, 02:51 PM   #3 (permalink)
 
True Techie

Join Date: May 2006

Location: Chicago IL

Posts: 104

thejeremy

Send a message via AIM to thejeremy
Default

Make sure you include the <vector> header file (as jaeusm said).

One vector constructor consists of the following,

vector<type> yourArray(x, y);

where "type" is the type of variable that your vector will hold (int, string, etc.), and the initial vector will hold x copies of the value y. For instance, if I used the constructor

vector<int> myArray(10, 0);

myArray would initially be composed of 10 slots that all hold the value 0.

To tack on an additional value at the end of the vector, use the function call push_back.

Here is a code snippet:

vector<string> sentence(3, "hello");
sentence.push_back("world");

The result of sentence is now ["hello", "hello", "hello", "world"]
Of course, for this example, you'd have to include the header file <string> but I'm assuming you already know that
__________________
CPU: AMD Athlon 64 X2 5200+ Windsor, 2.6 GHz
RAM: CORSAIR XMS2 2GB 240-Pin DDR2
VIDCARD: EVGA PCI-Express x16 GeForce 7900GS 256MB
MOBO: ASUS M2N-SLI Deluxe AM2
HDD: Seagate Barracuda 320GB 7200 RPM SATA

my blog: http://jspot.gotdns.com
thejeremy is offline  
Old 09-08-2006, 04:08 PM   #4 (permalink)
 
True Techie

Join Date: May 2006

Posts: 184

ssjheero_yuy99

Default

Hey guys,

Thanks for your help!

I got it figured out now, your responces were very useful.

Thanks again!
__________________
Tech TV is Back!
www.dl.tv
-- watch it, its good stuff.
-------------------------------
AMD Athlon 64 3400+
Powered by: Antec Earth Watts
-------------------------------
ssjheero_yuy99 is offline  
 
Closed Thread

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On