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.

6
  • @d4rk4ng31 when people input vector of pairs most people do: a.push_back({b,c}) but i've had this problem for a while and instead of {} I always had to write "make_pair" because I received that error. Do you know how I can replace this make_pair in this case? Commented Jul 9, 2020 at 6:56
  • Why am I receiving down votes? Commented Jul 9, 2020 at 7:23
  • Hmm, for me it works fine Commented Jul 9, 2020 at 7:35
  • I have no idea what b[i] = {a,i}; suppose to do. It doesn't have a seance. IMO it should be just b[i] = a; Commented Jul 9, 2020 at 9:26
  • I'm guessing this is the same problem as stackoverflow.com/questions/62812376/… ? Commented Jul 9, 2020 at 12:11

1 Answer 1

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;

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.