C++ 如何初始化 const std::pair?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1231788/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-27 19:14:52 来源:igfitidea点击:
How do I initialize a const std::pair?
提问by Maciek
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 ?
如何初始化 const my_pair ?
回答by
Use its constructor:
使用它的构造函数:
const my_pair p( 1, 2 );
回答by Ozgur Murat
With C++11 you can also use one of the following:
使用 C++11,您还可以使用以下之一:
const my_pair p = {2, 3};
const my_pair p({2, 3});
const my_pair p{2, 3};
回答by C?t?lin Piti?
const my_pair p = std::make_pair( 2, 3);
回答by Ivan
const my_pair p = my_pair(3, 2);
const my_pair p = my_pair(3, 2);