Qualified's blog

By Qualified, history, 4 years ago, In English

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

  • Vote: I like it
  • -1
  • Vote: I do not like it

| Write comment?
»
4 years ago, # |
  Vote: I like it -9 Vote: I do not like it

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

  • »
    »
    4 years ago, # ^ |
      Vote: I like it 0 Vote: I do not like it

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

»
4 years ago, # |
Rev. 2   Vote: I like it +7 Vote: I do not like it

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.