C++ 制作c++;编译器优化以在映射初始化中调用构造函数一次
我有一个函数,可以生成一个映射到类型a的对象C++ 制作c++;编译器优化以在映射初始化中调用构造函数一次,c++,initialization,C++,Initialization,我有一个函数,可以生成一个映射到类型a的对象 map<int,A> test() { map<int, A> m; A a1(10); // constructor A a2(20); A a3(30); m[0] = a1; m[1] = a2; m[2] = a3; // <-- copy constructor and = operator return m; } map测试() { 地图m; a1(10);/
map<int,A> test()
{
map<int, A> m;
A a1(10); // constructor
A a2(20);
A a3(30);
m[0] = a1; m[1] = a2; m[2] = a3; // <-- copy constructor and = operator
return m;
}
map测试()
{
地图m;
a1(10);//构造函数
A a2(20);
A a3(30);
m[0]=a1;m[1]=a2;m[2]=a3;//如果您的实现支持它,则:
我还用智能指针进行了测试,我觉得很好
#include <iostream>
#include <map>
#include <memory>
using namespace std;
class A
{
int x;
public:
A() {
cout << "Default constructor called" << endl;
}
A(const A& rhs)
{
this->x = rhs.x;
cout << "Copy constructor called" << endl;
}
A& operator=(const A& rhs)
{
this->x = rhs.x;
cout << "Copy constructor called" << endl;
}
A(int x) : x(x) {
cout << "*I'm in" << endl;
}
~A() {
cout << "*I'm out" << endl;
}
int get() const {return x;}
void set(int x) {this->x = x;}
};
std::map<int,unique_ptr<A>> test()
{
std::map<int, unique_ptr<A>> m;
m[0] = unique_ptr<A>(new A(101));
m[1] = unique_ptr<A>(new A(201));
m[2] = unique_ptr<A>(new A(301));
return m;
}
using namespace std;
int main(int argc, char *argv[]) {
auto m = test();
for (const auto& i: m)
{
cout << i.second->get() << endl;
}
}
您可以执行返回{{0,A{10},{1,A{20},{2,A{30}};
。您应该通过引用传递映射,这样可以避免在外部复制。
map<int,A*> test3()
{
map<int, A*> m;
A* a1 = new A(10);
A* a2 = new A(20);
A* a3 = new A(30);
m[0] = a1; m[1] = a2; m[2] = a3;
return m;
}
...
map<int,A*> x = test3();
...
for (auto val: x)
{
delete val.second;
}
#include <map>
#include <iostream>
struct A {
A(int) { std::cout << "ctor\n"; }
A(const A&) { std::cout << "copy ctor\n"; }
A(A&&) { std::cout << "move ctor\n"; }
~A() { std::cout << "dtor\n"; }
};
std::map<int,A> test()
{
std::map<int, A> m;
m.emplace(0, 10);
m.emplace(1, 20);
m.emplace(2, 30);
return m;
}
int main()
{
std::map<int, A> m = test();
std::cout << "m.size() = " << m.size() << '\n';
}
$ ./test
ctor
ctor
ctor
m.size() = 3
dtor
dtor
dtor
#include <iostream>
#include <map>
#include <memory>
using namespace std;
class A
{
int x;
public:
A() {
cout << "Default constructor called" << endl;
}
A(const A& rhs)
{
this->x = rhs.x;
cout << "Copy constructor called" << endl;
}
A& operator=(const A& rhs)
{
this->x = rhs.x;
cout << "Copy constructor called" << endl;
}
A(int x) : x(x) {
cout << "*I'm in" << endl;
}
~A() {
cout << "*I'm out" << endl;
}
int get() const {return x;}
void set(int x) {this->x = x;}
};
std::map<int,unique_ptr<A>> test()
{
std::map<int, unique_ptr<A>> m;
m[0] = unique_ptr<A>(new A(101));
m[1] = unique_ptr<A>(new A(201));
m[2] = unique_ptr<A>(new A(301));
return m;
}
using namespace std;
int main(int argc, char *argv[]) {
auto m = test();
for (const auto& i: m)
{
cout << i.second->get() << endl;
}
}
*I'm in
*I'm in
*I'm in
101
201
301
*I'm out
*I'm out
*I'm out