Old but still C++ Tricks

I see lots of programmers write code like this one:


<span class="pln">pair</span><span class="pun">&lt;</span><span class="kwd">int</span><span class="pun">,</span> <span class="kwd">int</span><span class="pun">&gt;</span><span class="pln"> p</span><span class="pun">;</span><span class="pln">
vector</span><span class="str">&lt;int&gt;</span><span class="pln"> v</span><span class="pun">;</span>
<span class="com">// ...</span><span class="pln">
p </span><span class="pun">=</span><span class="pln"> make_pair</span><span class="pun">(</span><span class="lit">3</span><span class="pun">,</span> <span class="lit">4</span><span class="pun">);</span><span class="pln">
v</span><span class="pun">.</span><span class="pln">push_back</span><span class="pun">(</span><span class="lit">4</span><span class="pun">);</span><span class="pln"> v</span><span class="pun">.</span><span class="pln">push_back</span><span class="pun">(</span><span class="lit">5</span><span class="pun">);</span>

while you can just do this:


<span class="pln">pair</span><span class="pun">&lt;</span><span class="kwd">int</span><span class="pun">,</span> <span class="kwd">int</span><span class="pun">&gt;</span><span class="pln"> p</span><span class="pun">;</span><span class="pln">
vector</span><span class="str">&lt;int&gt;</span><span class="pln"> v</span><span class="pun">;</span>
<span class="com">// ...</span><span class="pln">
p </span><span class="pun">=</span> <span class="pun">{</span><span class="lit">3</span><span class="pun">,</span> <span class="lit">4</span><span class="pun">};</span><span class="pln">
v </span><span class="pun">=</span> <span class="pun">{</span><span class="lit">4</span><span class="pun">,</span> <span class="lit">5</span><span class="pun">};</span>

1. Assign value by a pair of {} to a container

I see lots of programmers write code like this one:


<span class="pln">pair</span><span class="pun">&lt;</span><span class="kwd">int</span><span class="pun">,</span> <span class="kwd">int</span><span class="pun">&gt;</span><span class="pln"> p</span><span class="pun">;</span>
<span class="com">// ...</span><span class="pln">
p </span><span class="pun">=</span><span class="pln"> make_pair</span><span class="pun">(</span><span class="lit">3</span><span class="pun">,</span> <span class="lit">4</span><span class="pun">);</span>

while you can just do this:


<span class="pln">pair</span><span class="pun">&lt;</span><span class="kwd">int</span><span class="pun">,</span> <span class="kwd">int</span><span class="pun">&gt;</span><span class="pln"> p</span><span class="pun">;</span>
<span class="com">// ...</span><span class="pln">
p </span><span class="pun">=</span> <span class="pun">{</span><span class="lit">3</span><span class="pun">,</span> <span class="lit">4</span><span class="pun">};</span>

even a more complex 

pair

<span class="pln">pair</span><span class="pun">&lt;</span><span class="kwd">int</span><span class="pun">,</span><span class="pln"> pair</span><span class="pun">&lt;</span><span class="kwd">char</span><span class="pun">,</span> <span class="kwd">long</span> <span class="kwd">long</span><span class="pun">&gt;</span> <span class="pun">&gt;</span><span class="pln"> p</span><span class="pun">;</span>
<span class="com">// ...</span><span class="pln">
p </span><span class="pun">=</span> <span class="pun">{</span><span class="lit">3</span><span class="pun">,</span> <span class="pun">{</span><span class="str">'a'</span><span class="pun">,</span> <span class="lit">8ll</span><span class="pun">}};</span>

What about 

vector

deque

set

 and other containers?


<span class="pln">vector</span><span class="str">&lt;int&gt;</span><span class="pln"> v</span><span class="pun">;</span><span class="pln">
v </span><span class="pun">=</span> <span class="pun">{</span><span class="lit">1</span><span class="pun">,</span> <span class="lit">2</span><span class="pun">,</span> <span class="lit">5</span><span class="pun">,</span> <span class="lit">2</span><span class="pun">};</span>
<span class="kwd">for</span> <span class="pun">(</span><span class="kwd">auto</span><span class="pln"> i</span><span class="pun">:</span><span class="pln"> v</span><span class="pun">)</span><span class="pln">
    cout </span><span class="pun">&lt;&lt;</span><span class="pln"> i </span><span class="pun">&lt;&lt;</span> <span class="str">' '</span><span class="pun">;</span><span class="pln">
cout </span><span class="pun">&lt;&lt;</span> <span class="str">'\n'</span><span class="pun">;</span>
<span class="com">// prints "1 2 5 2"</span><span class="pln">


deque</span><span class="pun">&lt;</span><span class="pln">vector</span><span class="pun">&lt;</span><span class="pln">pair</span><span class="pun">&lt;</span><span class="kwd">int</span><span class="pun">,</span> <span class="kwd">int</span><span class="pun">&gt;&gt;&gt;</span><span class="pln"> d</span><span class="pun">;</span><span class="pln">
d </span><span class="pun">=</span> <span class="pun">{{{</span><span class="lit">3</span><span class="pun">,</span> <span class="lit">4</span><span class="pun">},</span> <span class="pun">{</span><span class="lit">5</span><span class="pun">,</span> <span class="lit">6</span><span class="pun">}},</span> <span class="pun">{{</span><span class="lit">1</span><span class="pun">,</span> <span class="lit">2</span><span class="pun">},</span> <span class="pun">{</span><span class="lit">3</span><span class="pun">,</span> <span class="lit">4</span><span class="pun">}}};</span>
<span class="kwd">for</span> <span class="pun">(</span><span class="kwd">auto</span><span class="pln"> i</span><span class="pun">:</span><span class="pln"> d</span><span class="pun">)</span> <span class="pun">{</span>
    <span class="kwd">for</span> <span class="pun">(</span><span class="kwd">auto</span><span class="pln"> j</span><span class="pun">:</span><span class="pln"> i</span><span class="pun">)</span><span class="pln">
        cout </span><span class="pun">&lt;&lt;</span><span class="pln"> j</span><span class="pun">.</span><span class="pln">first </span><span class="pun">&lt;&lt;</span> <span class="str">' '</span> <span class="pun">&lt;&lt;</span><span class="pln"> j</span><span class="pun">.</span><span class="pln">second </span><span class="pun">&lt;&lt;</span> <span class="str">'\n'</span><span class="pun">;</span><span class="pln">
    cout </span><span class="pun">&lt;&lt;</span> <span class="str">"-\n"</span><span class="pun">;</span>
<span class="pun">}</span>
<span class="com">// prints "3 4</span>
<span class="com">//         5 6</span>
<span class="com">//         -</span>
<span class="com">//       1 2</span>
<span class="com">//       3 4</span>
<span class="com">//       -"</span>


