就时空复杂度而言,以下哪一项是迭代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/C++源代码后生成的OBJ文件会生成OBJ文件,后来与其他所需库链接以形成EXEcutable文件.我想知道OBJ文件的格式/结构.请继续.
看起来 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)