如何初始化 const std::pair?

发布于 2024-07-30 02:55:30 字数 154 浏览 3 评论 0 原文

假设我有一个:

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

如何初始化 const my_pair ?

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 ?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

娇俏 2024-08-06 02:55:30

对于 C++11,您还可以使用以下其中一项:

const my_pair p = {2, 3};
const my_pair p({2, 3});
const my_pair p{2, 3};

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};
凡间太子 2024-08-06 02:55:30
const my_pair p = std::make_pair( 2, 3);
const my_pair p = std::make_pair( 2, 3);
一萌ing 2024-08-06 02:55:30

const my_pair p = my_pair(3, 2);

const my_pair p = my_pair(3, 2);

梦途 2024-08-06 02:55:30

虽然这篇文章已经很老了,但我今天遇到了同样的问题并设法解决如下。
我希望这对其他人有帮助。

const std::array, argument_no>; infoArr = {std::make_pair("start_year", 0), std::make_pair("end_year", 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)};

救星 2024-08-06 02:55:30

使用它的构造函数:

const my_pair p( 1, 2 );

Use its constructor:

const my_pair p( 1, 2 );
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文