0% found this document useful (0 votes)
9 views

C++11 tricks- Codeforces

The document discusses the advantages of using C++11 features in programming contests, highlighting improvements such as type inference with the 'auto' keyword, initializer lists, range-based for loops, and lambda functions. It emphasizes how these features can simplify code and enhance readability without sacrificing performance. Additionally, it touches on move semantics, which optimizes the return of non-pointer values by avoiding deep copies.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views

C++11 tricks- Codeforces

The document discusses the advantages of using C++11 features in programming contests, highlighting improvements such as type inference with the 'auto' keyword, initializer lists, range-based for loops, and lambda functions. It emphasizes how these features can simplify code and enhance readability without sacrificing performance. Additionally, it touches on move semantics, which optimizes the return of non-pointer values by avoiding deep copies.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 13

16/01/2023, 01:09 C++11 for programming contests...

- Codeforces

 |
GuyNEN | Logout
You have +393! Wow!

HOME TOP CATALOG CONTESTS GYM PROBLEMSET GROUPS RATING EDU API CALENDAR HELP

MUKEL BLOG TEAMS SUBMISSIONS GROUPS CONTESTS PROBLEMSETTING

mukel's blog → Pay attention

Before contest
C++11 for programming contests... Codeforces Round #845 (Div. 2)
6 days
By mukel, 9 years ago,

Updated: 30 December 2013 3:27 CEST (Added a brief introduction to Lambda Functions). → GuyNEN

Rating: 393
The new C++ standard, also known as C++11 and also as C++0x is here, with some sugars Contribution: 0
for programming contest. I'll update this thread soon with new contents (and better Settings
structure). You can use C++11 on Topcoder, Codeforces, HackerRank ... This thread is based Blog
Teams GuyNEN
on my own experience, so you don't need to dig on the whole C++11 specification to find
Submissions
some useful feature you can use in programming contests. Favourites
Talks
Contests
The "auto" keyword:
Type inference is included in many modern programming languages, and C++ is not behind,
the "auto" keyword works like "var" in C#, it only tells the compiler to infer the type for us: → Top rated
So, # User Rating
1 Benq 3813
map< string, pair< int, int > > somyLongTypeName = map< string, pair<
int, int > >(); 2 tourist 3768
3 maroonrk 3570
Can be shortened to, with no impact on speed, since the type is inferred at compile time: 4 Radewoosh 3535
5 fantasy 3526
auto longTypeNamesAreHistory = map< string, pair< int, int > >();
6 Um_nik 3523

Of course that a typedef could do the same, but using auto is faster (to type). 6 jiangly 3523
8 orzdevinwang 3441
Personally I think that one of the best use of auto is when dealing with iterators, for example: 9 cnnfls_csy 3427
10 zh0ukangyang 3423
for (map< string, pair< int, int > >::iterator it = m.begin(); it !=
Countries | Cities | Organizations View all →
m.end(); ++it)
{
/* do something with it */ → Top contributors
} # User Contrib.
1 awoo 180
Becomes:
1 -is-this-fft- 180

for (auto it = m.begin(); it != m.end(); ++it) 3 dario2994 167


{ 4 SecondThread 164
/* do something with it */ 5 Um_nik 163
}
5 maroonrk 163
7 adamant 162
Which is a great improvement, easier to write and read, and to maintain (imagine we change
one of the template arguments of m). 7 nor 162
7 kostka 162
Initializer lists: 10 YouKn0wWho 160
View all →
This is also a nice addition, and makes easier to initialize stl containers, let's a basic
example:
→ Find user
vector< int > primeDigits = {2, 3, 5, 7};
vector< string > days = {"Monday", "Tuesday", "Wednesday", "Thursday", Handle:

https://codeforces.com/blog/entry/10124 1/13
16/01/2023, 01:09 C++11 for programming contests... - Codeforces

"Friday", "Saturday", "Sunday"}; Find



pair< int, int > p = {1, 2}; // equivalent to make_pair(1, 2)

Maps also can be initialized: → Recent actions

map< int, string > numeral = { tourist → Codeforces Round #844 (Div. 1 +
Div. 2)
{0, "zero"},
Cheaters_Hunter → One Cheater becomes
{1, "one"}, expert and Specialist at the same round !!
{2, "two"},
{3, "three"}, -is-this-fft- → [Tutorial] Collection of little
techniques
{4, "four"},
{5, "five"}, n0sk1ll → Editorial for Hello 2023

