Skip to content

Instantly share code, notes, and snippets.

View derofim's full-sized avatar

Den derofim

  • Google
  • USA
View GitHub Profile
#include <iostream>
enum Color { RED, GREEN, BLUE };
Color r = RED, g = GREEN;
int x = 1;
int main()
{
if ( r == g ) { std::cout << "RED == GREEN"; } // false
if ( x == g ) { std::cout << "x=1 == GREEN"; } // true
x = RED;
}
#include <iostream>
enum class Color { RED, GREEN, BLUE };
Color redColor = Color::RED; // RED -> Color::RED
int num = ( int ) Color::GREEN; // RED -> ( int ) Color::RED
int main()
{
if ( redColor == Color::GREEN ) { std::cout << "RED == GREEN"; } // false
if ( num == (int) Color::GREEN ) { std::cout << "x=1 == GREEN"; } // true
}
#include <iostream>
using namespace std;
enum class Color : char { RED, GREEN, BLUE };
enum class Mark { ONE, TWO, THREE, FOUR, FIVE };
Color redColor = Color::RED;
Mark fiveMark = Mark::FIVE;
int main()
{
cout << sizeof ( Color ) << endl; // Вывод: 1
cout << sizeof ( redColor ) << endl; // Вывод: 1
#include <iostream>
const int n = 2, m = 3;
int main()
{
int arr[n][m] = { { 3, 5, 4 }, { 8, 6, 9 } };
for ( auto& row : arr )
{
for ( auto col : row )
{
std::cout << col << " "; // 3 5 4 8 6 9
int max ( int a, int b, int c )
{
int m = ( a > b ) ? a : b;
return ( m > c ) ? m : c;
}
#include <iostream>
int add ( int a, int b ) { return ( a + b ); }
int sub ( int a, int b ) { return ( a - b ); }
int main()
{
using namespace std;
int ( *pf ) ( int, int ) = add;
cout << pf ( 3, 2 ) << endl; // Вывод: 5
cout << pf ( 4, 5 ) << endl; // Вывод: 9
pf = sub;
#ifndef CONSTANTS_H
#define CONSTANTS_H
namespace Constants
{
const double pi ( 3.14159 );
const double avogadro ( 6.0221413e23 );
}
#endif
namespace Constants
{
const double pi ( 3.14159 );
const double avogadro ( 6.0221413e23 );
}
#ifndef CONSTANTS_H
#define CONSTANTS_H
namespace Constants
{
extern const double pi;
extern const double avogadro;
}
#endif
#include "constants.h"
double triplePi = 3 * Constants::pi;