From the course: C++ Templates and the STL
Unlock this course with a free trial
Join today to access over 22,600 courses taught by industry experts.
Pair and tuple - C++ Tutorial
From the course: C++ Templates and the STL
Pair and tuple
- [Bill] Pairs and tuples are useful in places where you want to carry multiple, strongly typed values. In many cases, a pair or a tuple can be more convenient than a struct. Here, I have a working copy of pair.cpp from chapter two of the exercise files. The pair type is in the utility header. It's a small and simple type that's useful for carrying a pair of strongly typed values. There are several constructors available for pair, and we can scroll down here and we can see the pair being constructed with an initializer list, which is available, of course, beginning in C++11. A pair being instructed with a default constructor and the values being passed directly, and a pair being constructed with make_pair, and there's also comparison operators available. When I build and run this, you'll see we have our initialized pairs and we have the comparison operator results. The comparison operator does compare both values of the pair, but it gives priority to the first value. So, if the first…
Practice while you learn with exercise files
Download the files the instructor uses to teach the course. Follow along and learn by watching, listening and practicing.