传输矢量内容 [英] Transferring vector content
问题描述
我有这样的载体:
std :: vector< T> v1,v2;
需要将v1的所有项目转移到v2。
最快的方法是什么?
tuvok写道:我有这样的载体:
std :: vector< T> v1,v2;
需要将v1的所有项目转移到v2。
最快的方法是什么?
取决于几件事情:
1.你需要保持v1完整吗?
如果不是:
std :: swap(v1,v2 );
2.否则,什么是错的
v2 = v1;
大概是实施赢了''没有比简单复制语义更容易理解你正在做什么和优化的事情。
" Ron Natalie"写了tuvok写道:我有这样的载体:
std :: vector< T> v1,v2;
需要将v1的所有项目转移到v2。
最快的方法是什么?
取决于几件事:>
1.你需要保持v1原封不动吗?
如果不是:
std :: swap(v1,v2);
2.否则,出了什么问题
v2 = v1;
大概这个实现没有比简单的复制语义更容易理解你正在做什么和优化。
我目前正在使用第二个版本,但正在寻找更快的方式。
v1在转移后不需要完好无损。如果它没有调用任何项目dtor,那么最好是
收缩到0元。这可能吗?
" tuvok" < 52 *************** @ t-online.de>在消息中写道
news:d8 ************* @ news.t-online.com ..." Ron Natalie" ;写了tuvok写道:我有这样的载体:
std :: vector< T> v1,v2;
需要将v1的所有项目转移到v2。
最快的方法是什么?
取决于几件事:>
1.你需要保持v1原封不动吗?
如果不是:
std :: swap(v1,v2);
2.否则,出了什么问题
v2 = v1;
大概这个实现没有比简单的复制语义更容易理解你正在做什么和优化。
我目前正在使用第二个版本,但正在寻找更快的方式。
v1在转移后不需要完好无损。如果它没有调用任何项目dtor缩小到0元,那将是最好的。这可能吗?
你必须使用std :: vector吗?如果您使用std :: list
,那么您将使用拼接和拼接。方法。
" splice"只会重新解释V1
的元素作为在V2结束时添加的元素,留下
V1为空。非常快,因为没有实际的元素移动了!只更改一两个指针。
-
干杯,
Robbie Hatley
加利福尼亚州塔斯廷,美国
邮箱:lonewolfintj在pacbell dot net
web:home dot pacbell dot net slant earnur slant
---- ==通过Newsfeeds.Com发布 - 无限制 - 未经审查 - 安全Usenet新闻== ----
http://www.newsfeeds.com 世界排名第一的新闻组服务! 120,000多个新闻组
---- =东海岸和西海岸服务器农场 - 通过加密实现全隐私= ----
I have such vectors:
std::vector<T> v1, v2;
and need to transfer all items of v1 to v2.
What is the fastest method?
tuvok wrote:I have such vectors:
std::vector<T> v1, v2;
and need to transfer all items of v1 to v2.
What is the fastest method?
Depends on a few things:
1. Do you need to leave v1 intact?
If not:
std::swap(v1, v2);
2. Otherwise, what''s wrong with
v2 = v1;
Presumably the implementation won''t have any easier time figuring
out what you''re doing and optimizing than the simple copy semantics.
"Ron Natalie" wrotetuvok wrote:I have such vectors:
std::vector<T> v1, v2;
and need to transfer all items of v1 to v2.
What is the fastest method?
Depends on a few things:
1. Do you need to leave v1 intact?
If not:
std::swap(v1, v2);
2. Otherwise, what''s wrong with
v2 = v1;
Presumably the implementation won''t have any easier time figuring
out what you''re doing and optimizing than the simple copy semantics.
I''m currently using the 2nd version, but was looking for a faster way.
v1 does not need to be intact after the transfer. Best would be if it
shrinks to 0 elems without calling any item dtor. Is this possible?
"tuvok" <52***************@t-online.de> wrote in message
news:d8*************@news.t-online.com..."Ron Natalie" wrotetuvok wrote:I have such vectors:
std::vector<T> v1, v2;
and need to transfer all items of v1 to v2.
What is the fastest method?
Depends on a few things:
1. Do you need to leave v1 intact?
If not:
std::swap(v1, v2);
2. Otherwise, what''s wrong with
v2 = v1;
Presumably the implementation won''t have any easier time figuring
out what you''re doing and optimizing than the simple copy semantics.
I''m currently using the 2nd version, but was looking for a faster way.
v1 does not need to be intact after the transfer. Best would be if it
shrinks to 0 elems without calling any item dtor. Is this possible?
Do you have to use a std::vector? If you used std::list
instead, you''d have use of the "splice" method.
"splice" would just re-interpret the elements of V1
as being elements tacked-on to the end of V2, leaving
V1 empty. Blazingly fast, because no actual elements
are moved! Just a pointer or two is changed.
--
Cheers,
Robbie Hatley
Tustin, CA, USA
email: lonewolfintj at pacbell dot net
web: home dot pacbell dot net slant earnur slant
----== Posted via Newsfeeds.Com - Unlimited-Uncensored-Secure Usenet News==----
http://www.newsfeeds.com The #1 Newsgroup Service in the World! 120,000+ Newsgroups
----= East and West-Coast Server Farms - Total Privacy via Encryption =----
这篇关于传输矢量内容的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!