Solved

array in c++

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

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Find Visual Studio Tools 2 126
C++ mouse_event mouse look 7 112
How can I build my own IDE using ASP.NET MVC? 2 67
I could not set window to top 4 45
What is C++ STL?: STL stands for Standard Template Library and is a part of standard C++ libraries. It contains many useful data structures (containers) and algorithms, which can spare you a lot of the time. Today we will look at the STL Vector. …
Many modern programming languages support the concept of a property -- a class member that combines characteristics of both a data member and a method.  These are sometimes called "smart fields" because you can add logic that is applied automaticall…
The viewer will learn how to use and create new code templates in NetBeans IDE 8.0 for Windows.
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…

730 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