0

In php you can create a matrix like $mat1 = array(); without knowing the size of the array and add elements in non sequential index like first $mat1[1][3] = x then $mat1[2][0] = y and so on. How can this be implemented in C++? Also same thing for a 1D array?

huskygrad
  • 1,257
  • 1
  • 10
  • 21

2 Answers2

4

What you have in PHP is an associative array; it's called std::map<> in C++. If you want an int -> int -> string map, you need:

std::map< int, std::map< int, std::string > > mat1;

Note that this does not preserve the order which the keys are inserted.

If you don't know the number of dimensions in advance, you might create a recursive data structure where each value is either of a value_type or a map.

lorro
  • 10,687
  • 23
  • 36
  • It is actually more technically correct to say that in PHP the implementation of an array is an ordered hashmap (_not really an array as in a vector_). So similar to C++ map, but with order of insertion preserved regardless of the key. In C++ a `map` sorts elements by key, which is not the case in PHP. – Sherif Aug 27 '16 at 14:12
  • @Sherif: note that an ordered hashmap is an associative array :). That said, the second and third sentence is important, so I've edited the answer. (btw. is there a simple, clean, pragmatic way to represent an ordered associative container in C++ w/o keeping two containers or rolling your own?) – lorro Aug 27 '16 at 14:32
  • Not necessarily, no. __Associative array__ is somewhat dubious. In PHP arrays share some characteristics of a vector/list and _some_ characteristics of a hashmap. For example, in PHP you are not required to supply a key, but keys also have no effect on the ordering of elements, access, or even memory allocation. This is also why PHP's implementation of the array is made up of 3 levels of indirection. The `hashtable`, the `bucket`, and the doubly linked list of `bucket`s. This addresses your last question (_without that 3rd level you get more resizing of the hash due to collision_). – Sherif Aug 27 '16 at 14:44
  • @Sherif: got your point. So you need a list/vector and an unordered_map (one having iterators to the other, other having elements) to exactly mimic it in C++, it doesn't get simpler than that. Also, since PHP is (well, was, some time ago) dynamically typed, that might be simulated using `boost::any<>`. – lorro Aug 27 '16 at 21:41
  • @lorro Thanks! I had thought about this but wasn't sure. And I tried checking boost but at first it seemed to be an overkill, although the documentation is not very clear to me. I will test this method and get back if I need anything else. – huskygrad Aug 29 '16 at 13:32
2

From the PHP manual

An array in PHP is actually an ordered map. A map is a type that associates values to keys. This type is optimized for several different uses; it can be treated as an array, list (vector), hash table (an implementation of a map), dictionary, collection, stack, queue, and probably more. As array values can be other arrays, trees and multidimensional arrays are also possible.

So check here : http://www.cplusplus.com/reference/map/map/ and : how to use stl::map as two dimension array

Community
  • 1
  • 1
William Jones
  • 204
  • 1
  • 5