<span class="kwd">set</span><span class="str">&lt;int&gt;</span><span class="pln"> s</span><span class="pun">;</span><span class="pln">
s </span><span class="pun">=</span> <span class="pun">{</span><span class="lit">4</span><span class="pun">,</span> <span class="lit">6</span><span class="pun">,</span> <span class="lit">2</span><span class="pun">,</span> <span class="lit">7</span><span class="pun">,</span> <span class="lit">4</span><span class="pun">};</span>
<span class="kwd">for</span> <span class="pun">(</span><span class="kwd">auto</span><span class="pln"> i</span><span class="pun">:</span><span class="pln"> s</span><span class="pun">)</span><span class="pln">
    cout </span><span class="pun">&lt;&lt;</span><span class="pln"> i </span><span class="pun">&lt;&lt;</span> <span class="str">' '</span><span class="pun">;</span><span class="pln">
cout </span><span class="pun">&lt;&lt;</span> <span class="str">'\n'</span><span class="pun">;</span>
<span class="com">// prints "2 4 6 7"</span><span class="pln">


list</span><span class="str">&lt;int&gt;</span><span class="pln"> l</span><span class="pun">;</span><span class="pln">
l </span><span class="pun">=</span> <span class="pun">{</span><span class="lit">5</span><span class="pun">,</span> <span class="lit">6</span><span class="pun">,</span> <span class="lit">9</span><span class="pun">,</span> <span class="lit">1</span><span class="pun">};</span>
<span class="kwd">for</span> <span class="pun">(</span><span class="kwd">auto</span><span class="pln"> i</span><span class="pun">:</span><span class="pln"> l</span><span class="pun">)</span><span class="pln">
    cout </span><span class="pun">&lt;&lt;</span><span class="pln"> i </span><span class="pun">&lt;&lt;</span> <span class="str">' '</span><span class="pun">;</span><span class="pln">
cout </span><span class="pun">&lt;&lt;</span> <span class="str">'\n'</span><span class="pun">;</span>
<span class="com">// prints "5 6 9 1"</span><span class="pln">


array</span><span class="pun">&lt;</span><span class="kwd">int</span><span class="pun">,</span> <span class="lit">4</span><span class="pun">&gt;</span><span class="pln"> a</span><span class="pun">;</span><span class="pln">
a </span><span class="pun">=</span> <span class="pun">{</span><span class="lit">5</span><span class="pun">,</span> <span class="lit">8</span><span class="pun">,</span> <span class="lit">9</span><span class="pun">,</span> <span class="lit">2</span><span class="pun">};</span>
<span class="kwd">for</span> <span class="pun">(</span><span class="kwd">auto</span><span class="pln"> i</span><span class="pun">:</span><span class="pln"> a</span><span class="pun">)</span><span class="pln">
    cout </span><span class="pun">&lt;&lt;</span><span class="pln"> i </span><span class="pun">&lt;&lt;</span> <span class="str">' '</span><span class="pun">;</span><span class="pln">
cout </span><span class="pun">&lt;&lt;</span> <span class="str">'\n'</span><span class="pun">;</span>
<span class="com">// prints "5 8 9 2"</span><span class="pln">


tuple</span><span class="pun">&lt;</span><span class="kwd">int</span><span class="pun">,</span> <span class="kwd">int</span><span class="pun">,</span> <span class="kwd">char</span><span class="pun">&gt;</span><span class="pln"> t</span><span class="pun">;</span><span class="pln">
t </span><span class="pun">=</span> <span class="pun">{</span><span class="lit">3</span><span class="pun">,</span> <span class="lit">4</span><span class="pun">,</span> <span class="str">'f'</span><span class="pun">};</span><span class="pln">
cout </span><span class="pun">&lt;&lt;</span> <span class="kwd">get</span><span class="pun">&lt;</span><span class="lit">2</span><span class="pun">&gt;(</span><span class="pln">t</span><span class="pun">)</span> <span class="pun">&lt;&lt;</span> <span class="str">'\n'</span><span class="pun">;</span>

Note that it doesn’t work for 

stack

 and 

queue

.

2. Name of argument in macros

You can use ‘#’ sign to get exact name of an argument passed to a macro:


<span class="com">#define</span><span class="pln"> what_is</span><span class="pun">(</span><span class="pln">x</span><span class="pun">)</span><span class="pln"> cerr </span><span class="pun">&lt;&lt;</span> <span class="com">#x &lt;&lt; " is " &lt;&lt; x &lt;&lt; endl;</span>
<span class="com">// ...</span>
<span class="kwd">int</span><span class="pln"> a_variable </span><span class="pun">=</span> <span class="lit">376</span><span class="pun">;</span><span class="pln">
what_is</span><span class="pun">(</span><span class="pln">a_variable</span><span class="pun">);</span>
<span class="com">// prints "a_variable is 376"</span><span class="pln">
what_is</span><span class="pun">(</span><span class="pln">a_variable </span><span class="pun">*</span> <span class="lit">2</span> <span class="pun">+</span> <span class="lit">1</span><span class="pun">)</span>
<span class="com">// prints "a_variable * 2 + 1 is 753"</span>

3. Get rid of those includes!

Simply use


<span class="com">#include</span> <span class="pun">&lt;</span><span class="pln">bits</span><span class="pun">/</span><span class="pln">stdc</span><span class="pun">++.</span><span class="pln">h</span><span class="pun">&gt;</span>

This library includes many of libraries we do need in contest like 

algorithm

iostream

vector

 and many more. Believe me you don’t need to include anything else!

4. Hidden function (not really hidden but not used often)

one)


<span class="pln">__gcd</span><span class="pun">(</span><span class="pln">value1</span><span class="pun">,</span><span class="pln"> value2</span><span class="pun">)</span>

You don’t need to code Euclidean Algorithm for a gcd function, from now on we can use. This function returns gcd of two numbers.

e.g. __gcd(18, 27) = 9.

two)


<span class="pln">__builtin_ffs</span><span class="pun">(</span><span class="pln">x</span><span class="pun">)</span>

This function returns 1 + least significant 1-bit of x. If x == 0, returns 0. Here x is 

int

, this function with suffix ‘l’ gets a 

long

 argument and with suffix ‘ll’ gets a 

long long

 argument.

e.g. __builtin_ffs(10) = 2 because 10 is ‘…10 1 0′ in base 2 and first 1-bit from right is at index 1 (0-based) and function returns 1 + index.

