Thread: 2D array

  1. #1
    Registered User
    Join Date
    Apr 2011
    Posts
    2

    Unhappy 2D array

    how can i get a character input in a 2d array?

  2. #2
    [](){}(); manasij7479's Avatar
    Join Date
    Feb 2011
    Location
    *nullptr
    Posts
    2,657
    Code:
    char d2array[50][50];
    cin>>d2array[x][y]; //where x,y < 50
    Depending on whether you need to input all the elements, you need to run a loop.

    ##I came across a handy *hack* when first learning dynamic allocation months ago, which according to most books..is impossible, but I tested in two compilers and it worked seamlessly.
    Code:
     char d2array[a][b];
    where a and b are variables ...and the array apparently gets dynamically allocated..Why does this work?..is this compiler specific ? ##

  3. #3
    Registered User
    Join Date
    Apr 2011
    Posts
    2
    i need to insert a line in that array!!!
    just like in sngle dimensional arrays we use getline function!!!

  4. #4
    [](){}(); manasij7479's Avatar
    Join Date
    Feb 2011
    Location
    *nullptr
    Posts
    2,657
    and you expressed that as character input?
    I'm still not sure what you are trying to say..but the following probably fits....;
    Code:
    std::string m[50];
    std::getline(std::cin,m[x]);  //where x <50
    and if you want each string to be a single element of a 2d array..then just replace the 'char's in first reply with 'std::string'
    and the cin ' line with the getline function..

  5. #5
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Quote Originally Posted by manasij7479 View Post
    ##I came across a handy *hack* when first learning dynamic allocation months ago, which according to most books..is impossible, but I tested in two compilers and it worked seamlessly.
    Code:
     char d2array[a][b];
    where a and b are variables ...and the array apparently gets dynamically allocated..Why does this work?..is this compiler specific ? ##
    This is a non-standard compiler extension.
    The correct way is to use std::vector.
    Eg:
    std::vector<std::vector<int>> vec(std::vector(b), a);
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  6. #6
    [](){}(); manasij7479's Avatar
    Join Date
    Feb 2011
    Location
    *nullptr
    Posts
    2,657
    std::vector<std::vector<int>> vec(std::vector(b), a);
    Is there any advantage of initially allocating the size of a std::vector ?.. I have always used std::vector:: push_back(..); when in need of new space..
    Last edited by manasij7479; 04-19-2011 at 06:24 AM. Reason: .. :[b]:p[/b]ush_back(..); was interpreted rather 'nicely'

  7. #7
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    Quote Originally Posted by manasij7479 View Post
    Is there any advantage of initially allocating the size of a std::vector ?.. I have always used std::vector:ush_back(..); when in need of new space..
    There is the advantage of only sizing once if the total number of elements needed can be predicted in advance.

    With multiple calls of push_back(), it is necessary to resize the vector. When the new size exceeds the previous size, it is necessary to copy all elements from an old buffer to a new buffer. Apart from multiple calls to dynamically allocate memory, it is necessary to copy all values to a new buffer every time the vector is resized. These involve performance hits, particularly if copying individual elements is expensive, and increased chances of memory fragmentation (in the executing process, not the vector).

    Implementations of vector can reduce the overhead of repeatedly growing the vector using different strategies (allocating more memory than needed in anticipation of growth, and then not resizing actual buffers for the next few elements push_back'd). No such strategy can beat initialisation with a required size, as that involves resizing the buffer exactly once.
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

  8. #8
    [](){}(); manasij7479's Avatar
    Join Date
    Feb 2011
    Location
    *nullptr
    Posts
    2,657
    ..how is the above process done in a dynamically allocated array ?...i.e when more space is needed...
    Is it also completely copied over?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 9
    Last Post: 08-23-2010, 02:31 PM
  2. Replies: 9
    Last Post: 04-07-2010, 10:03 PM
  3. Replies: 1
    Last Post: 10-21-2007, 07:44 AM
  4. Replies: 6
    Last Post: 11-09-2006, 03:28 AM
  5. Replies: 1
    Last Post: 04-25-2006, 12:14 AM