Faqts : Business : Programming : Shopping For You : C++

+ Search
Add Entry AlertManage Folder Edit Entry Add page to http://del.icio.us/
Did You Find This Entry Useful?

49 of 166 people (30%) answered Yes
Recently 6 of 10 people (60%) answered Yes

Entry

How do I define and Use Dynamic Array in C++?

Aug 25th, 2009 00:04
Joe Bloggs, Colin Thomsen, Rafael Udaondo, Lam Ho,


See this sample with a int array:

   int *array = new int[ 256];
   array[ 0] = 10;
   array[ 1] = 2;
   printf( "%d %d", array[ 0], array[ 1]);


Note by Colin Thomsen.

These days you should be able to get by without using arrays at all
(unless you have to interface to legacy C code).

The key is to pick a container from the Standard Template Library (STL)
such as a vector. STL containers:
- are highly optimised
- handle memory allocation automatically (dynamic resize)
- allow you to make the equivalent of an array for objects without
default constructors
- provide bounds checking if you use the 'at' method
- provide a size method

For example

#include <vector>
#include <iostream>

int main()
{
   // allocate 256 elements for a variable called container
   std::vector<int> container(256);
   container[0] = 10;
   container[1] = 2;
   // Use streams instead of printf, since they are typesafe
   std::cout <<  container[0] << " " << container[1] << std::endl;

   // use another container without knowing final size
   std::vector<int> container2;
   container2.push_back(10);
   container2.push_back(2);
   std::cout <<  container2[0] << " " << container2[1] << std::endl;
   std::cout << "Size : " << container2.size() << std::endl;

}

======================================

http://www.attorneyslawyersdirectory.com
http://www.findattorneyslawyers.com
http://www.goodlawyersattorneys.com
http://www.professionallawyersattorneys.com
http://www.bestprofessionallawyers.com
http://www.americalawyersattorneys.com
http://www.availablelawyers.com
http://www.lawyersattorneyslist.com
http://www.lawyersattorneysfirm.com
http://www.originallawyers.com
http://www.lawyersgulf.com
http://www.lawyersband.com
http://www.lawyersdatacenter.com
http://www.lawyerstigers.com
http://www.lawcliff.com
http://www.tidylawyers.com
http://www.lawyersblock.com
http://www.californialawyersattorneys.com
http://www.chicagoattorneyslawyers.com
http://www.goodcalifornialawyers.com
http://www.lawyersattorneysfaqs.com
http://www.divorceattorneyslawyers.com
http://www.duiattorneyslawyers.com
http://www.personalinjurylawyerslist.com
http://www.injurylawyersattorneys.com
http://www.dwilawyersattorneys.com
http://www.healthbigboss.com
http://www.healthdohealth.com
http://www.healthcass.com
http://www.healthsilence.com
http://www.healthpicnic.com
http://www.blackmesothelioma.com
http://www.affordablelawyersattorneys.com
http://www.pickarticles.com
http://www.pickarticle.com
http://www.bloodyarticles.com
http://www.healthment.com
http://www.healthbyhealth.com
http://www.ehealthebooks.com
http://www.bestproductsofhealth.com
http://www.dietlosingweight.com
http://www.americanehealth.com
http://www.healthydepartment.com
http://www.medicalehealth.com
http://www.healthservicescenter.com
http://www.ehealthenews.com
http://www.ehealthdept.com
http://www.benefitshealthcenter.com
http://www.toplawyersattorneys.com 
http://www.healthxhealth.com
http://mesotheliomakiller.com
http://www.healthinhealth.com
http://www.booksyshop.com
http://www.mesotheliomaout.com
http://www.mesotheliomadown.com
http://www.qwesz.com
http://americanahost.com
http://www.healthfirewall.com
http://www.healthbodyguard.com
http://www.flathealth.com
http://www.computerstan.com
http://www.financestan.com
http://www.technologystan.com
http://www.zobab.com
http://www.businessian.com
http://www.tarabiza.com
http://www.internetstan.com
http://www.moneyenews.com
http://www.hantira.com
http://www.mantofa.com
http://www.tantofa.com
http://www.fantofa.com
http://www.tanpola.com
http://www.tampola.com
http://www.yamot.com
http://www.mozmar.com
http://www.uploadarticles.com
http://www.articlesfreedirectory.com
http://www.articlesxarticles.com
http://www.shikapika.com
http://www.ganazat.com
http://www.stakoza.com
http://www.stupidarticles.com
http://www.damima.com
http://www.halazona.com
http://www.best-cheap-web-hosting.com

======================================