{6, "six"}, XoXoHarsh → What kind of project can I


make on the topic algorithms which would
{7, "seven"},
also help me in CP?
{8, "eight"},
Wind_Eagle → Codeforces Round #843. One
{9, "nine"} of the contests authors decided to entertain
}; again

YPK → When to learn advanced algorithms?

Range based for loops: TheScrasse → [Tutorial] Number theory —


Storing information about multiples/divisors
C++11 also introduced a new way to iterate thourgh STL containers:
chokudai → AtCoder Beginner Contest 191
The old way (using auto for simplicity): Announcement

shivanshsingh9670 → Who knows why ?


for (auto it = container.begin(); it != container.end(); ++it)
HosseinYousefi → C++ Tricks
{
gepardo → Writing Contests in Assembly
/* do something with it */ Language?
}
xiaowu5933 → HDU--1115

chokudai → AtCoder Beginner Contest 285


Becomes: Announcement

brunomont → Can you find the time


for (auto x : container) complexity?
{
vito1036 → Croatian Open Competition in
cout << x << endl; Informatics (COCI) 2022/2023 — Round #3
}
Life_Is_A_Game → Happy New Year!

We can even modify the elements (note the &): POPtab → What does "Div. 1 + Div. 2,
based on VK Cup 2022 — Elimination
Round" means?
vector< int > numbers = {2, 3, 5, 7};
alittlemiddle → Help me pls
for (auto& x : numbers)
Rising-coder → Should I go back ?
x *= 2;
ChthollyNotaSeniorious → Polynomial Round
2022 (Div. 1 + Div. 2) Editorial
for (auto x : numbers)
cout << x << endl; Vedkribhu → Need Help CSES Graph
Problem: High Score

CristianoPenaldo → I need comfort


The above should multiply by 2 all the elements of container.
awoo → Educational Codeforces Round 85
Editorial
But we can go even further and pythonize the for loops as follows:
YouKn0wWho → [Tutorial] Common
Mistakes in Competitive Programming and
First let's define a number_iterator and number_range: How to Avoid Them

template<typename T> Detailed →

struct number_iterator : std::iterator<random_access_iterator_tag, T>{


T v;
number_iterator(T _v) : v(_v) {}
operator T&(){return v;}
T operator *() const {return v;}
};
template <typename T>
struct number_range {
T b,e;
number_range(T b, T e):b(b),e(e){}

https://codeforces.com/blog/entry/10124 2/13
16/01/2023, 01:09 C++11 for programming contests... - Codeforces

number_iterator<T> begin(){return b;}



number_iterator<T> end(){return e;}
};
/* make_pair like functions for our range type */
template<typename T> number_range<T> range(T e) {return number_range<T>
(0, e);}

// inclusive range
template<typename T> number_range<T> range(T b, T e) {return
number_range<T>(b, e);}

Now we can do something like:

for (auto i: range(1000))


sum += i;

or

for (auto i: range(10, 20))


cout << i << endl;

Nothing to worry about speed, is exactly the same when compiled with -O2 (tested on g++
4.8), which is logical since all the operations of the iterators are delegated to underlying type
T (int or long long). Until now, my experience tell me that the new range for loops are easier
to write/read without any speed penalty. Note that this is not so practical since we need to
implement our own iterator and range classes, but if we have a pre-written template, the
code becomes very clean and readable.

Another common example, (no more -> for (int i = 0; i < G[u].size(); ++i)... :

void dfs(int u, int from) {


for (auto v: G[u])
if (v != from)
dfs(v);
}

But we can go even further (with the help of lambdas) and implement binary search using the
well known lower_bound and upper_bound functions:

Let's implement a simple binary search to find the square root:

int findSqrt(int n) {
int lo = 0, hi = n;
while (lo <= hi) {
int mid = (lo + hi) / 2;
if (mid * mid <= n)
lo = mid + 1;
else
hi = mid - 1;
}
return lo - 1;
}

Using our number_iterator and lower_bound the above code becomes:

int findSqrt(int n) {
int lb = lower_bound(number_iterator<int>(0),
number_iterator<int>(n), 0,
[&] (int value, int /* ignored */ ) {
return value * value <= n;
}
);

https://codeforces.com/blog/entry/10124 3/13
16/01/2023, 01:09 C++11 for programming contests... - Codeforces

return lb - 1;

}

As you can see, no need to cast (or de-reference) the resulting iterator, we can also use long
long (not doubles obviously).

New array type:


C++11 brings a new array type which offer complete integration with the STL (not too useful
IMHO), the syntax is verbose for multidimensional arrays, but there is no penalty on speed:

auto arr = array< int, 10 >{5, 8, 1, 9, 0, 3, 4, 2, 7, 6}; // array of


10 ints

sort(arr.begin(), arr.end()); // yes, same as sort(arr, arr + n) for


normal arrays

arr[0] += arr[5]; // normal [] indexing

for (auto i: range(arr.size()))


{
/* do something */
}

auto matrix = array< array<double, 10>, 10>(); // 10 by 10 matrix (not


the same as double[10][10])

