[英]vector<string> or vector<shared_ptr<string>> in c++ 14
在旧的c ++样式中,我总是使用vector < shared_ptr < string> >
或vector < string* >
来避免在构造包含许多string
对象的大vector
时进行内存复制。
从c ++ 11开始,c ++具有rvalue
和move
语义; 我现在可以使用vector < string >
吗?
我使用gcc 7.1.0和clang 3.6与c ++ 14选项。
在以下几种情况下,使用vector<shared_ptr<string>>
或vector<string*>
可以帮助在C ++ 11之前优化性能:
std::string
具有非抛出移动构造函数,因此对于std::vector<string>
push_back()应该std::vector<std::shared_ptr<string>>
慢。 std::vector<string>
也是合适的。 如果你的目的是返回集合的副本而不是复制元素 - 这是std::vector<shared_ptr<string>>
仍然std::vector<shared_ptr<string>>
的唯一方法。 但在这种情况下,我的建议是在集合之间共享不可变对象,即使用std::vector<shared_ptr<const string>>
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.