three)


<span class="pln">__builtin_clz</span><span class="pun">(</span><span class="pln">x</span><span class="pun">)</span>

This function returns number of leading 0-bits of x which starts from most significant bit position. x is 

unsigned int

 and like previous function this function with suffix ‘l gets a 

unsigned long

 argument and with suffix ‘ll’ gets a 

unsigned long long

 argument. If x == 0, returns an undefined value.

e.g. __builtin_clz(16) = 27 because 16 is ‘  10000′. Number of bits in a 

unsigned int

 is 32. so function returns 32 — 5 = 27.

four)


<span class="pln">__builtin_ctz</span><span class="pun">(</span><span class="pln">x</span><span class="pun">)</span>

This function returns number of trailing 0-bits of x which starts from least significant bit position. x is 

unsigned int

 and like previous function this function with suffix ‘l’ gets a 

unsigned long

 argument and with suffix ‘ll’ gets a 

unsigned long long

 argument. If x == 0, returns an undefined value.

e.g. __builtin_ctz(16) = 4 because 16 is ‘…1 0000 ‘. Number of trailing 0-bits is 4.

five)


<span class="pln">__builtin_popcount</span><span class="pun">(</span><span class="pln">x</span><span class="pun">)</span>

This function returns number of 1-bits of x. x is 

unsigned int

 and like previous function this function with suffix ‘l’ gets a 

unsigned long

 argument and with suffix ‘ll’ gets a 

unsigned long long

 argument. If x == 0, returns an undefined value.

e.g. __builtin_popcount(14) = 3 because 14 is ‘… 111 0′ and has three 1-bits.

Note: There are other 

__builtin

 functions too, but they are not as useful as these ones.

Note: Other functions are not unknown to bring them here but if you are interested to work with them, I suggest this website.

5. Variadic Functions and Macros

We can have a variadic function. I want to write a sum function which gets a number of ints, and returns sum of them. Look at the code below:


<span class="kwd">int</span><span class="pln"> sum</span><span class="pun">()</span> <span class="pun">{</span> <span class="kwd">return</span> <span class="lit">0</span><span class="pun">;</span> <span class="pun">}</span>

<span class="kwd">template</span><span class="pun">&lt;</span><span class="kwd">typename</span><span class="pun">...</span> <span class="typ">Args</span><span class="pun">&gt;</span>
<span class="kwd">int</span><span class="pln"> sum</span><span class="pun">(</span><span class="kwd">int</span><span class="pln"> a</span><span class="pun">,</span> <span class="typ">Args</span><span class="pun">...</span><span class="pln"> args</span><span class="pun">)</span> <span class="pun">{</span> <span class="kwd">return</span><span class="pln"> a </span><span class="pun">+</span><span class="pln"> sum</span><span class="pun">(</span><span class="pln">args</span><span class="pun">...);</span> <span class="pun">}</span>

<span class="kwd">int</span><span class="pln"> main</span><span class="pun">()</span> <span class="pun">{</span><span class="pln"> cout </span><span class="pun">&lt;&lt;</span><span class="pln"> sum</span><span class="pun">(</span><span class="lit">5</span><span class="pun">,</span> <span class="lit">7</span><span class="pun">,</span> <span class="lit">2</span><span class="pun">,</span> <span class="lit">2</span><span class="pun">)</span> <span class="pun">+</span><span class="pln"> sum</span><span class="pun">(</span><span class="lit">3</span><span class="pun">,</span> <span class="lit">4</span><span class="pun">);</span> <span class="com">/* prints "23" */</span> <span class="pun">}</span>

In the code above I used a template. sum(5, 7, 2, 2) becomes 5 + sum(7, 2, 2) then sum(7, 2, 2), itself, becomes 7 + sum(2, 2) and so on… I also declare another sum function which gets 0 arguments and returns 0.

I can even define a any-type sum function:


<span class="kwd">int</span><span class="pln"> sum</span><span class="pun">()</span> <span class="pun">{</span> <span class="kwd">return</span> <span class="lit">0</span><span class="pun">;</span> <span class="pun">}</span>

<span class="kwd">template</span><span class="pun">&lt;</span><span class="kwd">typename</span><span class="pln"> T</span><span class="pun">,</span> <span class="kwd">typename</span><span class="pun">...</span> <span class="typ">Args</span><span class="pun">&gt;</span><span class="pln">
T sum</span><span class="pun">(</span><span class="pln">T a</span><span class="pun">,</span> <span class="typ">Args</span><span class="pun">...</span><span class="pln"> args</span><span class="pun">)</span> <span class="pun">{</span> <span class="kwd">return</span><span class="pln"> a </span><span class="pun">+</span><span class="pln"> sum</span><span class="pun">(</span><span class="pln">args</span><span class="pun">...);</span> <span class="pun">}</span>

<span class="kwd">int</span><span class="pln"> main</span><span class="pun">()</span> <span class="pun">{</span><span class="pln"> cout </span><span class="pun">&lt;&lt;</span><span class="pln"> sum</span><span class="pun">(</span><span class="lit">5</span><span class="pun">,</span> <span class="lit">7</span><span class="pun">,</span> <span class="lit">2</span><span class="pun">,</span> <span class="lit">2</span><span class="pun">)</span> <span class="pun">+</span><span class="pln"> sum</span><span class="pun">(</span><span class="lit">3.14</span><span class="pun">,</span> <span class="lit">4.89</span><span class="pun">);</span> <span class="com">/* prints "24.03" */</span> <span class="pun">}</span>

Here, I just changed 

int

 to 

T

 and added 

typename T

 to my template.

In C++14 you can also use 

auto sum(T a, Args... args)

 in order to get sum of mixed types. (Thanks to slycelote and Corei13)

We can also use variadic macros:


<span class="com">#define</span><span class="pln"> a_macro</span><span class="pun">(</span><span class="pln">args</span><span class="pun">...)</span><span class="pln"> sum</span><span class="pun">(</span><span class="pln">args</span><span class="pun">...)</span>

<span class="kwd">int</span><span class="pln"> sum</span><span class="pun">()</span> <span class="pun">{</span> <span class="kwd">return</span> <span class="lit">0</span><span class="pun">;</span> <span class="pun">}</span>

<span class="kwd">template</span><span class="pun">&lt;</span><span class="kwd">typename</span><span class="pln"> T</span><span class="pun">,</span> <span class="kwd">typename</span><span class="pun">...</span> <span class="typ">Args</span><span class="pun">&gt;</span>
<span class="kwd">auto</span><span class="pln"> sum</span><span class="pun">(</span><span class="pln">T a</span><span class="pun">,</span> <span class="typ">Args</span><span class="pun">...</span><span class="pln"> args</span><span class="pun">)</span> <span class="pun">{</span> <span class="kwd">return</span><span class="pln"> a </span><span class="pun">+</span><span class="pln"> sum</span><span class="pun">(</span><span class="pln">args</span><span class="pun">...);</span> <span class="pun">}</span>

