Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there an equivalent to C++'s std::vector in PHP?

Is there a lightweight replacement for PHP's array to be used when I don't need any of the associated array functionality? From what I know, array is a hash map internally, which is excessive and inefficient for storing a simple array of elements. If PHP had a class or programming construct similar to C++'s std::vector, it would be just great.

Reference: http://www.cplusplus.com/reference/vector/vector/

like image 586
Desmond Hume Avatar asked Jul 20 '13 13:07

Desmond Hume


People also ask

Are there vectors in PHP?

PHP in TeluguThe Vector is a sequence of values in a contiguous buffer that grows and shrinks automatically. It is the most efficient sequential structure because the value index is a direct mapping to its index in a buffer, and the growth factor is not bound to specific multiple or exponent.

What can I use instead of vector in C?

A rough equivalent of a C++ vector would be a resizing C array (to account for more elements than available). Ergo, the equivalent of an array of vectors would be an array of pointers (an array of arrays wouldn't cut it because of the resizing constraint). int* values[1000];

What is the difference between std :: array and std::vector?

Difference between std::vector and std::array in C++Vector is a sequential container to store elements and not index based. Array stores a fixed-size sequential collection of elements of the same type and it is index based. Vector is dynamic in nature so, size increases with insertion of elements.

Should I use std for vector?

Here is a rule of thumb: If you want to add elements to your container or remove elements from your container, use a std::vector; if not, use a std::array.


1 Answers

Have a look at SPL datastructures. An example is http://www.php.net/manual/en/class.splfixedarray.php which is faster than normal array.

http://www.php.net/manual/en/spl.datastructures.php

like image 171
bitWorking Avatar answered Oct 17 '22 12:10

bitWorking