-4
#include <iostream>
#include <vector>
using namespace std;

#define sz(x) (int)size(x)

using ll = long long;
using vl = vector<ll>;

vl psum(const vl& a) {
    vl psum(sz(a) + 1);
    for (int i = 0; i < sz(a); ++i)
        psum[i + 1] = psum[i] + a[i];
    return psum;
}

int main() {
    int N, Q;
    cin >> N >> Q;
    vl a(N);
    for (ll& x : a)
        cin >> x;
    vl p = psum(a);
    for (int i = 0; i < Q; ++i) {
        int l, r;
        cin >> l >> r;
        cout << p[r] - p[l] << "\n";
    }
}

vl psum(sz(a) + 1); (Use of undeclared identifier 'size')

for (int i = 0; i < sz(a); ++i)

My question is, how is this sz function not identified?

Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770
  • 3
    `My question is how is this 'sz' function not identified?` It is, but `size` isn't. – tkausl Sep 09 '21 at 18:37
  • 1
    [What are your compiler options?](https://en.cppreference.com/w/cpp/iterator/size) – chris Sep 09 '21 at 18:44
  • Were you attempting to use `std::size`? Your use of `using namespace std;` obfuscates whether you were attempting to use the `std` namespace. – Drew Dormann Sep 09 '21 at 18:44
  • 3
    `#define sz(x) (int)size(x)` -- This looks like one of those terrible macros used by "competitive programming" websites. If the competition is timed, you've now spent wasted minutes on trying to get that macro to "work", when you could have easily typed in a few extra keystrokes without using that macro. – PaulMcKenzie Sep 09 '21 at 18:45
  • 2
    OT: If you *need* to write synonym macros like `#define ll long long` or `using ll = long long;`, you should invest in a keyboarding class. These "tricks" may make typing faster, but make maintenance more difficulty (including reading of program). They have negligible effect on build time, no effect on execution time or program space requirements. Just spell everything out and reduce the reliance on abbreviations. – Thomas Matthews Sep 09 '21 at 18:49
  • *My question is, how is this sz function not identified?* -- There is no `sz` function. It is a macro -- the actual function is `std::size`. – PaulMcKenzie Sep 09 '21 at 18:51
  • Or at least we HOPE it's `std::size`. Hard to be 100% sure once you bring `using namespace std;` into play. [One of the reasons you shouldn't use `using namespace std;`](https://stackoverflow.com/questions/1452721/why-is-using-namespace-std-considered-bad-practice) – user4581301 Sep 09 '21 at 19:32

1 Answers1

1

sz() is a macro, not a function.

But, it is clearly being defined. The error message says that size() is what is not defined, not sz().

Assuming you are trying to use std::size(), the std::vector overload of std::size() is defined in <vector> only in C++17 and later, so make sure you are configuring your compiler to use C++17, ie by specifying the -std=c++17 or similar flag, depending on your particular compiler.

Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770