Initialization can be somehow particular, please refer to this for further details.

Lambda functions:
Before the new standard, C++ used functors, which are classes that simulates functions, still
functions were not 1st class constructs. The new standard introduced lambda functions (still
not 1st class citizens), and make the use of functions more pleasant than before. Let's see a
trivial example:

Generates a permutation sorted by heights[].

auto heights = vector< int >{ ...some values here... };

auto order = vector< int >(heights.size());

for (int i: range(heights.size()))


order[i] = i;

sort(order.begin(), order.end(),
[&] (const int& a, const& int b) -> bool {
return heights[a] < heights[b];
}
);

See also the binary search implemented using lower_bound in the "Range based for loops"
section.

The above is a quite common use case, but one may wonder why use a lambda instead of a
simple static function, the reason is quite simple, the cool part is that lambda functions can
capture other variables (by value or by reference, is even possible to specify which variables
to capture), allowing us to access to all visible variables in the scope. The old style functions
can only access global variables. The compiler can infer the return type in most cases. My
advice is to avoid recursive lambda functions. Lambdas are a huge advance, but definetely
not the same as in functional languages were functions are real 1st class constructs.

C++

https://codeforces.com/blog/entry/10124 4/13
16/01/2023, 01:09 C++11 for programming contests... - Codeforces

for_each(v.begin(), v.end(),

[&] (int x) {
cout << x << endl;
}
)

Scala

v foreach println

TODO: Better/more examples

Move semantics:
What is it and how it works?

C++03 and previous standards had a serious penalty when returning non-pointer values, eg.
vector< int >, map< string, int >, string?... since the whole value should be deep-copied by
value, move semantics is an optimization that avoid the copy of the whole structure and
integrates flawlessly to existing code. To explain better how it works (without entering in
technical details) let's try with a concrete example, let's assume we are returning a vector<
int > , instead of copy the vector when returning, it just "moves" (with the help of some
additional construct) what we are returning, the "move" is achieved by copying only the
necesary internal "data" of the vector, which consist in a pointer and a integer (size), that way
the whole data is NOT copied, only the necessary to have. Note that "move" is different from
copy, it just "moves" cleverly what is really important, resulting in a considerable boost. So
you can safely return vectors, maps, strings with a no performance overhead in a more
natural way that using pointers which can be harder to code, debug ...

Soon, benchmarks using the new move semantics feature...

More is coming, lambdas, benchmarks, regex, new pseudo-random number generators, bye
bye rand() ...

c++, programming, c++11, trick, implementation

+244 mukel 9 years ago 47

Comments (46) Show archived | Write comment?

9 years ago, # | +17

Very helpful tutorial. Thanks!

Btw. notice that C++11 is allowed at IOI 2014 ! :)


GaryYe
→ Reply

8 years ago, # ^ | +3

Also 2014 ACM-ICPC World Finals will switch the default C++ to
gnu++0x (C++0x plus GNU extensions)
→ Reply
nakeep

9 years ago, # | +16

I think here typo mistake .

int mid = (lo + hi) - 1;


Aziza
must be

int mid = (lo + hi) / 2;

→ Reply

https://codeforces.com/blog/entry/10124 5/13
16/01/2023, 01:09 C++11 for programming contests... - Codeforces

→ Reply

3 years ago, # ^ | -11

or maybe he wanna write : int mid = (lo + hi)>>1;


→ Reply

chaudhary_19

9 years ago, # | +2

Also, we can use "long" instead of "long long" on 64 bit systems. As far as I
know, it works on topcoder but not on codechef. Haven't tried it on codeforces
though.
xorfire → Reply

9 years ago, # ^ | ← Rev. 2 0

Codeforces uses MinGW 4.7.2 32 bits, so "long" is only 32-bit, on 64-


