加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

c – std :: vector reserve方法是否需要Object类的复制构造函数

发布时间:2020-12-16 10:08:53 所属栏目:百科 来源:网络整理
导读:我试图将一些点保留到std :: vector中,并且出现错误我不明白: #include iostream#include string#include vectorusing namespace std;class Foo{public: std::string str; int i; Foo(){ this-str = ""; this-i = 0; } Foo(Foo copyFoo){ this-str = copyFo
我试图将一些点保留到std :: vector中,并且出现错误我不明白:

#include <iostream>
#include <string>
#include <vector>

using namespace std;

class Foo{
public:    
    std::string str;
    int i;

    Foo(){
        this->str = "";
        this->i = 0;
    }

    Foo(Foo &copyFoo){
        this->str = copyFoo.str; //Or get methods if private members
        this->i = copyFoo.i;
    }

    Foo(std::string str,int i){
        this->str = str;
        this->i = i;
    }
};

int main()
{
   std::vector<Foo> fooVector;
   fooVector.reserve(20); // Error

   for(int i=0; i<20; i++){
       fooVector[i] = Foo("Test",i); // Or should I use operator new?
       // Or should I even stick to the push_back method?
   }

   return 0;
}

当然我可以不保留,它可能会工作.但现在我对它为什么现在不起作用感兴趣.我添加了复制构造函数,因为它看起来可能是我当时遇到的问题.但是在添加了复制构造函数之后,它也无法正常工作.

错误说:

In file included from

/usr/local/gcc-4.8.1/include/c++/4.8.1/vector:62:0,

06001

instantiation of ‘void std::_Construct(_T1*,_Args&& …) [with _T1 =

Foo; _Args = {Foo}]’:

/usr/local/gcc-4.8.1/include/c++/4.8.1/bits/stl_uninitialized.h:75:53:

required from ‘static _ForwardIterator

std::__uninitialized_copy<TrivialValueTypes>::_uninit_copy(_InputIterator,_InputIterator,_ForwardIterator) [with _InputIterator = std::move_iterator; _ForwardIterator = Foo*; bool

_TrivialValueTypes = false]’ /usr/local/gcc-4.8.1/include/c++/4.8.1/bits/stl_uninitialized.h:117:41:

required from ‘_ForwardIterator

std::uninitialized_copy(_InputIterator,

_ForwardIterator) [with _InputIterator = std::move_iterator; _ForwardIterator = Foo*]’ /usr/local/gcc-4.8.1/include/c++/4.8.1/bits/stl_uninitialized.h:258:63:

required from ‘_ForwardIterator

std::__uninitialized_copy_a(_InputIterator,

_ForwardIterator,std::allocator<_Tp>&) [with _InputIterator = std::move_iterator; _ForwardIterator = Foo*; _Tp = Foo]’

/usr/local/gcc-4.8.1/include/c++/4.8.1/bits/stl_vector.h:1142:29:

required from ‘std::vector<_Tp,_Alloc>::pointer std::vector<_Tp,

_Alloc>::_M_allocate_and_copy(std::vector<_Tp,_Alloc>::size_type,_ForwardIterator,_ForwardIterator) [with _ForwardIterator = std::move_iterator; _Tp = Foo; _Alloc = std::allocator;

std::vector<_Tp,_Alloc>::pointer = Foo*; std::vector<_Tp,

_Alloc>::size_type = long unsigned int]’ /usr/local/gcc-4.8.1/include/c++/4.8.1/bits/vector.tcc:75:70:

required from ‘void std::vector<_Tp,_Alloc>::reserve(std::vector<_Tp,

_Alloc>::size_type) [with _Tp = Foo; _Alloc = std::allocator; std::vector<_Tp,_Alloc>::size_type = long unsigned int]’

main.cpp:31:24: required from here

/usr/local/gcc-4.8.1/include/c++/4.8.1/bits/stl_construct.h:75:7:

error: no matching function for call to ‘Foo::Foo(Foo)’

06002

note: candidates are: main.cpp:22:5: note: Foo::Foo(std::string,int)

06003

问题出在哪儿?我是否需要初始化std:vector对象,或者只是将每个位置分配给对象实例?

编辑:我正在使用C 11.如果我删除复制构造函数,我在保留方法行收到以下错误:

required from 'void std::_Construct(_T1*,_Args&& ---) [with _TI = Foo; _Args = {Foo&}]

这就是我首先编写复制构造函数的原因.

我不想使用resize方法,因为我希望size方法返回包含在向量中的实际Foo对象数,而不是我保留的数量.

解决方法

std::vector<Foo> fooVector;
   fooVector.reserve(20); // Error

   for(int i=0; i<20; i++){
       fooVector[i] = Foo("Test",i); // Or should I use operator new?
       // Or should I even stick to the push_back method?
   }

上面的代码是错误的.您正在访问容器中size()以外的元素.这样做的惯用方法是在容器上执行push_back / emplace_back来实际创建对象,而不仅仅是内存:

for(int i=0; i<20; i++){
       fooVector.emplace_back("Test",i);  // Alternatively 'push_back'
   }

除此之外,在C 11中,容器中使用的类型需要复制或移动构造函数.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读