Meron3r's blog

By Meron3r, history, 9 months ago, In English

Macros or also known as #define in c++ are very useful, but it takes a while to think of some great one that will save you a LOT of time, or a little, but it still makes it look better. Like, I was a python programmer and then when I switched to c++, I wasn't used to writing string, so I made a define like this:

#define str string

Here are my personal macros

#define sz size
#define yes cout << "YES"
#define no cout << "NO"
#define str string
#define ll long long
#define ull unsigned ll
#define uint unsigned int
#define pb push_back
#define pf push_front
#define ppb pop_back
#define ppf pop_front
#define vc vector
#define vci vector<int>
#define vcstr vector<str>
#define vcll vector<ll>
#define vcc vector<char>
#define fast ios::sync_with_stdio(false); cin.tie(nullptr); cout.tie(nullptr);
#define fi first
#define se second
#define be begin()
#define en end()

When you start solving a problem, you usually write long pieces of code. #define allows you to shorten and simplifie any ones code. If you want to make a macro for yourself you need to know how to make one first:

// here  we make a define for long long:
#define ll long long

After you do that you can use it anytime you want, for example:

ll a = 98210;

vector<ll> t;

unsigned ll factorial(x) {

if (x == 1 || x == 0) return 1;

else return x * factorial(x - 1);

}

Remember, defines vary from people, you can even get inspired by other peoples #define's too! ask a friend about his defines, they might also be useful for you too!

  • Vote: I like it
  • -21
  • Vote: I do not like it

»
9 months ago, # |
  Vote: I like it -11 Vote: I do not like it

Hi!

»
9 months ago, # |
  Vote: I like it 0 Vote: I do not like it

I don't recommend it, because if you're used to abbreviations and you don't have "define" on the test, you might forget the spelling. Of course, if you memorize these "define", it's a different story.

  • »
    »
    9 months ago, # ^ |
      Vote: I like it +8 Vote: I do not like it

    There is a downside to "define" :

    #define MAX(a,b) (((a)>(b))?(a):(b))
    

    When you write int x=MAX(add(1,1),add(1,2));, you will find the add(1,2) function, called twice.

    The "add" function is defined as follows:

    int add(int x,int y){
        return x+y;
    }
    
  • »
    »
    9 months ago, # ^ |
      Vote: I like it 0 Vote: I do not like it

    $$$《How$$$ $$$to$$$ $$$argue$$$ $$$with$$$ $$$others$$$ $$$with$$$ $$$reason》$$$ $$$-yzh$$$

»
9 months ago, # |
  Vote: I like it +1 Vote: I do not like it

Not useful.