Solved

array in c++

Posted on 2010-08-22
5
298 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
  • 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

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

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

When writing generic code, using template meta-programming techniques, it is sometimes useful to know if a type is convertible to another type. A good example of when this might be is if you are writing diagnostic instrumentation for code to generat…
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…
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.

803 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