<span class="kwd">int</span><span class="pln"> main</span><span class="pun">()</span> <span class="pun">{</span><span class="pln"> cout </span><span class="pun">&lt;&lt;</span><span class="pln"> a_macro</span><span class="pun">(</span><span class="lit">5</span><span class="pun">,</span> <span class="lit">7</span><span class="pun">,</span> <span class="lit">2</span><span class="pun">,</span> <span class="lit">2</span><span class="pun">)</span> <span class="pun">+</span><span class="pln"> a_macro</span><span class="pun">(</span><span class="lit">3.14</span><span class="pun">,</span> <span class="lit">4.89</span><span class="pun">);</span> <span class="com">/* prints "24.03" */</span> <span class="pun">}</span>

Using these 2, we can have a great debugging function: (thanks to Igorjan94) — Updated!


<span class="com">#include</span> <span class="pun">&lt;</span><span class="pln">bits</span><span class="pun">/</span><span class="pln">stdc</span><span class="pun">++.</span><span class="pln">h</span><span class="pun">&gt;</span>

<span class="kwd">using</span> <span class="kwd">namespace</span><span class="pln"> std</span><span class="pun">;</span>

<span class="com">#define</span><span class="pln"> error</span><span class="pun">(</span><span class="pln">args</span><span class="pun">...)</span> <span class="pun">{</span> <span class="kwd">string</span><span class="pln"> _s </span><span class="pun">=</span> <span class="com">#args; replace(_s.begin(), _s.end(), ',', ' '); stringstream _ss(_s); istream_iterator&lt;string&gt; _it(_ss); err(_it, args); }</span>

<span class="kwd">void</span><span class="pln"> err</span><span class="pun">(</span><span class="pln">istream_iterator</span><span class="str">&lt;string&gt;</span><span class="pln"> it</span><span class="pun">)</span> <span class="pun">{}</span>
<span class="kwd">template</span><span class="pun">&lt;</span><span class="kwd">typename</span><span class="pln"> T</span><span class="pun">,</span> <span class="kwd">typename</span><span class="pun">...</span> <span class="typ">Args</span><span class="pun">&gt;</span>
<span class="kwd">void</span><span class="pln"> err</span><span class="pun">(</span><span class="pln">istream_iterator</span><span class="str">&lt;string&gt;</span><span class="pln"> it</span><span class="pun">,</span><span class="pln"> T a</span><span class="pun">,</span> <span class="typ">Args</span><span class="pun">...</span><span class="pln"> args</span><span class="pun">)</span> <span class="pun">{</span><span class="pln">
    cerr </span><span class="pun">&lt;&lt;</span> <span class="pun">*</span><span class="pln">it </span><span class="pun">&lt;&lt;</span> <span class="str">" = "</span> <span class="pun">&lt;&lt;</span><span class="pln"> a </span><span class="pun">&lt;&lt;</span><span class="pln"> endl</span><span class="pun">;</span><span class="pln">
    err</span><span class="pun">(++</span><span class="pln">it</span><span class="pun">,</span><span class="pln"> args</span><span class="pun">...);</span>
<span class="pun">}</span>

<span class="kwd">int</span><span class="pln"> main</span><span class="pun">()</span> <span class="pun">{</span>
    <span class="kwd">int</span><span class="pln"> a </span><span class="pun">=</span> <span class="lit">4</span><span class="pun">,</span><span class="pln"> b </span><span class="pun">=</span> <span class="lit">8</span><span class="pun">,</span><span class="pln"> c </span><span class="pun">=</span> <span class="lit">9</span><span class="pun">;</span><span class="pln">
    error</span><span class="pun">(</span><span class="pln">a</span><span class="pun">,</span><span class="pln"> b</span><span class="pun">,</span><span class="pln"> c</span><span class="pun">);</span>
<span class="pun">}</span>

Output:


<span class="pln">a </span><span class="pun">=</span> <span class="lit">4</span><span class="pln">
b </span><span class="pun">=</span> <span class="lit">8</span><span class="pln">
c </span><span class="pun">=</span> <span class="lit">9</span>

This function helps a lot in debugging.

6. Here is C++0x in CF, why still C++?

Variadic functions also belong to C++11 or C++0x, In this section I want to show you some great features of C++11.

one) Range-based For-loop

Here is a piece of an old code:


<span class="kwd">set</span><span class="str">&lt;int&gt;</span><span class="pln"> s </span><span class="pun">=</span> <span class="pun">{</span><span class="lit">8</span><span class="pun">,</span> <span class="lit">2</span><span class="pun">,</span> <span class="lit">3</span><span class="pun">,</span> <span class="lit">1</span><span class="pun">};</span>
<span class="kwd">for</span> <span class="pun">(</span><span class="kwd">set</span><span class="str">&lt;int&gt;</span><span class="pun">::</span><span class="pln">iterator it </span><span class="pun">=</span><span class="pln"> s</span><span class="pun">.</span><span class="kwd">begin</span><span class="pun">();</span><span class="pln"> it </span><span class="pun">!=</span><span class="pln"> s</span><span class="pun">.</span><span class="kwd">end</span><span class="pun">();</span> <span class="pun">++</span><span class="pln">it</span><span class="pun">)</span><span class="pln">
    cout </span><span class="pun">&lt;&lt;</span> <span class="pun">*</span><span class="pln">it </span><span class="pun">&lt;&lt;</span> <span class="str">' '</span><span class="pun">;</span>
<span class="com">// prints "1 2 3 8"</span>

Trust me, that’s a lot of code for that, just use this:


<span class="kwd">set</span><span class="str">&lt;int&gt;</span><span class="pln"> s </span><span class="pun">=</span> <span class="pun">{</span><span class="lit">8</span><span class="pun">,</span> <span class="lit">2</span><span class="pun">,</span> <span class="lit">3</span><span class="pun">,</span> <span class="lit">1</span><span class="pun">};</span>
<span class="kwd">for</span> <span class="pun">(</span><span class="kwd">auto</span><span class="pln"> it</span><span class="pun">:</span><span class="pln"> s</span><span class="pun">)</span><span class="pln">
    cout </span><span class="pun">&lt;&lt;</span><span class="pln"> it </span><span class="pun">&lt;&lt;</span> <span class="str">' '</span><span class="pun">;</span>
