1

Im having some troubles using std::array.

I have an array called b, lets declare it like this: array<int,2>b[3];

Perhaps I would input it in a way like this:

for(int i = 0; i<3; i++){
    int a; cin >> a;
    b[i] = {a,i};
}

When I do this though, I get an error:

ar.cpp:30:8: error: expected expression
                b[i]={a, i};

Also lets say I want to input an vector<array<>>, lets define it as vector<array<int, 2>> ans;

I would probably do this:

int a = 10, b = 20;
ans.push_back({a,b});

I received another error stating :

 error: expected expression
  ans.push_back({a, j});

Note I put them as separate cases and separate variables

I tried doing methods like make_pair but that didn't work.

These errors are probably because I use c++17 and not 11. But I've tried for a very long time and I wasn't able to fix it. To help me fix this please go here: Visual Studio Code c++11 extension warning and lamda warnings

Can anyone suggest what I should do instead of this? Thank you.

Note perhaps a method similar to make_pair but for vector<array<>> is what im looking for? When I inputed vector of pairs I always used push_back and make_pair instead of {} or emplace_back because those methods didn't work because I always received errors like those.

Rainier1
  • 73
  • 6

1 Answers1

0

In std::array, the first template argument specifهes type of array. If you want to map an integer to another , you probably need to use std::map:

std::map<int, int> a;
a[3] = 10;

std::cout << a[3] << std::endl;

Ali Askari
  • 515
  • 3
  • 8