vector :: push_back的效率? [英] efficiency of vector::push_back?

查看:235
本文介绍了vector :: push_back的效率?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我有C级的对象,它们相当大。然后考虑:


vector< C> vc;

vc.push_back(C());


天真这似乎构造了一个临时对象C(),复制它

进入vector所拥有的空间,然后删除临时对象。


我意识到这是依赖于实现的,但是最现代的是

编译器优化了临时对象的创建,或者是否有一个令人信服的理由说明这是不可能的?


谢谢,

Mark

Say I have objects of class C which are fairly large. Then consider:

vector<C> vc;
vc.push_back(C());

Naively this would seem to construct a temporary object C(), copy it
into the space owned by the vector, and then delete the temporary object.

I realize this is implementation dependent, but will most modern
compilers optimize away the creation of the temporary object, or is
there a compelling reason why this is not possible?

Thanks,
Mark

推荐答案

Mark P写道:
Mark P wrote:
说我有C级的对象是公平的大。然后考虑:

vector< C> vc;
vc.push_back(C());

天真这似乎构造了一个临时对象C(),将它复制到向量所拥有的空间中,然后删除临时对象。

我意识到这是依赖于实现的,但是大多数现代编译器会优化掉临时对象的创建,或者是否有令人信服的理由为什么这是不可能的?

谢谢,
马克
Say I have objects of class C which are fairly large. Then consider:

vector<C> vc;
vc.push_back(C());

Naively this would seem to construct a temporary object C(), copy it
into the space owned by the vector, and then delete the temporary object.

I realize this is implementation dependent, but will most modern
compilers optimize away the creation of the temporary object, or is
there a compelling reason why this is not possible?

Thanks,
Mark




我相信会发生的事情是:


1.用0容量创建vc。

2.当调用push_back时,构造临时C对象。

3.然后增长vc容量为1.

4.随着向量的增长,构造另一个C对象。这个是

里面的vc

5.然后临时C对象(由2中的push_back调用创建)是

然后被复制(通过operator =或复制构造函数进入C对象,

在向量内部(在4中创建)。



I believe what happens is the:

1. vc is created with 0 capacity.
2. When push_back is called a temporary C object is constructed.
3. vc is then grown to capacity of 1.
4. As the vector is grown, another C object is constructed. This one is
inside of vc
5. Then the temporary C object (created by the call to push_back in 2) is
then copied (via the operator= or copy constructor into the C object that
is inside the vector (created in 4).


>说我C类的对象是相当
>Say I have objects of class C which are fairly
大。然后考虑:
vector< C> vc;
vc.push_back(C());
天真这个似乎构造了一个临时的对象C(),将其复制到
向量所拥有的空间中,然后删除临时对象。
我意识到这是依赖于实现的,但是
large. Then consider: vector<C> vc;
vc.push_back(C()); Naively this would seem to construct a temporary
object C(), copy it into the space owned by the
vector, and then delete the temporary object. I realize this is implementation dependent, but
will most modern compilers optimize away the
creation of the temporary object, or is there a
compelling reason why this is not possible?




如果构造和复制C类对象的价格昂贵,请不要按值存储它们。

std :: vector通常使用
$ b实现$ b动态数组,所以当它必须分配内存时,

它将复制其元素。改为使用指针。


std :: vector< C *> vc;

vc.push_back(新C);


-

Jonathan

[常见问题] - http://www.parashift.com/c++-faq- lite /



If constructing and copying objects of class C is
expensive, do not store them by value.
std::vector is usually implemented using a
dynamic array, so when it has to allocate memory,
it will copy its elements. Use pointers instead.

std::vector<C*> vc;
vc.push_back(new C);

--
Jonathan
[FAQ] - http://www.parashift.com/c++-faq-lite/


Mark P写道:
Mark P wrote:
说我有C类的对象是相当大。
然后考虑:

vector< C> vc;
vc.push_back(C());

天真地,这似乎构造了一个临时对象C(),
将它复制到向量所拥有的空间
然后删除临时对象。


是的,但是......

我意识到这是依赖于实现的
但大多数现代编译器都会优化掉
临时对象的创建
还是有一个令人信服的理由说明为什么这是不可能的?
cat main.cc
#include< iostream>

#include< vector>


class C {

私人:

//代表

int I;

public:

//运营商

朋友

std :: ostream& operator<<(std :: ostream& os,const C& c){

return os<< cI;

}

//构造函数

C(int i = 0):I(i){

std :: cerr<< "Ç:: C(INT)" << std :: endl;

}

C(const C& c):I(cI){

std :: cerr<< ; C :: C(const C&) << std :: endl;

}

~C(无效){

std :: cerr<< "Ç::〜C(空隙)" << std :: endl;

}

};


int main(int argc,char * argv []){

std :: vector< C> vc;

vc.push_back(C());

返回0;

}

g ++ -Wall -ansi -pedantic -O3 -o main main.cc
./main
Say I have objects of class C which are fairly large.
Then consider:

vector<C> vc;
vc.push_back(C());

Naively, this would seem to construct a temporary object C(),
copy it into the space owned by the vector
and then delete the temporary object.
Yes, but...
I realize [that] this is implementation dependent
but will most modern compilers optimize away
the creation of the temporary object
or is there a compelling reason why this is not possible? cat main.cc #include <iostream>
#include <vector>

class C {
private:
// representation
int I;
public:
// operators
friend
std::ostream& operator<<(std::ostream& os, const C& c) {
return os << c.I;
}
// constructors
C(int i = 0): I(i) {
std::cerr << "C::C(int)" << std::endl;
}
C(const C& c): I(c.I) {
std::cerr << "C::C(const C&)" << std::endl;
}
~C(void) {
std::cerr << "C::~C(void)" << std::endl;
}
};

int main(int argc, char* argv[]) {
std::vector<C> vc;
vc.push_back(C());
return 0;
}
g++ -Wall -ansi -pedantic -O3 -o main main.cc
./main



C :: C(int)

C :: C (const C&)

C ::〜C(无效)

C ::〜C(无效)


自构造函数,析构函数和push_back函数

都定义为内联函数

编译器可以优化除了诊断消息之外的所有内容

标准错误。


C::C(int)
C::C(const C&)
C::~C(void)
C::~C(void)

Since the constructors, destructors and the push_back function
are all defined to be inline functions
the compiler can optimize away everything
except the diagnostic messages to standard error.


这篇关于vector :: push_back的效率?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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