How do I initialize a const std::pair?
Asked Answered
D

5

20

Let's say that I've got a :

#include <utility>
using namespace std;
typedef pair<int, int> my_pair;

how do I initialize a const my_pair ?

Doralynn answered 5/8, 2009 at 8:5 Comment(0)
A
38

Use its constructor:

const my_pair p( 1, 2 );
Alicea answered 5/8, 2009 at 8:7 Comment(0)
M
28

With C++11 you can also use one of the following:

const my_pair p = {2, 3};
const my_pair p({2, 3});
const my_pair p{2, 3};
Medical answered 27/8, 2015 at 14:19 Comment(0)
A
21
const my_pair p = std::make_pair( 2, 3);
Allowed answered 5/8, 2009 at 8:12 Comment(0)
T
1

const my_pair p = my_pair(3, 2);

Teofilateosinte answered 5/8, 2009 at 8:10 Comment(0)
D
0

Although this post is quite old, I encountered the same problem today and managed to resolve it as follows. I hope it will be helpful for others.

const std::array<const std::pair<std::string, int>, argument_no> infoArr = {std::make_pair("start_year", 0), std::make_pair("end_year", 0)};

Dealings answered 16/5 at 14:54 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.