传递给 CPP 中的函数时 { } 是什么?
What is { } when passed to a function in CPP?
map
insert
函数以 {string,int}
作为参数。这是如何工作的?
#include <map>
using namespace std;
int main(int argc, char *arg[])
{
map<string, int> m;
m.insert({"a", 1});
}
{"a", 1}
是一个 braced-init-list,当传递给函数时,copy-list-initialization(在 C++11 中引入)是执行。
function( { arg1, arg2, ... } ) (7)
7) in a function call expression, with braced-init-list used as an
argument and list-initialization initializes the function parameter
给定 map<string, int> m;
和 m.insert({"a", 1});
,std::map::insert
expectes a std::pair<const string, int>
; therefore {"a", 1}
is used to initialize a temporary std::pair
which is passed to .insert()
. The temporary std::pair
is initialized by its constructor;将其成员 first
初始化为 "a"
并将 second
初始化为 1
.
map
insert
函数以 {string,int}
作为参数。这是如何工作的?
#include <map>
using namespace std;
int main(int argc, char *arg[])
{
map<string, int> m;
m.insert({"a", 1});
}
{"a", 1}
是一个 braced-init-list,当传递给函数时,copy-list-initialization(在 C++11 中引入)是执行。
function( { arg1, arg2, ... } ) (7)
7) in a function call expression, with braced-init-list used as an argument and list-initialization initializes the function parameter
给定 map<string, int> m;
和 m.insert({"a", 1});
,std::map::insert
expectes a std::pair<const string, int>
; therefore {"a", 1}
is used to initialize a temporary std::pair
which is passed to .insert()
. The temporary std::pair
is initialized by its constructor;将其成员 first
初始化为 "a"
并将 second
初始化为 1
.