小编Suh*_*sis的帖子

迭代std :: vector的最有效方法是什么?为什么?

就时空复杂度而言,以下哪一项是迭代std :: vector的最佳方法,为什么?

方法1:

for(std::vector<T>::iterator it = v.begin(); it != v.end(); ++it) {
    /* std::cout << *it; ... */
}
Run Code Online (Sandbox Code Playgroud)

方式2:

for(std::vector<int>::size_type i = 0; i != v.size(); i++) {
    /* std::cout << v[i]; ... */
}
Run Code Online (Sandbox Code Playgroud)

方式三:

for(size_t i = 0; i != v.size(); i++) {
    /* std::cout << v[i]; ... */
}
Run Code Online (Sandbox Code Playgroud)

方式4:

for(auto const& value: a) {
     /* std::cout << value; ... */
Run Code Online (Sandbox Code Playgroud)

c++ performance iterator stl

40
推荐指数
3
解决办法
3209
查看次数

OBJ文件的内容是什么?

我知道在任何标准编译器中编译C/C++源代码后生成的OBJ文件会生成OBJ文件,后来与其他所需库链接以形成EXEcutable文件.我想知道OBJ文件的格式/结构.请继续.

c++ c++builder

2
推荐指数
1
解决办法
2517
查看次数

为什么要对 std::make_unique 进行一组额外的构造函数/析构函数调用?

看起来 std::make_unique 正在制作额外的副本(相关帖子),而它应该更有效。下面是具有典型 C++ 多态类架构的示例实现。代码片段下面提到的输出显示,如果我们取消对 make_unique 调用的注释,它会调用一对额外的 ctor/dtor。

class Shape
{
public:
    Shape() { cout << "Shape::ctor\n"; }
    virtual ~Shape() {cout << "Shape::dtor\n";}
};

class Rectangle : public Shape
{
public:
    Rectangle() {cout << "Rectangle::ctor\n";}
    virtual ~Rectangle() {cout << "Rectangle::dtor\n";}
};

unique_ptr<Shape> makeShape(int type) 
{
    //unique_ptr<Shape> spShape = make_unique<Shape>(); //extra ctor call
    unique_ptr<Shape> spShape(nullptr);
    switch (type)
    {
    case 1: //rect
        spShape.reset(new Rectangle());
        break;   
    default:
        break;
    }
    return spShape;
}

int main(int argc, char const *argv[])
{
    auto shape1 …
Run Code Online (Sandbox Code Playgroud)

c++ unique-ptr c++14

0
推荐指数
1
解决办法
361
查看次数

标签 统计

c++ ×3

c++14 ×1

c++builder ×1

iterator ×1

performance ×1

stl ×1

unique-ptr ×1