bit compilers "long" should have 64-bits to fit register (word) size (as in
TopCoder). In fact the number of bits for each type is implementation
dependent, int should have at least 16 bits, long should have at least
32 bits, the standard only provides lower bounds on the number of
mukel bits. Resuming, on Codeforces long is exactly the same as int, use
long long for 64 bits.
→ Reply

9 years ago, # ^ | +5

on 64-bit systems "long" should have 64-bits to fit


register (word) size

No, as you said yourself, long is guaranteed to hold only


numbers from - 231 to 231 - 1. On 64-bit Windows long
andreyv
indeed is still 32 bits. When you want 64 bits, use long
long or [u]int64_t if it is defined.
→ Reply

9 years ago, # ^ | 0

I mean 64-bit compilers, not 64-bit OS, and even in


that case there is no guarantee that "long" has 64-
bits.
mukel → Reply

9 years ago, # ^ | +5

Yes, I also meant that long is still 32


bits in this very case.
→ Reply
andreyv

8 years ago, # ^ | 0

Windows takes the design to unify


datatype size across 32-bit and 64-bit
nakeep systems and compilers ( with the main
exception of pointer and size_t types)
aiming to easy migration of code. While
Unix-like system chose the opposite, so
only if use 64-bit compiler on 64-bit Linux
or OS X you will get a 64-bit long data
type. If you want a crossplatform solution
either never use long ( use int or long long
according to your needs) or use int32_t
and int64_t defined in <cinttypes>
header, both solution are supported on
any C++11 compliant compiler, but the
first is supported on gcc before C++11.
→ Reply

https://codeforces.com/blog/entry/10124 6/13
16/01/2023, 01:09 C++11 for programming contests... - Codeforces


8 years ago, # ^
← Rev. 2 | +8

Strictly speaking, int32_t


may doesn't exist if compiler
doesn't support 32bit types.
(e.g it its char size is smth like
10bit)
riadwaw
Not that it's going to happen on
PC.
→ Reply

9 years ago, # | +5

On VS12 doesn't work this feature

std::vector<int> a = {1, 2, 3};

tyamgin Maybe there's some project settings?


→ Reply

9 years ago, # ^ | +8

Sadly, VS is behind GNU and Clang in terms of C++11 support, but VS


2013 should support initializer lists, more info here.

mukel
→ Reply

9 years ago, # | 0

Thanks for this nice post! Could you suggest any good books on C++11, or
tutorials on inheritance of STL's container/iterator,etc?
→ Reply
ka16

9 years ago, # | +3

Nice tutorial, I haven't seen the range trick before. BTW, Bjarne Stroustrup's
C++11 FAQ is also very helpful: http://www.stroustrup.com/C++11FAQ.html
→ Reply
andreyv

7 years ago, # ^ | 0

Thanks for this!

By the way, is there a better recommendation available now? The FAQ


seems to be missing some chunks.
superkira
→ Reply

3 years ago, # ^ | 0

but what if we want to traverse from l to r,where l>r. then what can we
do?
→ Reply
Dhruv_Gheewala

3 years ago, # ^ | 0

one way is, we can add this line before return statement in
range(b,e) :
Dhruv_Gheewala
if(b>e)return number_range(-b,-e); we can use our variable by
adding minus before it.

like :

for(int i : range(1000,0)) cout<<arr[-i]<<" "; cout<<endl;

will print array in reverse order. i know we can do like this,

for(int i : range(0 1000)) cout<<arr[999-i]<<" "; cout<<endl;

https://codeforces.com/blog/entry/10124 7/13
16/01/2023, 01:09 C++11 for programming contests... - Codeforces

for(int i : range(0,1000)) cout<<arr[999 i]<< ; cout<<endl;



but, what if we really want reverse range(), then we can use
above one !!

is there any other way??


→ Reply

9 years ago, # | 0

http://channel9.msdn.com/Events/GoingNative/2013/An-Effective-Cpp11-14-
Sampler

An Effective C++11/14 Sampler(Scott Meyers)


000golabi
→ Reply

9 years ago, # | 0

can u elaborate on how auto matrix = array< array<double, 10>,


10>(); is different from double matrix[10][10];
JuanMata → Reply

9 years ago, # ^ | +8

#include<array>
#include<iostream>
using namespace std;

template<class T, unsigned int N>


ostream& operator<<(ostream& os, const array<T, N>&
arr){
for (int i = 0; i < N; ++i)
os << arr[i] << (i < N - 1 ? "," : "");
os << endl;
return os;
}

