dynamic-arrays

How to get size of dynamic array in C++ [duplicate]

 ̄綄美尐妖づ 提交于 2019-11-27 04:15:49
This question already has an answer here: How to get size c++ dynamic array 3 answers Code for dynamic array by entering size and storing it into "n" variable, but I want to get the array length from a template method and not using "n". int* a = NULL; // Pointer to int, initialize to nothing. int n; // Size needed for array cin >> n; // Read in the size a = new int[n]; // Allocate n ints and save ptr in a. for (int i=0; i<n; i++) { a[i] = 0; // Initialize all elements to zero. } . . . // Use a as a normal array delete [] a; // When done, free memory pointed to by a. a = NULL; // Clear a to

Pointer-to-pointer dynamic two-dimensional array

谁说胖子不能爱 提交于 2019-11-27 00:15:13
First timer on this website, so here goes.. I'm a newbie to C++ and I'm currently working through the book "Data structures using C++ 2nd ed, of D.S. Malik". In the book Malik offers two ways of creating a dynamic two-dimensional array. In the first method, you declare a variable to be an array of pointers, where each pointer is of type integer. ex. int *board[4]; ..and then use a for-loop to create the 'columns' while using the array of pointers as 'rows'. The second method, you use a pointer to a pointer. int **board; board = new int* [10]; etc. My question is this: which is the better

Range-based for loop on a dynamic array?

痴心易碎 提交于 2019-11-26 23:01:16
问题 There is a range-based for loop with the syntax: for(auto& i : array) It works with constant arrays but not with pointer based dynamic ones, like int *array = new int[size]; for(auto& i : array) cout<< i << endl; It gives errors and warnings about failure of substitution, for instance: Error] C:\Users\Siegfred\Documents\C-Free\Temp\Untitled2.cpp:16:16: error: no matching function for call to 'begin(int*&)' How do I use this new syntax with dynamic arrays? 回答1: To make use of the range-based

How to get priorly-unknown array as the output of a function in Fortran

天大地大妈咪最大 提交于 2019-11-26 22:54:20
问题 In Python : def select(x): y = [] for e in x: if e!=0: y.append(e) return y that works as: x = [1,0,2,0,0,3] select(x) [1,2,3] to be translated into Fortran : function select(x,n) result(y) implicit none integer:: x(n),n,i,j,y(?) j = 0 do i=1,n if (x(i)/=0) then j = j+1 y(j) = x(i) endif enddo end function The questions are in Fortran: how to declare y(?) ? how to declare predefined values for x how to avoid dimension info n for 1 if it is defined as y(n) the output will be: x = (/1,0,2,0,0,3

Variable Length Array (VLA) in C++ compilers

て烟熏妆下的殇ゞ 提交于 2019-11-26 22:36:10
As we already know, VLA (standardized in C99 ) are not part of the standard in C++. So the code below is "illegal" in C++ : void foo(int n) { int vla[n]; for (int i = 0; i < n; ++i) { vla[i] = i; } } Despite of that the compiler ( g++ and clang++ ) accepts the code as valid syntax, producing just a warning in case -pedantic flag is enable . ISO C++ forbids variable length array ‘vla’ [-Wvla] My questions are: Why does the compiler accept that declaration? The compiler cannot just reject an array in which length [is-no-know-at-compile-time] ? Is there a sort of compatibility syntax rule to

Fastest way to add an Item to an Array

一笑奈何 提交于 2019-11-26 20:09:55
问题 What is the fastest way to add a new item to an existing array? Dim arr As Integer() = {1, 2, 3} Dim newItem As Integer = 4 (I already know that when working with dynamic list of items you should rather use a List , ArrayList or similar IEnumerables . But what to do if you're stuck to legacy code that uses arrays?) What I've tried so far: ' A) converting to List, add item and convert back Dim list As List(Of Integer)(arr) list.Add(newItem) arr = list.ToArray() ' --> duration for adding 100

Why is it undefined behavior to delete[] an array of derived objects via a base pointer?

此生再无相见时 提交于 2019-11-26 18:36:28
I found the following snippet in the C++03 Standard under 5.3.5 [expr.delete] p3 : In the first alternative ( delete object ), if the static type of the object to be deleted is different from its dynamic type, the static type shall be a base class of the operand’s dynamic type and the static type shall have a virtual destructor or the behavior is undefined. In the second alternative ( delete array ) if the dynamic type of the object to be deleted differs from its static type, the behavior is undefined. Quick review on static and dynamic types: struct B{ virtual ~B(){} }; struct D : B{}; B* p =

How to properly work with dynamically-allocated multi-dimensional arrays in C++ [duplicate]

浪尽此生 提交于 2019-11-26 17:09:11
问题 This question already has an answer here: How do I declare a 2d array in C++ using new? 23 answers How do I define a dynamic multi-dimensional array in C++? For example, two-dimensional array? I tried using a pointer to pointer, but somehow it is failing. 回答1: The first thing one should realize that there is no multi-dimensional array support in C++, either as a language feature or standard library. So anything we can do within that is some emulation of it. How can we emulate, say, 2

c++ dynamic array initialization with declaration

て烟熏妆下的殇ゞ 提交于 2019-11-26 16:46:49
问题 I have function like this: void findScarf1(bool ** matrix, int m, int n, int radius, int connectivity); and in main function I create 2d dynamic array to pass in this function bool matrix[6][7] = { {0, 0, 1, 1, 1, 0, 0}, {0, 0, 1, 1, 1, 0, 0}, {0, 0, 1, 1, 1, 0, 0}, {0, 0, 1, 1, 1, 0, 0}, {0, 0, 1, 1, 1, 0, 0}, {0, 0, 1, 1, 1, 0, 0} }; The problem is: findScarf1(matrix, 6, 7, 3, 4); causes error C2664: 'findScarf1' : cannot convert parameter 1 from 'bool [6][7]' to 'bool **' How to initialize

How can I dynamically add items to a Java array?

微笑、不失礼 提交于 2019-11-26 14:27:43
In PHP, you can dynamically add elements to arrays by the following: $x = new Array(); $x[] = 1; $x[] = 2; After this, $x would be an array like this: {1,2} . Is there a way to do something similar in Java? Look at java.util.LinkedList or java.util.ArrayList List<Integer> x = new ArrayList<Integer>(); x.add(1); x.add(2); Arrays in Java have a fixed size, so you can't "add something at the end" as you could do in PHP. A bit similar to the PHP behaviour is this: int[] addElement(int[] org, int added) { int[] result = Arrays.copyOf(org, org.length +1); result[org.length] = added; return result; }