Solved

array in c++

Posted on 2010-08-22
5
302 Views
Last Modified: 2013-11-23
Hi there,

I have 2dim array which have huge number I have no idea about number my question is How can I initialize this array if not are there any different way like array list in Java which is accept two dim and extend automatically
0
Comment
Question by:obad62
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
5 Comments
 
LVL 32

Expert Comment

by:phoffric
ID: 33497411
In c++, if you say:
     int arr2d[3][4] = {0};
then every element of the array will be initialized to 0.
0
 
LVL 32

Expert Comment

by:phoffric
ID: 33497412
If you say:
    int arr2d[3][4] = {5};
then arr2d[0][0] will be set to 5, and all the other elements in the array will be set to 0.
0
 
LVL 32

Accepted Solution

by:
phoffric earned 500 total points
ID: 33497423
If you want to be able to extend an array, then I recommend using vector.
    http://www.cplusplus.com/reference/stl/vector/

There are various ways to construct/initialize a vector.
    http://www.cplusplus.com/reference/stl/vector/vector/

For example:
    vector<int> second (4,100);      // four ints with value 100

Below is the sample code for the vector constructor link.

To extend the array (without worrying about whether it needs extending), you can just add elements to the vector using push_back.
      http://www.cplusplus.com/reference/stl/vector/push_back/
// constructing vectors
#include <iostream>
#include <vector>
using namespace std;

int main ()
{
  unsigned int i;

  // constructors used in the same order as described above:
  vector<int> first;                                // empty vector of ints
  vector<int> second (4,100);                       // four ints with value 100
  vector<int> third (second.begin(),second.end());  // iterating through second
  vector<int> fourth (third);                       // a copy of third

  // the iterator constructor can also be used to construct from arrays:
  int myints[] = {16,2,77,29};
  vector<int> fifth (myints, myints + sizeof(myints) / sizeof(int) );

  cout << "The contents of fifth are:";
  for (i=0; i < fifth.size(); i++)
    cout << " " << fifth[i];

  cout << endl;

  return 0;
}

Open in new window

0
 
LVL 32

Expert Comment

by:phoffric
ID: 33497440
Above was a 1D discussion of vectors. Here's a thread needing a 2D vector:
     http://w.e-e.com/pJzEMB
0
 
LVL 40

Expert Comment

by:evilrix
ID: 33498588
If you want a simple 2D array, Boost's ublas library offers a simple to use matrix.
http://www.boost.org/doc/libs/1_38_0/libs/numeric/ublas/doc/matrix.htm

If you matrix is huge it is worth considering the sparse version.
http://www.boost.org/doc/libs/1_38_0/libs/numeric/ublas/doc/matrix_sparse.htm
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article will show you some of the more useful Standard Template Library (STL) algorithms through the use of working examples.  You will learn about how these algorithms fit into the STL architecture, how they work with STL containers, and why t…
Introduction This article is a continuation of the C/C++ Visual Studio Express debugger series. Part 1 provided a quick start guide in using the debugger. Part 2 focused on additional topics in breakpoints. As your assignments become a little more …
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…

707 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question