I have defined vector<int> as vi using #define in my C++ program. But when I am trying to use vi it is showing identifier "v" is undefinedC/C++(20).
I have installed mingw version: g++.exe (MinGW.org GCC-6.3.0-1) 6.3.0
I am using Visual studio code as my code editor.
Here is my code:
#include <bits/stdc++.h>
using namespace std;
#define ll long long;
#define vi vector<int>;
#define all(v) v.begin(), v.end();
#define tle ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
void solution() {
vi v; // here I am getting error
}
int main() {
tle;
int test_case;
cin >> test_case;
while (test_case--) {
solution();
}
return 0;
}
Why I am getting the error? How to solve this?
I am expecting to get solution or how to use #define in my C++ program
>Solution :
The issue you are facing is due to the incorrect usage of the #define macro in your code. When defining macros, you should not include a semicolon (;) at the end of the macro definition. The semicolon should be placed when you use the macro.
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define vi vector<int>
#define all(v) v.begin(), v.end()
#define tle ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
void solution() {
vi v; // No error now
}
int main() {
tle;
int test_case;
cin >> test_case;
while (test_case--) {
solution();
}
return 0;
}
By removing the semicolon after each #define, you have successfully defined the macros ll, vi, and all, and they can now be used correctly in your code. The semicolon should only be used when you use the macro, not when you define it.
With this change, the error "identifier ‘v’ is undefined" should be resolved, and your code should compile without any issues.