2 ms·
The only type used in the demos is vector<pair<int,int>>. Vectors are guaranteed to be contiguous in memory. I'm pretty sure pairs are too, but that's easy to
by chubot 11y ago
The only type used in the demos is vector<pair<int,int>>. Vectors are guaranteed to be contiguous in memory. I'm pretty sure pairs are too, but that's easy to test:
#include "stdio.h"
#include <utility>
printf("%zu\n", sizeof(std::pair<int, int>));
8
So it's the obvious layout you would see in C with an array of struct { int x; int y }.
The cling interpreter is good for this kind of thing.
- MaulingMonkey 11y ago20.2.2 Pairs ¶ 1 (ISO/IEC 14882:2003): The library provides a template for heterogeneous pairs of values. The library also provides a matching function template to simplify their construction. template <class T1, class T2> struct pair { typedef T1 first_type; typedef T2 second_type; T1 first; T2 second; pair(); pair(const T1& x, const T2& y); template<class U, class V> pair(const pair<U, V> &p); }; pair(); tl;dr: It looks like the 03 standard requires it to be equivalent to the plant C version in terms of layout, although that could involve padding between first and second if e.g. T1 = short and T2 = int, just like in the equivalent C code.