Solved

array in c++

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

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 …
Jaspersoft Studio is a plugin for Eclipse that lets you create reports from a datasource.  In this article, we'll go over creating a report from a default template and setting up a datasource that connects to your database.
The viewer will learn how to synchronize PHP projects with a remote server in NetBeans IDE 8.0 for Windows.
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.

863 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

Need Help in Real-Time?

Connect with top rated Experts

28 Experts available now in Live!

Get 1:1 Help Now