Like Tree3Likes
  • 3 Post By rags_to_riches

Starting with c++

This is a discussion on Starting with c++ within the C++ Programming forums, part of the General Programming Boards category; Hi, I am just starting with c++ and i am a bit confused. My "a priori" knowledge is c and ...

  1. #1
    Registered User
    Join Date
    Jan 2011
    Posts
    176

    Starting with c++

    Hi,

    I am just starting with c++ and i am a bit confused. My "a priori" knowledge is c and in c if i have a large file that i wish to read in into an array, having no clue how large is the file i can allocate an array of size X and the if X is exceeded a reallocate it by, let say, Y. now how is this resolved in c++. I am asking this because i see that c++ uses new() and delete() for memory allocation ? But how is the memory reallocation done?

    Thank you

    Baxy

  2. #2
    a_capitalist_story
    Join Date
    Dec 2007
    Posts
    2,652
    You would avoid it by using one of the standard containers, like vector, instead of an array.
    baxy, laserlight and Elysia like this.

  3. #3
    Registered User
    Join Date
    Jan 2011
    Posts
    176
    is it possible to get some example ? because if i have a file like this:

    Code:
    1
    2
    3
    4
    5
    6
    67
    7
    2
    12
    3
    and i need the numbers in my array. do i then first store them into container and the restore them into array ??

    thnx

    baxy

  4. #4
    Registered User
    Join Date
    May 2010
    Posts
    2,893
    You would use the container instead of the array.

    Jim

  5. #5
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,806
    At its most basic, the outline you'd want is the following:

    1. Create an ifstream object for your input file. Open the file.
    2. Create a vector<int> object for storing your integers that you've read from the file.
    3. Create a single temporary int to read into from the file.
    4. While you can successfully read an integer from the file into the temporary int do the following:
      • Push the temporary int's value onto the vector
    5. Do stuff with the ints now stored in the vector


    Relevant headers for the above would be at least <fstream> and <vector>, probably more (<iostream>) for user I/O.

    Do a quick bit of research and post your attempt if you are having problems.
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Need help starting up :)
    By Arpan Das in forum C++ Programming
    Replies: 19
    Last Post: 06-25-2011, 02:58 PM
  2. Starting out...
    By blarney in forum C Programming
    Replies: 5
    Last Post: 12-24-2007, 10:44 AM
  3. Starting out
    By THolly in forum C++ Programming
    Replies: 3
    Last Post: 07-03-2005, 03:56 PM
  4. HELP with starting out.
    By sworc66 in forum C Programming
    Replies: 4
    Last Post: 09-05-2002, 11:09 AM
  5. starting all over again...
    By dune911 in forum C Programming
    Replies: 5
    Last Post: 07-01-2002, 03:02 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21