-7

If I were to form an array of integers in C++, I know that I could do it like this:

#include <iostream>
using namespace std;

int main()
{
int numbers[] = {1,2,3,4,5,6,7,8,9,10};
for(int i = 0; i < 10; i++){cout << i << " ";}

return 0;
}

Executing the above C++ code just prints the integers 1 to 10 with a space separating each integer.

So I don't access memory space outside of the array it's good practice to declare the integer size of the array, so it would normally be declared as int numbers[10] = {1,2,3,4,5,6,7,8,9,10}; ... but that's not necessary, as the array of integers can be accessed with a range based for loop.

However, this involves declaring an array, usually with the size, or if you don't give it the size (number of elements), then you have to immediately specify what the elements are in the array, and then C++ determines the size. This is a static array.

What I'm interested in is dynamic arrays in which you do not have to declare the size of the array to begin with or immediately fill it with elements, but you can later add items to the array. Considering an array of integers, I like to do this trick in Perl:

use strict;
use warnings;

my @numbers = (); # Declare an array without declaring its size or elements. 

for(my $i = 1; $i <= 10; $i++)
{
    push @numbers, $i; # Dynamically form the @numbers array with values 1 to 10. 
}

while(my $values= <@numbers>){print "$values";} # Print every value in the array of numbers. 

Executing the following Perl code produces this output:

1 2 3 4 5 6 7 8 9 10

This is the contents of the @numbers array from element 0 to 9. You can see that it was formed dynamically.

In Perl arrays are declared with the "@" symbol and singular variables are referred to with the "$" symbol. I didn't use Perl for any particular reason besides the fact that I know how to form dynamic arrays with it.

I would like to know if there is any way in which in C++ that dynamic arrays can be formed. Perhaps by using special libraries that include new functions?

Thanks!

xyz123
  • 651
  • 4
  • 19
  • 11
    Yes, it's called a `std::vector`. Go [get a good book and start reading](https://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list). The only way to learn C++ is to read a good book, and not keep asking questions about basic, infinitessimally small parts of what's arguably the most complicated general purpose programming language that exists today. – Sam Varshavchik Sep 05 '17 at 01:08
  • Do you need the start of the data to be at a consistent location? – Dave Newton Sep 05 '17 at 01:11
  • 2
    Use [std::vector](http://en.cppreference.com/w/cpp/container/vector). – O'Neil Sep 05 '17 at 01:20

1 Answers1

7

Use std::vector, which is designed for exactly this use case.

The (rough) translation of the Perl code you have given to C++ using std::vector would be something like this:

#include <iostream>
#include <vector>

int main() {
    // Declare a vector that can only contain int values.
    std::vector<int> numbers;

    // Put the numbers [1,10] into the array.
    for (int i = 1; i <= 10; ++i) {
        numbers.push_back(i);
    }

    // Display the contents of the vector.
    for (auto const &values : numbers) { std::cout << values << '\n'; }

    return 0;
}

(Live demo)

If you do not need the container allocations to be contiguous, std::deque might perform better under certain access/modification patterns, as it makes multiple allocations and therefore does not require that the existing contents be copied when the container runs out of capacity.

cdhowie
  • 158,093
  • 24
  • 286
  • 300
  • @xyz123 You are welcome. Have a look at the [containers library summary](http://en.cppreference.com/w/cpp/container) for more useful containers. (In C++ we use the term "containers" to refer to "objects that manage a collection of other objects in various structures," such as flat arrays (`std::vector`), linked lists (`std::list`), red-black tree (`std::map`), etc. The container and iterator concepts are particularly worth learning. The standard [algorithms library](http://en.cppreference.com/w/cpp/algorithm) has a lot of common search/sort/erase/etc. algorithms ready to go.) – cdhowie Sep 05 '17 at 01:38
  • @xyz123 The algorithms library in particular is extremely powerful when paired with lambda functions. Any time you are looping over a container, stop and look if one of the standard algorithms does what you're about to write. You can save a ton of lines of code, but -- more importantly -- time debugging your implementation when there's already a well-tested implementation sitting there, ready to use. – cdhowie Sep 05 '17 at 01:40