<span class="com">// prints "1 2 3 8"</span>

We can also change the values just change 

auto

 with 

auto &amp;

:


<span class="pln">vector</span><span class="str">&lt;int&gt;</span><span class="pln"> v </span><span class="pun">=</span> <span class="pun">{</span><span class="lit">8</span><span class="pun">,</span> <span class="lit">2</span><span class="pun">,</span> <span class="lit">3</span><span class="pun">,</span> <span class="lit">1</span><span class="pun">};</span>
<span class="kwd">for</span> <span class="pun">(</span><span class="kwd">auto</span> <span class="pun">&amp;</span><span class="pln">it</span><span class="pun">:</span><span class="pln"> v</span><span class="pun">)</span><span class="pln">
    it </span><span class="pun">*=</span> <span class="lit">2</span><span class="pun">;</span>
<span class="kwd">for</span> <span class="pun">(</span><span class="kwd">auto</span><span class="pln"> it</span><span class="pun">:</span><span class="pln"> v</span><span class="pun">)</span><span class="pln">
    cout </span><span class="pun">&lt;&lt;</span><span class="pln"> it </span><span class="pun">&lt;&lt;</span> <span class="str">' '</span><span class="pun">;</span>
<span class="com">// prints "16 4 6 2"</span>

two) The Power of 

auto

You don’t need to name the type you want to use, C++11 can infer it for you. If you need to loop over iterators of a set<pair<int, pair<int, int> > > from begin to end, you need to type 

set&lt;pair&lt;int, pair&lt;int, int&gt; &gt; &gt;::iterator

 for me it’s so suffering! just use auto it = s.begin()

also x.begin() and x.end() now are accessible using begin(x) and end(x).

There are more things. I think I said useful features. Maybe I add somethings else to post. If you know anything useful please share with Codeforces community 🙂

From Ximera‘s comment:

this code:


<span class="kwd">for</span><span class="pun">(</span><span class="pln">i </span><span class="pun">=</span> <span class="lit">1</span><span class="pun">;</span><span class="pln"> i </span><span class="pun">&lt;=</span><span class="pln"> n</span><span class="pun">;</span><span class="pln"> i</span><span class="pun">++)</span> <span class="pun">{</span>
    <span class="kwd">for</span><span class="pun">(</span><span class="pln">j </span><span class="pun">=</span> <span class="lit">1</span><span class="pun">;</span><span class="pln"> j </span><span class="pun">&lt;=</span><span class="pln"> m</span><span class="pun">;</span><span class="pln"> j</span><span class="pun">++)</span><span class="pln">
        cout </span><span class="pun">&lt;&lt;</span><span class="pln"> a</span><span class="pun">[</span><span class="pln">i</span><span class="pun">][</span><span class="pln">j</span><span class="pun">]</span> <span class="pun">&lt;&lt;</span> <span class="str">" "</span><span class="pun">;</span><span class="pln">
    cout </span><span class="pun">&lt;&lt;</span> <span class="str">"\n"</span><span class="pun">;</span>
<span class="pun">}</span>

is equivalent to this:


<span class="kwd">for</span><span class="pun">(</span><span class="pln">i </span><span class="pun">=</span> <span class="lit">1</span><span class="pun">;</span><span class="pln"> i </span><span class="pun">&lt;=</span><span class="pln"> n</span><span class="pun">;</span><span class="pln"> i</span><span class="pun">++)</span>
    <span class="kwd">for</span><span class="pun">(</span><span class="pln">j </span><span class="pun">=</span> <span class="lit">1</span><span class="pun">;</span><span class="pln"> j </span><span class="pun">&lt;=</span><span class="pln"> m</span><span class="pun">;</span><span class="pln"> j</span><span class="pun">++)</span><span class="pln">
        cout </span><span class="pun">&lt;&lt;</span><span class="pln"> a</span><span class="pun">[</span><span class="pln">i</span><span class="pun">][</span><span class="pln">j</span><span class="pun">]</span> <span class="pun">&lt;&lt;</span> <span class="str">" \n"</span><span class="pun">[</span><span class="pln">j </span><span class="pun">==</span><span class="pln"> m</span><span class="pun">];</span>

And here is the reason: 

" \n"

 is a 

char*

" \n"[0]

 is 

' '

 and 

" \n"[1]

 is 

'\n'

.

From technetium28‘s comment:

Usage of 

tie

 and 

emplace_back

:


<span class="com">#define</span><span class="pln"> mt make_tuple
</span><span class="com">#define</span><span class="pln"> eb emplace_back
</span><span class="kwd">typedef</span><span class="pln"> tuple</span><span class="pun">&lt;</span><span class="kwd">int</span><span class="pun">,</span><span class="kwd">int</span><span class="pun">,</span><span class="kwd">int</span><span class="pun">&gt;</span> <span class="typ">State</span><span class="pun">;</span> <span class="com">// operator&lt; defined</span>