int main(){
array<array<int, 10>, 10> a;
for (int i = 0; i < a.size(); ++i)
a[i].fill(i);
for (int i = 1; i < a.size(); i += 2)
a[i].swap(a[i - 1]);
000golabi
cout << a << endl;
/* prints
1,1,1,1,1,1,1,1,1,1
,0,0,0,0,0,0,0,0,0,0
,3,3,3,3,3,3,3,3,3,3
,2,2,2,2,2,2,2,2,2,2
,5,5,5,5,5,5,5,5,5,5
,4,4,4,4,4,4,4,4,4,4
,7,7,7,7,7,7,7,7,7,7
,6,6,6,6,6,6,6,6,6,6
,9,9,9,9,9,9,9,9,9,9
,8,8,8,8,8,8,8,8,8,8 */
return 0;
}

just a simple example !


→ Reply

4 years ago, # ^ | 0

Hi.

I was trying to execute the sample code

https://codeforces.com/blog/entry/10124 8/13
16/01/2023, 01:09 C++11 for programming contests... - Codeforces

I was trying to execute the sample code.


abhishek-jha

I am getting the following error on clang C++17:

Invalid operands to binary expression


('std::ostream' (aka 'basic_ostream<char>')
and 'array<array<int, 10>, 10>') The error is on
the line cout << a << endl;

I think the overloaded outstream operator will print the array


by itself. No need to iterate over elements to print.

Also, in reference tot he question to which you replied, I am


still kind of confused. How are they different?

Thanks.
→ Reply

4 years ago, # ^ | 0

replace unsigned int N with size_t N


→ Reply

MrDindows

4 years ago, # ^ | 0

Thanks. That worked. Any explanations


that i can read up on?
→ Reply
abhishek-jha

4 years ago,←# ^2 |
Rev. 0

Explanation is very simple,


std::array is defined as:

template<class T,
std::size_t N> struct
array;

MrDindows And on 64-bit systems


size_t != unsigned
int
→ Reply

9 years ago, # ^ | ← Rev. 2 0

According to what I know, array<> is made for us to use that double


matrix[10][10]; as an STL, they're the same :p
→ Reply
lazyc97

9 years ago, # ^ | +1

A difference means any difference right ? :)

std::array provides nicer interface to deal with the same type


of storage, and thus should be preferred; there's nothing to
000golabi gain with static arrays over std::array.
→ Reply

7 years ago, # ^ | 0

there's nothing to gain with static arrays over


std::array.

You save lots of typing.


JohStraat
→ Reply

https://codeforces.com/blog/entry/10124 9/13
16/01/2023, 01:09 C++11 for programming contests... - Codeforces

 7 years ago, # ^ | 0

Well, you should consider writing python.


→ Reply
000golabi

9 years ago, # ^ | -9

array< array<> > (multidimensional case) does not guarantee that a


contiguous chunk of memory is used as in the classic
multidimensional arrays. Since the new array type is not dynamic (the
size is a template parameter) the compiler can still reserve a
mukel contiguous space.
→ Reply

9 years ago, # ^ | 0

Well, how can it be not contiguous? Inner arrays are


contiguous and lay contiguously in the outer array because
of same guarantee for outer array. I can think only of
alignment issues here, but anyway they aren't in random
riadwaw
places. Do I miss anything?
→ Reply

9 years ago, # ^ | ← Rev. 2 0

array<array<int, 3>, 4> a;


a[1][1] = 5;
++a[0][4];
cout << a[1][1] << endl;
/* (&a[1][1]) == (&a[0][4]) */

000golabi prints 6 as expected with normal 2d arrays . because arr[0][4]


== *(a + 0*3 + 4) == *(a + 1*3 + 1) == a[1][1] !

so the memory is absolutely continuous .


→ Reply

9 years ago, # ^ | ← Rev. 2 -16

If it works on specific implementation on specific


platform in specific point of time, it doesn't mean
anything. UB may be on every step. It's C++

riadwaw Here, you cant get [4] of a[0] because its stay of 3
elements
→ Reply

9 years ago, # ^ | +8

you are wrong :)

page 757 of current standard iso :


http://isocpp.org/files/papers/N3797.pdf
000golabi
→ Reply

8 years ago, # | +2

Codeforces offer two options that include some of C++11 but not all, these are
MS Visual C++2010 and C++0x. For example : MS Visual C++2010 fails in
compilation of STL initializer lists, and C++0x fails in compiling stol() and stoi()
functions
nakeep
→ Reply

