Блог пользователя Qualified

Автор Qualified, история, 5 лет назад, По-английски

Is there a difference between std::array like array<int, 2> MyArray = {1, 2}; and int arr[2] = {1, 2}; Time complexities? Different functionalities?

  • Проголосовать: нравится
  • -1
  • Проголосовать: не нравится

»
5 лет назад, # |
  Проголосовать: нравится -9 Проголосовать: не нравится

No, std::array is used in something like

std::map<int, array<int, 2>>mp;
mp[1] = {3, 4};

In the other hand if you used std::map<int, vector<int>>mp; you have to assign the size before pushing values

  • »
    »
    5 лет назад, # ^ |
      Проголосовать: нравится 0 Проголосовать: не нравится

    So, how would one benefit over the other? Is std::array<int, 2> same thing as int arr[2];?

»
5 лет назад, # |
Rev. 2   Проголосовать: нравится +7 Проголосовать: не нравится

The benefit of std::array is that you can use it like a usual object. For example, you can assign std::arrays to each other, you can pass them to functions without them decaying to pointers, and you can return them from functions. You can also put them in other standard library containers. You can't do any of these with a plain old array.