<span class="kwd">int</span><span class="pln"> main</span><span class="pun">(){</span>
  <span class="kwd">int</span><span class="pln"> a</span><span class="pun">,</span><span class="pln">b</span><span class="pun">,</span><span class="pln">c</span><span class="pun">;</span><span class="pln">
  tie</span><span class="pun">(</span><span class="pln">a</span><span class="pun">,</span><span class="pln">b</span><span class="pun">,</span><span class="pln">c</span><span class="pun">)</span> <span class="pun">=</span><span class="pln"> mt</span><span class="pun">(</span><span class="lit">1</span><span class="pun">,</span><span class="lit">2</span><span class="pun">,</span><span class="lit">3</span><span class="pun">);</span> <span class="com">// assign</span><span class="pln">
  tie</span><span class="pun">(</span><span class="pln">a</span><span class="pun">,</span><span class="pln">b</span><span class="pun">)</span> <span class="pun">=</span><span class="pln"> mt</span><span class="pun">(</span><span class="pln">b</span><span class="pun">,</span><span class="pln">a</span><span class="pun">);</span> <span class="com">// swap(a,b)</span><span class="pln">

  vector</span><span class="pun">&lt;</span><span class="pln">pair</span><span class="pun">&lt;</span><span class="kwd">int</span><span class="pun">,</span><span class="kwd">int</span><span class="pun">&gt;&gt;</span><span class="pln"> v</span><span class="pun">;</span><span class="pln">
  v</span><span class="pun">.</span><span class="pln">eb</span><span class="pun">(</span><span class="pln">a</span><span class="pun">,</span><span class="pln">b</span><span class="pun">);</span> <span class="com">// shorter and faster than pb(mp(a,b))</span>

  <span class="com">// Dijkstra</span><span class="pln">
  priority_queue</span><span class="pun">&lt;</span><span class="typ">State</span><span class="pun">&gt;</span><span class="pln"> q</span><span class="pun">;</span><span class="pln">
  q</span><span class="pun">.</span><span class="pln">emplace</span><span class="pun">(</span><span class="lit">0</span><span class="pun">,</span><span class="pln">src</span><span class="pun">,-</span><span class="lit">1</span><span class="pun">);</span>
  <span class="kwd">while</span><span class="pun">(</span><span class="pln">q</span><span class="pun">.</span><span class="pln">size</span><span class="pun">()){</span>
    <span class="kwd">int</span><span class="pln"> dist</span><span class="pun">,</span><span class="pln"> node</span><span class="pun">,</span><span class="pln"> prev</span><span class="pun">;</span><span class="pln">
    tie</span><span class="pun">(</span><span class="pln">dist</span><span class="pun">,</span><span class="pln"> ode</span><span class="pun">,</span><span class="pln"> prev</span><span class="pun">)</span> <span class="pun">=</span><span class="pln"> q</span><span class="pun">.</span><span class="pln">top</span><span class="pun">();</span><span class="pln"> q</span><span class="pun">.</span><span class="pln">pop</span><span class="pun">();</span><span class="pln">
    dist </span><span class="pun">=</span> <span class="pun">-</span><span class="pln">dist</span><span class="pun">;</span>
    <span class="com">// ~~ find next state ~~</span><span class="pln">
    q</span><span class="pun">.</span><span class="pln">emplace</span><span class="pun">(-</span><span class="pln">new_dist</span><span class="pun">,</span><span class="pln"> new_node</span><span class="pun">,</span><span class="pln"> node</span><span class="pun">);</span>
  <span class="pun">}</span>
<span class="pun">}</span>

And that’s why 

emplace_back

 faster: 

emplace_back

 is faster than 

push_back

 ’cause it just construct value at the end of vector but 

push_back

 construct it somewhere else and then move it to the vector.

Also in the code above you can see how 

tie(args...)

 works. You can also use 

ignore

 keyword in 

tie

 to ignore a value:


<span class="pln">tuple</span><span class="pun">&lt;</span><span class="kwd">int</span><span class="pun">,</span> <span class="kwd">int</span><span class="pun">,</span> <span class="kwd">int</span><span class="pun">,</span> <span class="kwd">char</span><span class="pun">&gt;</span><span class="pln"> t </span><span class="pun">(</span><span class="lit">3</span><span class="pun">,</span> <span class="lit">4</span><span class="pun">,</span> <span class="lit">5</span><span class="pun">,</span> <span class="str">'g'</span><span class="pun">);</span>
<span class="kwd">int</span><span class="pln"> a</span><span class="pun">,</span><span class="pln"> b</span><span class="pun">;</span><span class="pln">
tie</span><span class="pun">(</span><span class="pln">b</span><span class="pun">,</span><span class="pln"> ignore</span><span class="pun">,</span><span class="pln"> a</span><span class="pun">,</span><span class="pln"> ignore</span><span class="pun">)</span> <span class="pun">=</span><span class="pln"> t</span><span class="pun">;</span><span class="pln">
cout </span><span class="pun">&lt;&lt;</span><span class="pln"> a </span><span class="pun">&lt;&lt;</span> <span class="str">' '</span> <span class="pun">&lt;&lt;</span><span class="pln"> b </span><span class="pun">&lt;&lt;</span> <span class="str">'\n'</span><span class="pun">;</span>

Output: 

5 3

I use this macro and I love it:


<span class="com">#define</span><span class="pln"> rep</span><span class="pun">(</span><span class="pln">i</span><span class="pun">,</span> <span class="kwd">begin</span><span class="pun">,</span> <span class="kwd">end</span><span class="pun">)</span> <span class="kwd">for</span> <span class="pun">(</span><span class="pln">__typeof</span><span class="pun">(</span><span class="kwd">end</span><span class="pun">)</span><span class="pln"> i </span><span class="pun">=</span> <span class="pun">(</span><span class="kwd">begin</span><span class="pun">)</span> <span class="pun">-</span> <span class="pun">((</span><span class="kwd">begin</span><span class="pun">)</span> <span class="pun">&gt;</span> <span class="pun">(</span><span class="kwd">end</span><span class="pun">));</span><span class="pln"> i </span><span class="pun">!=</span> <span class="pun">(</span><span class="kwd">end</span><span class="pun">)</span> <span class="pun">-</span> <span class="pun">((</span><span class="kwd">begin</span><span class="pun">)</span> <span class="pun">&gt;</span> <span class="pun">(</span><span class="kwd">end</span><span class="pun">));</span><span class="pln"> i </span><span class="pun">+=</span> <span class="lit">1</span> <span class="pun">-</span> <span class="lit">2</span> <span class="pun">*</span> <span class="pun">((</span><span class="kwd">begin</span><span class="pun">)</span> <span class="pun">&gt;</span> <span class="pun">(</span><span class="kwd">end</span><span class="pun">)))</span>

First of all, you don’t need to name the type you want to use. Second of all it goes forwards and backwards based on (begin > end) condition. e.g. 

rep(i, 1, 10)

 is 1, 2, …, 8, 9 and 

rep(i, 10, 1)

 is 9, 8, …, 2, 1

It works well with different types e.g.


<span class="pln">vector</span><span class="str">&lt;int&gt;</span><span class="pln"> v </span><span class="pun">=</span> <span class="pun">{</span><span class="lit">4</span><span class="pun">,</span> <span class="lit">5</span><span class="pun">,</span> <span class="lit">6</span><span class="pun">,</span> <span class="lit">4</span><span class="pun">,</span> <span class="lit">8</span><span class="pun">};</span><span class="pln">
rep</span><span class="pun">(</span><span class="pln">it</span><span class="pun">,</span> <span class="kwd">end</span><span class="pun">(</span><span class="pln">v</span><span class="pun">),</span> <span class="kwd">begin</span><span class="pun">(</span><span class="pln">v</span><span class="pun">))</span><span class="pln">
    cout </span><span class="pun">&lt;&lt;</span> <span class="pun">*</span><span class="pln">it </span><span class="pun">&lt;&lt;</span> <span class="str">' '</span><span class="pun">;</span>
<span class="com">// prints "8 4 6 5 4"</span>

Also there is another great feature of C++11, lambda functions!

Lambdas are like other languages’ closure. It defines like this:


