Solved

array in c++

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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

C++ Properties One feature missing from standard C++ that you will find in many other Object Oriented Programming languages is something called a Property (http://www.experts-exchange.com/Programming/Languages/CPP/A_3912-Object-Properties-in-C.ht…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
THe viewer will learn how to use NetBeans IDE 8.0 for Windows to perform CRUD operations on a MySql database.
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.

747 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

14 Experts available now in Live!

Get 1:1 Help Now