dynamic-arrays

What does Wikipedia mean when it says the complexity of inserting an item at the end of a dynamic array is O(1) amortized?

拥有回忆 提交于 2019-12-12 19:54:01
问题 http://en.wikipedia.org/wiki/Dynamic_array#Performance What exactly does it mean? I thought inserting at the end would be O(n), as you'd have to allocate say, twice the space of the original array, and then move all the items to that location and finally insert the item. How is this O(1)? 回答1: Amortized O(1) efficiency means that the sum of the runtimes of n insertions will be O(n), even if any individual operation may take a lot longer. You are absolutely correct that appending an element

C - dynamic arrays

徘徊边缘 提交于 2019-12-12 16:15:33
问题 I don't quite understand how pointers work with C arrays. Here's some code I got: int arrayOne[] = {1, 2, 3}; int arrayTwo[] = {4, 5, 6, 7}; int **arrayThree = (int **)malloc(2 * sizeof(int)); arrayThree[0] = arrayOne; arrayThree[1] = arrayTwo; for (int i = 0; i < 2; i++) { int *array = arrayThree[i]; int length = sizeof(array) / sizeof(int); for (int j = 0; j < length; j++) printf("arrayThree[%d][%d] = %d\n", i, j, array[j]); } I would have expected this to output the following: arrayThree[0

What's the right way to make a stack (or other dynamically resizable vector-like thing) in Rust?

允我心安 提交于 2019-12-12 12:25:01
问题 Google turns up many links on old methods that have now been removed from the language, but I can't find a reference on what to do for Rust 0.6. I've just implemented a linked list, which I could easily repurpose into a stack, but I'd rather use some well-tested, robust, existing data structure from the standard library. 回答1: I would try, in order : a deque a list or a dlist a vec a mutable owned vector You can learn more about the containers on the wiki. 来源: https://stackoverflow.com

Ada: objects with variable-sized array attribute

蹲街弑〆低调 提交于 2019-12-12 11:46:15
问题 I want to create a tagged type inside a package that describes a 2D discrete space, with a size determined in running time. (context : implementation of a game of life) First way I found was genericity : generic Size : Natural; package Worlds is type World_Type is tagged private; type World is access World_Type'Class; subtype Coordinate is Positive range 1..Size; private type World_Array is array (Coordinate, Coordinate) of Boolean; type World_Type is tagged record Content : World_Array; end

Creating an array of ints whose size is based on the size of an NSArray

南楼画角 提交于 2019-12-12 04:53:23
问题 I'm trying to create and zero an array of int s based on a size that I get at runtime: size = [gamePiece.availableMoves.moves count]; //debugger shows size = 1; int array[size]; //debugger shows this as int[0] ! memset(array, 0, size); indexes = array; size and indexes are both ivars of this class: int size; int* indexes; I end up with a 0-length array, though. How can I create it with the size indicated by [gamePiece.availableMoves.moves count] ? 回答1: First of all, you can't do what you're

Increasing array size dynamically in C

烂漫一生 提交于 2019-12-12 02:47:20
问题 I need to increase length of 2 arrays according to user input. I'm using the code below. But output is not matching with user input. #include<stdio.h> int main() { int i=0, key=0, size[key], time[key]; while (key!=-1) { printf("Insert value for size : "); scanf("%d",&size[i]); printf("Insert value for time : "); scanf("%d",&time[i]); i++; printf("Run again Press-1 or Exit : "); scanf("%d",&key); } int x=0; for (x ; x<i ; x++) { printf("%d %d\n",size[x],time[x]); } return 0; } When user inputs

How to create and populate an array in Powershell based on a dynamic variable?

走远了吗. 提交于 2019-12-12 01:49:44
问题 I've been struggling with this for a couple of days, and I'm not sure how to conquer it. I need to do the following: Import a csv of users with the following values: ID, Name, Region Create an array based on the Region values that I can then use to populate with ID's and Names with that region, ie. Array_SEA AA_SCOM, Adam Andrews, SEA Array_OAK BB_SCOM, Bob Barker, OAK Here's the code I've got right now: $list2 = ipcsv .\TSE_Contact_List.csv | sort-object BU $arraylist =@() foreach ($vitem in

Creating dynamic variable array names and then adding object to them

孤者浪人 提交于 2019-12-11 16:46:46
问题 What I'm trying to do is create array variable names dynamically, and then with a loop, add the object to its relevant array based on the hash table value being equal to the counter variable. $hshSite = @{} # Values like this CO,1 NE,2 IA,3 $counter = $hshSite.count For($i = $counter; $i -gt 0; $i--) { New-Variable -Name "arr$i" -Value @() } If $counter = 3, I would create arrays $arr1, $arr2, $arr3 $csv = Import-CSV.... ForEach ($x in $csv) { #if $hshSite.Name = $x.location (ie CO), look up

Correctly allocating multi-dimensional arrays

为君一笑 提交于 2019-12-11 08:06:49
问题 The intent of this question is to provide a reference about how to correctly allocate multi-dimensional arrays dynamically in C. This is a topic often misunderstood and poorly explained even in some C programming books. Therefore even seasoned C programmers struggle to get it right. I have been taught from my programming teacher/book/tutorial that the correct way to dynamically allocate a multi-dimensional array is by using pointer-to-pointers. However, several high rep users on SO now tell

Adding to dynamic array

a 夏天 提交于 2019-12-11 04:36:41
问题 Disclaimer: Yes, I know about std::vector. I'm doing this for the sake of learning. I'm working on making a dynamic array class, and I'm trying to get add to work. template <class T> void Array<T>::add(T value) { T * tmp = new T[mCount]; for (int i = 0; i < mCount; i++) { tmp[i] = mData[i]; } mCount++; delete[] mData; mData = tmp; mData[mCount - 1] = value; } It works... sort of. The function works in adding the element, but then the program crashes when exiting. No errors, no nothing. It