- c - 在位数组中找到第一个零
- linux - Unix 显示有关匹配两种模式之一的文件的信息
- 正则表达式替换多个文件
- linux - 隐藏来自 xtrace 的命令
我经常看到我的同事做第二个片段。为什么是这样?我尝试添加打印语句来跟踪 ctors 和 dtor,但两者看起来完全相同。
std::vector<ClassTest> vecClass1;
ClassTest ct1;
ct1.blah = blah // set some stuff
...
vecClass1.push_back(ct1);
std::vector<ClassTest> vecClass2;
vecClass2.push_back(ClassTest());
ClassTest& ct2 = vecClass2.back();
ct2.blah = blah // set some stuff
...
附言。如果标题有误导性,我很抱歉。
编辑:
首先感谢大家的回复。
我使用 std::move
编写了一个小型应用程序。结果让我感到惊讶,也许是因为我做错了什么……有人能解释一下为什么“快速”路径的性能要好得多吗。
#include <vector>
#include <string>
#include <boost/progress.hpp>
#include <iostream>
const std::size_t SIZE = 10*100*100*100;
//const std::size_t SIZE = 1;
const bool log = (SIZE == 1);
struct SomeType {
std::string who;
std::string bio;
SomeType() {
if (log) std::cout << "SomeType()" << std::endl;
}
SomeType(const SomeType& other) {
if (log) std::cout << "SomeType(const SomeType&)" << std::endl;
//this->who.swap(other.who);
//this->bio.swap(other.bio);
this->who = other.who;
this->bio = other.bio;
}
SomeType& operator=(SomeType& other) {
if (log) std::cout << "SomeType::operator=()" << std::endl;
this->who.swap(other.who);
this->bio.swap(other.bio);
return *this;
}
~SomeType() {
if (log) std::cout << "~SomeType()" << std::endl;
}
void swap(SomeType& other) {
if (log) std::cout << "Swapping" << std::endl;
this->who.swap(other.who);
this->bio.swap(other.bio);
}
// move semantics
SomeType(SomeType&& other) :
who(std::move(other.who))
, bio(std::move(other.bio)) {
if (log) std::cout << "SomeType(SomeType&&)" << std::endl;
}
SomeType& operator=(SomeType&& other) {
if (log) std::cout << "SomeType::operator=(SomeType&&)" << std::endl;
this->who = std::move(other.who);
this->bio = std::move(other.bio);
return *this;
}
};
int main(int argc, char** argv) {
{
boost::progress_timer time_taken;
std::vector<SomeType> store;
std::cout << "Timing \"slow\" path" << std::endl;
for (std::size_t i = 0; i < SIZE; ++i) {
SomeType some;
some.who = "bruce banner the hulk";
some.bio = "you do not want to see me angry";
//store.push_back(SomeType());
//store.back().swap(some);
store.push_back(std::move(some));
}
}
{
boost::progress_timer time_taken;
std::vector<SomeType> store;
std::cout << "Timing \"fast\" path" << std::endl;
for (std::size_t i = 0; i < SIZE; ++i) {
store.push_back(SomeType());
SomeType& some = store.back();
some.who = "bruce banner the hulk";
some.bio = "you do not want to see me angry";
}
}
return 0;
}
输出:
dev@ubuntu-10:~/Desktop/perf_test$ g++ -Wall -O3 push_back-test.cpp -std=c++0x
dev@ubuntu-10:~/Desktop/perf_test$ ./a.out
Timing "slow" path
3.36 s
Timing "fast" path
3.08 s
最佳答案
如果在“set some stuff”之后复制对象比之前更昂贵,那么如果在“set some stuff”之前插入对象,则将对象插入 vector 时发生的复制将比之前更便宜之后。
不过,实际上,由于您应该期望 vector 中的对象偶尔会被复制,所以这可能算不上什么优化。
关于c++ - 类和结构的高效 push_back,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4054850/
这个问题在这里已经有了答案: Passing by value vs const & and && overloads (3 个答案) 关闭 8 年前。 为什么push_back的函数签名如下? v
代码如下: std::vector s; s.push_back(~Dword(0)); 什么是~Dword?它是如何工作的? 最佳答案 Dword 这里是一个数字类型(可能是 DWORD 的类型别名
我正在测试 C++ 中推回对象与推回对象指针到 Vector 之间的性能差异。 我在 Stackoverflow 和其他文章中读到,您应该避免向后推指针,除非您必须这样做... 但是,我意识到推回指针
关闭。这个问题是not reproducible or was caused by typos .它目前不接受答案。 这个问题是由于错别字或无法再重现的问题引起的。虽然类似的问题可能是on-topi
我有一个很大的 .txt 文件,需要加载并存储在 vector 中。该文件大小约为 5MB,500 000 行,每行约 10-20 个字符,以 '\n' 分隔。我正在使用以下示例代码对读取整个文件所需
这个问题在这里已经有了答案: What is object slicing? (18 个答案) 关闭 9 年前。 我有这两个类: class A { public: A(); vir
在我当前的代码中,我想将新的 DrawObjects 插入到我创建的 vector 中, std::vector 对象; 有什么区别: objects.push_back(DrawObject(nam
我将 Cygwin 与 GCC 一起使用,最终我想将字 rune 件读入字符 vector ,并使用此代码 #include #include #include using namespace
以下代码创建一个临时对象 A 并将其推送到一个 vector 中。 在 push_back 期间删除复制构造函数并调用移动构造函数。我不确定这段代码的设计是否正确,肯定存在内存泄漏。 #include
我知道push_back可以抛出bad_alloc异常,并且如果没有try catch block ,则调用析构函数是不正确的。如果有任何push_back抛出并且它不在try catch block
正如 Scott Meyers 所指出的(http://channel9.msdn.com/Events/GoingNative/2013/An-Effective-Cpp11-14-Sampler
我正在写一个 push_back将临时容器添加到另一个容器的函数。 并且它应该在使用 push_back 之前调整或保留容器(如果两者都可用,它应该更喜欢保留而不是调整大小) 当前代码是: names
这个问题在这里已经有了答案: Create an array when the size is a variable not a constant (2 个答案) 关闭 3 年前。 我认为我在代码中
代码: // test2.cpp #include #include struct test_class { test_class() = default; test_class(
我试图在 forloop 的 vector 中推回一个 const char*。 char_temp 是一个名为 segment 的结构,而 chars_temp 是一个结构 vector 。请参阅下
在 C++ 入门书第 (3) 章中,有以下 for 循环将 vector 中的元素重置为零。 vector ivec; //UPDATE: vector declaration for (vector
class A { public: A():a(0) {} A(int x):a(x) { coutve
我正在尝试使用 Visual Leak Detector 查找内存泄漏。它告诉我 m_neighbors.push_back(ent);导致泄漏。 (简短调用堆栈 = NeighborCalculat
我们正在制作一个包含棋盘游戏信息(名称、年份、分数)的列表。我们从 .csv 文件中扫描信息,根据该信息创建一个结构,然后将该结构添加到列表中。我们一直这样做,直到文档阅读完毕。问题是列表的 push
以下代码是将“非重叠”的 TablePath 从 vector v 移动到 vector u。我在“u.push_back(*it1);”行遇到段错误。我没有复制对象(而是只复制对象的指针)所以我相信
我是一名优秀的程序员,十分优秀!