preprocessor

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

#define NUM 10
#define STR "String"
#define SUM(x,y) ((x) + (y)) // macro definition by #define

//#undef NUM --> you can undef preprocessor

void main()
{
  
  cout << NUM;
  cout << STR;
  cout << SUM(2,4);
  
#if NUM <40
  cout << "under 40" <<endl;
#elif NUM > 40
  cout << "exceed 40" << endl;
#else
  cout << NUM << endl;
#endif

}
  • Code Execution
    • Preprocessor > Compiler > Linker > Generate Executable file
    • the preprocessor is executed before compiling
  • Preprocessing statements are defined by #.
  • Do not add a ; semicolon at the end of the preprocessing statement.

define

  • #define keyword : Declare content to be used as a keyword
  • Used for macro
    • When declaring macro, parentheses are processed considering operator precedence

undef

  • #undef : delete keywords declared with #define
#include<bits/stdc++.h>
using namespace std;

#define NUM 40

void main()
{
#ifdef NUM
  cout << NUM << "is exist" << endl;
#else 
  cout << NUM << "is not exist" << endl;
#endif
}

ifdef

  • #ifdef : if keyword is defined with #define, it is true
// header.h

#ifndef _TEST_H_
#define _TEST_H_
/// test code
#endif

ifndef

  • #ifndef: true if the keyword is not declared with a #define statement
    • Prevents duplicate definitions
    • avoid redefinition
    • avoid predefinition
    • EXAMPLE
    • #ifndef TEST_H : Check if the _TEST_H_ keyword is declared
    • #define TEST_H : If there is no _TEST_H_ keyword, define _TEST_H_
    • /// test code
    • #endif

    • When accessing the ///test code again later
    • Because _TEST_H_ is already defined by the condition(#ifndef TEST_H), the define statement below is not processed.
// header.h

#pragma once

pragma once

  • #pragma command: It performs a special function according to the command.
  • Compiler dependent
  • #pragma once : Include this header file only once

Assertion

If the argument expression of this macro with functional form compares equal to zero (i.e., the expression is false), a message is written to the standard error device and abort is called, terminating the program execution.

  • Executable check for a property that must be true

  • assert

Example

/* assert example */
#include <stdio.h>      /* printf */
#include <assert.h>     /* assert */

void print_number(int* myInt) {
  assert (myInt!=NULL);
  printf ("%d\n",*myInt);
}

int main ()
{
  int a=10;
  int * b = NULL;
  int * c = NULL;

  b=&a;

  print_number (b);
  print_number (c);

  return 0;
}
  • execution result : 10

  • Dijkstra’s algorithm is a representative shortest path search algorithm using dynamic programming.
    • Dynamic programming: When it is difficult to solve a large problem at once, it is a technique to divide it into several small problems
  • Commonly used most often in satellite GPS software, etc.
  • When finding one shortest distance, the shortest distance information obtained before is used as it is.
  • Dijkstra
    • Set the starting node
    • Store the minimum cost of each node based on the starting node
    • Select the least expensive node among the unvisited nodes
    • In consideration of the case of going to a specific node through the corresponding node (at this time, using the shortest distance information obtained before), the minimum cost is updated
  • EXAMPLE

#include<bits/stdc++.h>

using namespace std;

int n, m, a, b, e;

struct Graph{
	int vertex;
	int edge;
	Graph(int ve, int ed) {
		vertex = ve;
		edge = ed;
	}
	bool operator < (const Graph& cmpGraph) const {
		return edge > cmpGraph.edge;
	}
};

int main() {
	ios::sync_with_stdio(false);
	cin.tie(nullptr);
	
	freopen("input.txt", "rt", stdin);
	
	cin >> n >> m;
	vector<Graph> v[22];
	for(int i = 0; i < m; i++) {
		cin >> a >> b >> e;
		v[a].push_back({b,e});
	}

	vector<int> cost(22, 2147000000); // need to initialize all the data as 2147000000 by vector
	
	priority_queue<Graph> pq;
	pq.push({1, 0});
	cost[1] = 0;
	
	// below while code is Dijkstra algorithm
	while(!pq.empty()){
		int curNode = pq.top().vertex;
		int curCost = pq.top().edge;
		pq.pop();
		if(cost[curNode] < curCost) continue;
		for(int i = 0; i < v[curNode].size(); i++) {
			int nextNode = v[curNode][i].vertex;
			int nextCost = curCost + v[curNode][i].edge;
			if(nextCost < cost[nextNode]) {
				cost[nextNode] = nextCost;
				pq.push({nextNode, nextCost});
			}
		}
	}
	
	for(int i = 2 ; i <= n; i++) {
		if(cost[i] == 2147000000) cout <<"impossible";
		else cout << i << " : " << cost[i] << "\n";
	}

 	return 0;
}