<span class="pun">[</span><span class="pln">capture list</span><span class="pun">](</span><span class="pln">parameters</span><span class="pun">)</span> <span class="pun">-&gt;</span> <span class="kwd">return</span><span class="pln"> value </span><span class="pun">{</span><span class="pln"> body </span><span class="pun">}</span>

one) Capture List: simple! We don’t need it here, so just put 

[]

two) parameters: simple! e.g. int x, string s

three) return value: simple again! e.g. pair<int, int> which can be omitted most of the times (thanks to Jacob)

four) body: contains function bodies, and returns return value.

e.g.


<span class="kwd">auto</span><span class="pln"> f </span><span class="pun">=</span> <span class="pun">[]</span> <span class="pun">(</span><span class="kwd">int</span><span class="pln"> a</span><span class="pun">,</span> <span class="kwd">int</span><span class="pln"> b</span><span class="pun">)</span> <span class="pun">-&gt;</span> <span class="kwd">int</span> <span class="pun">{</span> <span class="kwd">return</span><span class="pln"> a </span><span class="pun">+</span><span class="pln"> b</span><span class="pun">;</span> <span class="pun">};</span><span class="pln">
cout </span><span class="pun">&lt;&lt;</span><span class="pln"> f</span><span class="pun">(</span><span class="lit">1</span><span class="pun">,</span> <span class="lit">2</span><span class="pun">);</span> <span class="com">// prints "3"</span>

You can use lambdas in 

for_each

sort

 and many more STL functions:


<span class="pln">vector</span><span class="str">&lt;int&gt;</span><span class="pln"> v </span><span class="pun">=</span> <span class="pun">{</span><span class="lit">3</span><span class="pun">,</span> <span class="lit">1</span><span class="pun">,</span> <span class="lit">2</span><span class="pun">,</span> <span class="lit">1</span><span class="pun">,</span> <span class="lit">8</span><span class="pun">};</span><span class="pln">
sort</span><span class="pun">(</span><span class="kwd">begin</span><span class="pun">(</span><span class="pln">v</span><span class="pun">),</span> <span class="kwd">end</span><span class="pun">(</span><span class="pln">v</span><span class="pun">),</span> <span class="pun">[]</span> <span class="pun">(</span><span class="kwd">int</span><span class="pln"> a</span><span class="pun">,</span> <span class="kwd">int</span><span class="pln"> b</span><span class="pun">)</span> <span class="pun">{</span> <span class="kwd">return</span><span class="pln"> a </span><span class="pun">&gt;</span><span class="pln"> b</span><span class="pun">;</span> <span class="pun">});</span>
<span class="kwd">for</span> <span class="pun">(</span><span class="kwd">auto</span><span class="pln"> i</span><span class="pun">:</span><span class="pln"> v</span><span class="pun">)</span><span class="pln"> cout </span><span class="pun">&lt;&lt;</span><span class="pln"> i </span><span class="pun">&lt;&lt;</span> <span class="str">' '</span><span class="pun">;</span>

Output:


<span class="lit">8</span> <span class="lit">3</span> <span class="lit">2</span> <span class="lit">1</span> <span class="lit">1</span>

From Igorjan94‘s comment:

Usage of 

move

:

When you work with STL containers like 

vector

, you can use 

move

 function to just move container, not to copy it all.


<span class="pln">vector</span><span class="str">&lt;int&gt;</span><span class="pln"> v </span><span class="pun">=</span> <span class="pun">{</span><span class="lit">1</span><span class="pun">,</span> <span class="lit">2</span><span class="pun">,</span> <span class="lit">3</span><span class="pun">,</span> <span class="lit">4</span><span class="pun">};</span><span class="pln">
vector</span><span class="str">&lt;int&gt;</span><span class="pln"> w </span><span class="pun">=</span><span class="pln"> move</span><span class="pun">(</span><span class="pln">v</span><span class="pun">);</span><span class="pln">

cout </span><span class="pun">&lt;&lt;</span> <span class="str">"v: "</span><span class="pun">;</span>
<span class="kwd">for</span> <span class="pun">(</span><span class="kwd">auto</span><span class="pln"> i</span><span class="pun">:</span><span class="pln"> v</span><span class="pun">)</span><span class="pln">
    cout </span><span class="pun">&lt;&lt;</span><span class="pln"> i </span><span class="pun">&lt;&lt;</span> <span class="str">' '</span><span class="pun">;</span><span class="pln">

cout </span><span class="pun">&lt;&lt;</span> <span class="str">"\nw: "</span><span class="pun">;</span>
<span class="kwd">for</span> <span class="pun">(</span><span class="kwd">auto</span><span class="pln"> i</span><span class="pun">:</span><span class="pln"> w</span><span class="pun">)</span><span class="pln">
    cout </span><span class="pun">&lt;&lt;</span><span class="pln"> i </span><span class="pun">&lt;&lt;</span> <span class="str">' '</span><span class="pun">;</span>

Output:


<span class="pln">v</span><span class="pun">:</span><span class="pln">
w</span><span class="pun">:</span> <span class="lit">1</span> <span class="lit">2</span> <span class="lit">3</span> <span class="lit">4</span>

As you can see 

v

 moved to 

w

 and not copied.

7. C++0x Strings

one) Raw Strings (From IvayloS‘s comment)

You can have UTF-8 strings, Raw strings and more. Here I want to show raw strings. We define a raw string as below:


<span class="kwd">string</span><span class="pln"> s </span><span class="pun">=</span><span class="pln"> R</span><span class="str">"(Hello, World!)"</span><span class="pun">;</span> <span class="com">// Stored: "Hello, World!"</span>

A raw string skips all escape characters like 

\n

 or 

"

. e.g.


<span class="kwd">string</span><span class="pln"> str </span><span class="pun">=</span> <span class="str">"Hello\tWorld\n"</span><span class="pun">;</span>
<span class="kwd">string</span><span class="pln"> r_str </span><span class="pun">=</span><span class="pln"> R</span><span class="str">"(Hello\tWorld\n)"</span><span class="pun">;</span><span class="pln">
cout </span><span class="pun">&lt;&lt;</span><span class="pln"> str </span><span class="pun">&lt;&lt;</span><span class="pln"> r_str</span><span class="pun">;</span>

Output:


<span class="typ">Hello</span>  <span class="typ">World</span>
<span class="typ">Hello</span><span class="pln">\t</span><span class="typ">World</span><span class="pln">\n</span>

You can also have multiple line raw string:


<span class="kwd">string</span><span class="pln"> r_str </span><span class="pun">=</span><span class="pln">
R</span><span class="str">"(Dear Programmers,
I'm using C++11
Regards, Swift!)"</span><span class="pun">;</span><span class="pln">
cout </span><span class="pun">&lt;&lt;</span><span class="pln"> r_str</span><span class="pun">;</span>

