The use of 'hash' in C++

  • 2020-04-01 23:38:57
  • OfStack

When writing programs, we often have to output, such as printf, cout, when you want to output the value of the expression, and to output the form of the expression in front of it, sometimes a little trouble! Such as the cout < < "A * b (c-d) :" < < A * b (c-d) < < Endl; Sometimes expressions like "a*b(c-d):" are too lazy to write, or too many of them are a waste of time to write. We can use "#" to reduce the trouble!
It doesn't seem clear. For example:

#include <iostream> 
using namespace std;
#define P(EX) cout<<#EX<<":"<<EX<<endl; 
int main()
{
    int a[10];
    for (int i = 0; i < 10; i++)
        a[i] = i;
    int *ip = a;
    P(*ip);            
    P(*++ip);
    P(*(ip+5));
    int *ip2 = ip + 5;
    P(*ip2);
    P(*(ip2 - 4)); 
    P(*--ip2);
    P(ip2-ip);         //Pointers subtract, note: Pointers cannot be added
    return 0;
}

So let's do that, let's see how it works, so we don't have to write cout many times < < "* (ip2-4)" < < ":" < < * (ip2-4) < < Endl; These strings. Although Effective C++ recommends using const and inline instead of # define, "use a compiler instead of preprocessing," # define is often considered as if it were not part of the language itself. But we can use it without interfering with our own program.

Related articles: