3

I am new to C++ and programming. I would appreciate for some help with dynamic array sizing in C or C++.

ex :- I need to store values to array. ( vales can be changing )

set 1: 0,1,2,3

set 2 :- 0,1,2,3,4

set 3:- 0,1

set 4:- 0

So on I want them to store the value of set one in array process it and then store set 2 in same array process it and so on???

Please do reply back,

Thanks

user2829
  • 451
  • 1
  • 7
  • 21
  • 1
    Your question is a bit unclear. Please edit and use more clarifying language and text. This will render better answers to your question(s). – Victor Zamanian Dec 22 '10 at 04:32
  • Funny that you mention them as sets - another template you could use. – Michael Dorgan Dec 22 '10 at 05:52
  • Why do you say you're new to C++ then ask for (non-exclusivly) C examples? You want C++, you're learning C++. Also, you'd do well to [get a book](http://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list), you'll likely never learn as much any other way. – GManNickG Dec 22 '10 at 06:02

4 Answers4

9

The dynamic array in C++ is called std::vector<T> with T replaced with the type you want to store in it.

You'll have to put #include <vector> at the top of your program as well.

e.g.

#include <vector> // instruct the compiler to recognize std::vector

void your_function(std::vector<int> v)
{
  // Do whatever you want here
}

int main()
{
  std::vector<int> v; // a dynamic array of ints, empty for now

  v.push_back(0); // add 0 at the end (back side) of the array
  v.push_back(1); // add 1 at the end (back side) of the array
  v.push_back(2); // etc...
  v.push_back(3);
  your_function(v); // do something with v = {0, 1, 2, 3}

  v.clear();       // make v empty again
  v.push_back(10);
  v.push_back(11);
  your_function(v); // do something with v = {10, 11}
}

Note to more experienced programmers: yes, a lot of things can be improved here (e.g. const references), but I'm afraid that would only confuse a beginning programmer.

Sjoerd
  • 6,837
  • 31
  • 44
4

You can use a std::vector:

Vector containers are implemented as dynamic arrays; Just as regular arrays, vector containers have their elements stored in contiguous storage locations, which means that their elements can be accessed not only using iterators but also using offsets on regular pointers to elements.

Jacob Relkin
  • 161,348
  • 33
  • 346
  • 320
4

Seems like you want a std::vector<int>.

i_am_jorf
  • 53,608
  • 15
  • 131
  • 222
1

Your question isn't completely clear, but it sounds as if you start with one set of data, perform some task on that set which produces another set, a task that uses the new set and creates yet another?

If that is the case, you'll probably want to learn about swap.

e.g.

int main(void)
{
    std::vector<int> inputs, outputs;
    // push_back something into inputs

    // perform some task 1 on inputs which fills in outputs
    inputs.swap(outputs); // now the outputs of task 1 have become the inputs of task 2
    outputs.clear();

    // perform some task 2 on inputs which fills in outputs
    inputs.swap(outputs); // now the outputs of task 2 have become the inputs of task 3
    outputs.clear();

    // perform some task 3 and so on

    return 0;
}
Ben Voigt
  • 277,958
  • 43
  • 419
  • 720
  • yeah but adding more to it... I start scanning data bits on input port and depending upon the first bit i use a case statement to process it. for example input data is 01 03 02 02. use case1( if 1st bit == 1) and then store 02 03 04 in array.. similarly with other data sets too . if data is 02 03 04 01 then case2: and then store 03 04 01 and so on... please do reply.... – user2829 Dec 22 '10 at 04:50
  • @user2829: If you want to read values from a vector, you do that like `switch (input[0]) { case 1: ... break; case 2: ... break; }`. To add values to the end of a vector (making it longer), use `output.push_back(7);`. To overwrite a value (length stays the same), you can do `output[3] = 7;` – Ben Voigt Dec 22 '10 at 04:57