https://codeforces.com/blog/entry/10124 10/13
16/01/2023, 01:09 C++11 for programming contests... - Codeforces

 8 years ago, # | ← Rev. 3 +12

Your post is good, but I would like to give some suggestions.

First, why you ever need an auto in the declaration of a variable? I meant,
sometimes it would be shorter without auto .

map<string, pair<int, int>> itCanBeEvenShorter; /* pure C++98


way on this line */
array<int, 10> arr {5, 8, 1, 9, 0, 3, 4, 2, 7, 6};

Second, your iterator is not quite standard conforming. Read §24.2


[iterator.requirements] of the C++11 specification or this for detail, and Boost's
Counting Iterator for a standard-conforming implementation. To make my third
point compile on my machine, you need to make your iterator default-
constructable.

number_iterator(T _v = 0) : v(_v) {}

johnchen902
Third, lower_bound should not be used like this, when there is a good
partition_point :

int findSqrt(int n) {
int lb = partition_point(number_iterator<int>(0),
number_iterator<int>(n),
[&] (int value) { return value * value <= n;
});
return lb - 1;
}

May fast AC be with you.


→ Reply

8 years ago, # ^ | ← Rev. 3 +28

Example where one may want auto :

there's a map you have:


map<that_is<really, long, type>,
std::vector<pair<tuple<int, int, int>, string>>>
mapa;
riadwaw
??? it = mapa.lower_bound(42);

I'd prefer to write auto instead of ??? .


→ Reply

8 years ago, # ^ | +32

by the way, there is another C++11 feature in your code: no


spaces between angle brackets
→ Reply
CountZero

8 years ago, # ^ | 0

yeah. that stupid error require expression


after >> operator
→ Reply
hzyfr

8 years ago, # ^ | +3

Oops. My comment did not match my intent. I would write


auto instead of ??? , too. I meant you need not to
write
johnchen902

auto mapa = map<that is<really long type>

https://codeforces.com/blog/entry/10124 11/13
16/01/2023, 01:09 C++11 for programming contests... - Codeforces

auto mapa = map<that_is<really, long, type>,


 std::vector<pair<tuple<int, int, int>, string>>>
();

as what OP did.
→ Reply

8 years ago, # | +10

Please, can we update to a newer MS C++ compiler? Visual Studio 14 is around


the corner, and Codeforces is still using the compiler from VS 2010, which lacks
most of the features and library support of C++ 11. Using GCC is an option of
course, but you always have a chance of receiving a compilation error, since
IgorAkhmetov compilers are still not 100% compatible.
→ Reply

7 years ago, # | 0

About your range example:

I suppose it to work everywhere but want to notice it's not valid random access
iterator because being random access iterator means to be forward iterator and
ForwardIterator has a requirement:

riadwaw — if X is a mutable iterator, reference is a reference to T; if X is a const


iterator, reference is a reference to const T

where reference is what operator * returns.


→ Reply

7 years ago, # | +18

My favorite use of lambdas is early return:

[&](){
for (int i = 0; i < n; ++i) {
for (int j = 0; j < m; ++j) {
if (whatever(i, j)) {
return;
}
Jacob
// more code
}
}
}();
→ Reply

7 years ago, # ^ | 0

I call it "goto" :). Of course many people consider goto as really bad,
but for this particular case I think it's "the way".
Swistakk → Reply

7 years ago, # | 0

In your samples you capture everything by reference in lambdas (i.e. [&])

That's a bad practice. Capture only things you need


cdkrot
[&by_ref, by_value](int some_args)
{
if (some_args + by_value == 3)
{
++by_ref;
return true;
}

return false;

https://codeforces.com/blog/entry/10124 12/13
16/01/2023, 01:09 C++11 for programming contests... - Codeforces

return false;
 }
→ Reply

7 years ago, # | 0

Another important thing in c++11 you missed is friendly brackets in templates.

map< string, pair< int, int > > somyLongTypeName = map< string,
pair< int, int > >(); // C++
map<string, pair<int, int>> somyLongTypeName = map<string,
cdkrot pair<int, int>>(); // C++11, much better!
→ Reply

Codeforces (c) Copyright 2010-2023 Mike Mirzayanov


The only programming contests Web 2.0 platform
Server time: Jan/16/2023 01:07:23UTC+6 (g1).
Desktop version, switch to mobile version.
Privacy Policy

Supported by

https://codeforces.com/blog/entry/10124 13/13

You might also like