如何避免在使用STL容器函数时使用复制构造函数(push_back等) [英] How to avoid the use of copy constructor when using STL container function (push_back, etc.)

查看:98
本文介绍了如何避免在使用STL容器函数时使用复制构造函数(push_back等)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

以下程序调用普通构造函数和副本

构造函数。

通过调用复制构造器是冗余的,我想要的只是一个

试用对象的向量。


有没有办法避免使用复制构造函数?


#include< vector>

#include< iostream>


课程试用{

public:

trial (int i){

std :: cout<< 在构造函数中 << std :: endl;

}

trial(const trial&){

std :: cout<< 在复制构造函数中。 << std :: endl;

}

};


int main()

{

std :: vector< trial> v;

v.push_back(trial(10));

}


/ ******** ******输出

在构造函数中。

在复制构造函数中。

************ ************ /

The following program calls the normal constructor and the copy
constructor.
By calling the copy constuctor is redundandant, all I want is only a
vector of a trial object.

Is there any way to avoid the use of the copy constructor?

#include <vector>
#include <iostream>

class trial {
public:
trial(int i) {
std::cout << "In constructor" << std::endl;
}
trial(const trial &) {
std::cout << "In copy constructor." << std::endl;
}
};

int main()
{
std::vector<trial> v;
v.push_back(trial(10));
}

/**************output
In constructor.
In copy constructor.
************************/

推荐答案

文章< 11 ******* **************@t31g2000cwb.googlegroups。 com>,

" Pe ******* @ gmail.com" < Pe的******* @ gmail.com>写道:
In article <11*********************@t31g2000cwb.googlegroups. com>,
"Pe*******@gmail.com" <Pe*******@gmail.com> wrote:
以下程序调用普通构造函数和副本
构造函数。
通过调用复制构造器是冗余的,我想要的只是一个
有没有办法避免使用复制构造函数?

#include< vector>
#include< ; iostream>

课堂试用{
公开:
试用(int i){
std :: cout<< 在构造函数中 << std :: endl;
}
试用版(const trial&){
std :: cout<< 在复制构造函数中。 << std :: endl;
}
~s trial(){

std :: cout<< 在析构函数中。 << std :: endl;

}};

int main()
{
std :: vector< trial> v;
v.push_back(试验(10));
}

/ **************输出
在构造函数。
在复制构造函数中。
在析构函数中。 ************************ /
The following program calls the normal constructor and the copy
constructor.
By calling the copy constuctor is redundandant, all I want is only a
vector of a trial object.

Is there any way to avoid the use of the copy constructor?

#include <vector>
#include <iostream>

class trial {
public:
trial(int i) {
std::cout << "In constructor" << std::endl;
}
trial(const trial &) {
std::cout << "In copy constructor." << std::endl;
} ~trial() {
std::cout << "In destructor." << std::endl;
} };

int main()
{
std::vector<trial> v;
v.push_back(trial(10));
}

/**************output
In constructor.
In copy constructor. In destructor. ************************/




复制构造函数调用不是多余的。向量无法存储您创建的

临时对象,因为只要达到下一个序列点,它就会超出范围。向量必须复制

试用对象才能存储。

-

魔术取决于传统和信仰。它不欢迎观察,

也不会通过实验获利。另一方面,科学的经验基于
;通过观察和实验可以纠正。



The copy constructor call is not redundant. The vector cannot store the
temporary object you created because it will go out of scope as soon as
the next sequence point is reached. The vector must make a copy of the
trial object to store.
--
Magic depends on tradition and belief. It does not welcome observation,
nor does it profit by experiment. On the other hand, science is based
on experience; it is open to correction by observation and experiment.



Pe*******@gmail.com skrev:

Pe*******@gmail.com skrev:
以下程序调用普通构造函数和副本
构造函数。
通过调用复制构造器是冗余的,我想要的只是一个试验对象的矢量。


copyconstructor不是多余的 - 它必须在那里才能将你的对象复制到向量中。

那里是关于移动物体的方式的提议 - 例如期间

建设。但是,对你的情况不会做任何事情。移动

通常对小物体非常有效。


/ Peter
有没有办法避免使用复制构造函数?

#include< vector>
#include< iostream>

班级试用版{
公开:
trial(int i){
std :: cout<< 在构造函数中 << std :: endl;
}
试用版(const trial&){
std :: cout<< 在复制构造函数中。 << std :: endl;
}
};

int main()
{
std :: vector< trial> v;
v.push_back(试验(10));
}

/ **************输出
在构造函数。
复制构造函数。
************************ /
The following program calls the normal constructor and the copy
constructor.
By calling the copy constuctor is redundandant, all I want is only a
vector of a trial object.
The copyconstructor is not redundant - it has to be there in order to
copy your object into the vector.
There is a proposal on the way that enables moving object - e.g. during
construction. It would not do anything for your case, however. A move
is typically quite effiecient for small objects.

/Peter
Is there any way to avoid the use of the copy constructor?

#include <vector>
#include <iostream>

class trial {
public:
trial(int i) {
std::cout << "In constructor" << std::endl;
}
trial(const trial &) {
std::cout << "In copy constructor." << std::endl;
}
};

int main()
{
std::vector<trial> v;
v.push_back(trial(10));
}

/**************output
In constructor.
In copy constructor.
************************/



Pe*******@gmail.com
以下程序调用普通构造函数和副本
构造函数。
通过调用复制构造器是冗余的,我想要的只是一个
向量试用对象。

有没有办法避免使用复制构造函数?

#include< vector>
#include< iostream> 公开:
试用(int i){
std :: cout<< 在构造函数中 << std :: endl;
}
试用版(const trial&){
std :: cout<< 在复制构造函数中。 << std :: endl;
}
};

int main()
{
std :: vector< trial> v;
v.push_back(试验(10));
}

/ **************输出
在构造函数。
复制构造函数。
************************ /
The following program calls the normal constructor and the copy
constructor.
By calling the copy constuctor is redundandant, all I want is only a
vector of a trial object.

Is there any way to avoid the use of the copy constructor?

#include <vector>
#include <iostream>

class trial {
public:
trial(int i) {
std::cout << "In constructor" << std::endl;
}
trial(const trial &) {
std::cout << "In copy constructor." << std::endl;
}
};

int main()
{
std::vector<trial> v;
v.push_back(trial(10));
}

/**************output
In constructor.
In copy constructor.
************************/



STL容器按值获取元素。一个建议是使用适合您任务的

容器智能指针。然后副本

只复制指针类。 Boost有一些你可以选择的聪明的b / b $ b指针。


BTW,不要使用标准库auto_ptr。由于它是复制

语义,因此不适合在STL容器中使用。


STL containers take elements by value. One suggestion is to use a
container of smart pointers appropriate to your task. Then the copy
is only copying the pointer class. Boost has a number of smart
pointers you can choose from.

BTW, do not use the standard library auto_ptr. Due to it''s copy
semantics, it is not appropriate for use in STL containers.


这篇关于如何避免在使用STL容器函数时使用复制构造函数(push_back等)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