0

Here I had defined the push_back an was using it in my code but don't know after doing this below task.

#include<bits/stdc++.h>
using namespace std;

#define pb push_back
vector<int> v[1001];

Here is the below error of this

prog.cpp:4:12: error: request for member ‘push_back’ in ‘v’, which is of non-class type ‘std::vector<int> [1001]’
 #define pb push_back
  • As a tip, use an IDE/editor which can help you autocomplete stuff like push_back instead of using defines. Maintaining such a code will become a nightmare down the line. – Jan Christoph Terasa Apr 26 '20 at 06:27
  • 1
    Just wait until `std::pb` is added to the library in C++ 27. – user4581301 Apr 26 '20 at 06:32
  • `pb` -- It's the same crazy macro used [here](https://stackoverflow.com/questions/61435548/codeforces-compiler-showing-weird-symbols) – PaulMcKenzie Apr 26 '20 at 06:42
  • [Why should I not #include ?](https://stackoverflow.com/questions/31816095/why-should-i-not-include-bits-stdc-h/31816096) and [Why is “using namespace std;” considered bad practice?](https://stackoverflow.com/q/1452721/364696) – David C. Rankin Apr 26 '20 at 06:46
  • @PaulMcKenzie -- Oh, that link is gooood... – David C. Rankin Apr 26 '20 at 06:48

1 Answers1

0

vector<int> v[1001]; is not a vector, it's an array of vectors. I'm guessing you meant this

vector<int> v;

which is a vector v of zero size. You can then increase the size by using push_back. But it's possible that you meant this

vector<int> v(1001);

which is a vector v of size 1001. You can also increase the size of this vector by using push_back, but since it's already has a size of 1001 I'm not sure you would want to.

Without seeing more code it's hard to be sure.

And why the macro? #define pb push_back, what is the point of that? Please let me know.

john
  • 85,011
  • 4
  • 57
  • 81