Output:


<span class="typ">Dear</span> <span class="typ">Programmer</span><span class="pun">,</span><span class="pln">
I</span><span class="str">'m using C++11
Regards, Swift!</span>

two) Regular Expressions (regex)

Regular expressions are useful tools in programming, we can define a regular expression by 

regex

 e.g. 

regex r = "[a-z]+";

. We will use raw string for them because sometimes they have 

\

 and other characters. Look at the example:


<span class="pln">regex email_pattern</span><span class="pun">(</span><span class="pln">R</span><span class="str">"(^[a-zA-Z0-9_.+-]+@[a-zA-Z0-9-]+\.[a-zA-Z0-9-.]+$)"</span><span class="pun">);</span> <span class="com">// This email pattern is not totally correct! It's correct for most emails.</span>

<span class="kwd">string</span><span class="pln">
valid_email</span><span class="pun">(</span><span class="str">"swift@codeforces.com"</span><span class="pun">),</span><span class="pln">
invalid_email</span><span class="pun">(</span><span class="str">"hello world"</span><span class="pun">);</span>

<span class="kwd">if</span> <span class="pun">(</span><span class="pln">regex_match</span><span class="pun">(</span><span class="pln">valid_email</span><span class="pun">,</span><span class="pln"> email_pattern</span><span class="pun">))</span><span class="pln">
    cout </span><span class="pun">&lt;&lt;</span><span class="pln"> valid_email </span><span class="pun">&lt;&lt;</span> <span class="str">" is valid\n"</span><span class="pun">;</span>
<span class="kwd">else</span><span class="pln">
    cout </span><span class="pun">&lt;&lt;</span><span class="pln"> valid_email </span><span class="pun">&lt;&lt;</span> <span class="str">" is invalid\n"</span><span class="pun">;</span>

<span class="kwd">if</span> <span class="pun">(</span><span class="pln">regex_match</span><span class="pun">(</span><span class="pln">invalid_email</span><span class="pun">,</span><span class="pln"> email_pattern</span><span class="pun">))</span><span class="pln">
    cout </span><span class="pun">&lt;&lt;</span><span class="pln"> invalid_email </span><span class="pun">&lt;&lt;</span> <span class="str">" is valid\n"</span><span class="pun">;</span>
<span class="kwd">else</span><span class="pln">
    cout </span><span class="pun">&lt;&lt;</span><span class="pln"> invalid_email </span><span class="pun">&lt;&lt;</span> <span class="str">" is invalid\n"</span><span class="pun">;</span>

Output:


<span class="pln">swift@codeforces</span><span class="pun">.</span><span class="pln">com </span><span class="kwd">is</span><span class="pln"> valid
hello world </span><span class="kwd">is</span><span class="pln"> invalid</span>

Note: You can learn Regex in this website.

three) User-defined literals

You already know literals from C++ like: 

0xA

1000ll

3.14f

 and so on…

Now you can have your own custom literals! Sounds great 🙂 So let’s see an example:


<span class="kwd">long</span> <span class="kwd">long</span> <span class="kwd">operator</span> <span class="str">""</span><span class="pln"> _m</span><span class="pun">(</span><span class="kwd">unsigned</span> <span class="kwd">long</span> <span class="kwd">long</span><span class="pln"> literal</span><span class="pun">)</span> <span class="pun">{</span>
    <span class="kwd">return</span><span class="pln"> literal</span><span class="pun">;</span>
<span class="pun">}</span>

<span class="kwd">long</span> <span class="kwd">double</span> <span class="kwd">operator</span> <span class="str">""</span><span class="pln"> _cm</span><span class="pun">(</span><span class="kwd">unsigned</span> <span class="kwd">long</span> <span class="kwd">long</span><span class="pln"> literal</span><span class="pun">)</span> <span class="pun">{</span>
    <span class="kwd">return</span><span class="pln"> literal </span><span class="pun">/</span> <span class="lit">100.0</span><span class="pun">;</span>
<span class="pun">}</span>

<span class="kwd">long</span> <span class="kwd">long</span> <span class="kwd">operator</span> <span class="str">""</span><span class="pln"> _km</span><span class="pun">(</span><span class="kwd">unsigned</span> <span class="kwd">long</span> <span class="kwd">long</span><span class="pln"> literal</span><span class="pun">)</span> <span class="pun">{</span>
    <span class="kwd">return</span><span class="pln"> literal </span><span class="pun">*</span> <span class="lit">1000</span><span class="pun">;</span>
<span class="pun">}</span>

<span class="kwd">int</span><span class="pln"> main</span><span class="pun">()</span> <span class="pun">{</span>
    <span class="com">// See results in meter:</span><span class="pln">
    cout </span><span class="pun">&lt;&lt;</span> <span class="lit">250</span><span class="pln">_m </span><span class="pun">&lt;&lt;</span> <span class="str">" meters \n"</span><span class="pun">;</span> <span class="com">// Prints 250 meters</span><span class="pln">
    cout </span><span class="pun">&lt;&lt;</span> <span class="lit">12</span><span class="pln">_km </span><span class="pun">&lt;&lt;</span> <span class="str">" meters \n"</span><span class="pun">;</span> <span class="com">// Prints 12000 meters</span><span class="pln">
    cout </span><span class="pun">&lt;&lt;</span> <span class="lit">421</span><span class="pln">_cm </span><span class="pun">&lt;&lt;</span> <span class="str">" meters \n"</span><span class="pun">;</span> <span class="com">// Prints 4.21 meters</span>
<span class="pun">}</span>

Note that a literal should start with an underscore (

_

). We declare a new literal by this pattern:


<span class="pun">[</span><span class="pln">returnType</span><span class="pun">]</span> <span class="kwd">operator</span> <span class="str">""</span><span class="pln"> _</span><span class="pun">[</span><span class="pln">name</span><span class="pun">]([</span><span class="pln">parameters</span><span class="pun">])</span> <span class="pun">{</span> <span class="pun">[</span><span class="pln">body</span><span class="pun">]</span> <span class="pun">}</span>

note that parameters only can be one of these:

(const char *)
(unsigned long long int)
(long double)
(char)
(wchar_t)
(char16_t)
(char32_t)
(const char *, size_t)
(const wchar_t *, size_t)
(const char16_t *, size_t)
(const char32_t *, size_t)

Literals also can used with templates.

РубрикиБез рубрики

Добавить комментарий

Ваш адрес email не будет опубликован. Обязательные поля помечены *