由买买提看人间百态

boards

本页内容为未名空间相应帖子的节选和存档,一周内的贴子最多显示50字,超过一周显示500字 访问原贴
Programming版 - C++ | list, queue or map on user defined classes
相关主题
dump out the *** contents *** of an XML element? (org.w3c.dom.Element)C#有没有很经典类似c++ programming language那样的书?
Three C/C++ Programming Questions各位给推荐一个C++ programming performance profiler?
JAVA generic programming 是怎么实现的?请大虾们推荐一本关于embedded real-time programming书
C++ Programming Zone zz推荐一个network programming的C++ library? (转载)
哪儿有经典的C++ programing 习题集嘛?[合集] Thinking in C++这本书怎么样啊?
What does C++ program return to the operating system?pthread and C++
提议:发起写书评活动good book on C/C++ programming under Linux/UNIX
Thinking in C++这本书怎么样啊?请推荐本php programming的书
相关话题的讨论汇总
话题: myelement话题: list话题: defined话题: elem话题: c++
进入Programming版参与讨论
1 (共1页)
k**l
发帖数: 2966
1
I have a question of c++ programing habit,
I sometimes use list, map or other container to store user defined classes,
and use it later in member functions--say
std::list m_list;
When inserting a new element, I always have to fully create an element, then
call push_back or insert.
MyElement elem(1,2,"something"...);
m_list.push_back(elem);
And push_back would call copy constructor (I defined in MyElement class) to
duplicate the object.
After inserting, the elem itself is never used anymore, only elements in m_
list got used again and again.
I think this is a bit waste of time to always create the MyElement class
twice for this insertion---considering MyElement has some char (or other
type of) arrays. How should I improve the efficiency? Should I always use
pointer containers in this case or there are some neater coding habit?
Thanks
k**l
发帖数: 2966
2
更不爽的是list 的 push 比其他的更复杂些,其他的是copy, list 是copy or move


,
then
to

【在 k**l 的大作中提到】
: I have a question of c++ programing habit,
: I sometimes use list, map or other container to store user defined classes,
: and use it later in member functions--say
: std::list m_list;
: When inserting a new element, I always have to fully create an element, then
: call push_back or insert.
: MyElement elem(1,2,"something"...);
: m_list.push_back(elem);
: And push_back would call copy constructor (I defined in MyElement class) to
: duplicate the object.

l*********s
发帖数: 5409
3
no sure what you mean by move, I don't know list container does that.
for optimization, you could do
(1) copy smart pointer than object,
(2) design your class such that copy op is really cheap
(3) move semantics in c++11, or maybe unnamed variable hoping the compiler
could optimize it away
1 (共1页)
进入Programming版参与讨论
相关主题
请推荐本php programming的书哪儿有经典的C++ programing 习题集嘛?
ZZ: 初学者学习C++的50条忠告What does C++ program return to the operating system?
C++证书提议:发起写书评活动
什么是win xp 上最好的C programming environment?Thinking in C++这本书怎么样啊?
dump out the *** contents *** of an XML element? (org.w3c.dom.Element)C#有没有很经典类似c++ programming language那样的书?
Three C/C++ Programming Questions各位给推荐一个C++ programming performance profiler?
JAVA generic programming 是怎么实现的?请大虾们推荐一本关于embedded real-time programming书
C++ Programming Zone zz推荐一个network programming的C++ library? (转载)
相关话题的讨论汇总
话题: myelement话题: list话题: defined话题: elem话题: c++