gpt4 book ai didi

c++ - 引用 vector 和调整大小的能力

转载 作者:行者123 更新时间:2023-12-02 09:52:50 26 4
gpt4 key购买 nike

以下代码类似于此答案Why can't I make a vector of references?

class A 
{
public:
A(int x) : _x(x)
{
std::cout << "A : ctor\n";
}

void func()
{
std::cout << "I am func \n";
}

int getval()
{
return _x;
}

private:
int _x;
};

int main()
{
A a1(2);
A a2(3);

std::vector<std::reference_wrapper<A>> vec;
vec.push_back(std::ref(a1));
vec.push_back(std::ref(a2));

for(auto v : vec)
{
std::remove_reference<A&>::type(v).func();
std::cout << std::remove_reference<A&>::type(v).getval() << "\n";
}
}
我需要能够调整vec的大小(例如vec.resize(100))并具有类似
vec[n] = std::ref(b);
但是我会收到一个我不太了解的错误:
/usr/include/c++/7/bits/stl_construct.h: In instantiation of ‘void std::_Construct(_T1*, _Args&& ...) [with _T1 = std::reference_wrapper<A>; _Args = {}]’:
/usr/include/c++/7/bits/stl_uninitialized.h:527:18: required from ‘static _ForwardIterator std::__uninitialized_default_n_1<_TrivialValueType>::__uninit_default_n(_ForwardIterator, _Size) [with _ForwardIterator = std::reference_wrapper<A>*; _Size = long unsigned int; bool _TrivialValueType = false]’
/usr/include/c++/7/bits/stl_uninitialized.h:583:20: required from ‘_ForwardIterator std::__uninitialized_default_n(_ForwardIterator, _Size) [with _ForwardIterator = std::reference_wrapper<A>*; _Size = long unsigned int]’
/usr/include/c++/7/bits/stl_uninitialized.h:645:44: required from ‘_ForwardIterator std::__uninitialized_default_n_a(_ForwardIterator, _Size, std::allocator<_Tp>&) [with _ForwardIterator = std::reference_wrapper<A>*; _Size = long unsigned int; _Tp = std::reference_wrapper<A>]’
/usr/include/c++/7/bits/vector.tcc:563:35: required from ‘void std::vector<_Tp, _Alloc>::_M_default_append(std::vector<_Tp, _Alloc>::size_type) [with _Tp = std::reference_wrapper<A>; _Alloc = std::allocator<std::reference_wrapper<A> >; std::vector<_Tp, _Alloc>::size_type = long unsigned int]’
/usr/include/c++/7/bits/stl_vector.h:692:21: required from ‘void std::vector<_Tp, _Alloc>::resize(std::vector<_Tp, _Alloc>::size_type) [with _Tp = std::reference_wrapper<A>; _Alloc = std::allocator<std::reference_wrapper<A> >; std::vector<_Tp, _Alloc>::size_type = long unsigned int]’
<span class="error_line" onclick="ide.gotoLine('main.cpp',47)">main.cpp:47:19</span>: required from here
/usr/include/c++/7/bits/stl_construct.h:75:7: error: no matching function for call to ‘std::reference_wrapper::reference_wrapper()’
{ ::new(static_cast<void*>(__p)) _T1(std::forward<_Args>(__args)...); }
^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
In file included from /usr/include/c++/7/bits/shared_ptr_base.h:56:0,
from /usr/include/c++/7/bits/shared_ptr.h:52,
from /usr/include/c++/7/memory:81,
from main.cpp:5:
/usr/include/c++/7/bits/refwrap.h:340:7: note: candidate: constexpr std::reference_wrapper<_Tp>::reference_wrapper(const std::reference_wrapper<_Tp>&) [with _Tp = A]
reference_wrapper(const reference_wrapper&) = default;
^~~~~~~~~~~~~~~~~
/usr/include/c++/7/bits/refwrap.h:340:7: note: candidate expects 1 argument, 0 provided
/usr/include/c++/7/bits/refwrap.h:334:7: note: candidate: std::reference_wrapper<_Tp>::reference_wrapper(_Tp&) [with _Tp = A]
reference_wrapper(_Tp& __indata) noexcept
^~~~~~~~~~~~~~~~~
/usr/include/c++/7/bits/refwrap.h:334:7: note: candidate expects 1 argument, 0 provided
对于给定的示例,是否有办法具有此功能?
谢谢

最佳答案

error: no matching function for call to ‘std::reference_wrapper::reference_wrapper()’这表示未找到包装程序的默认构造函数。它没有一个,因为必须始终对引用进行初始化。std::vector<T>::resize使用此默认构造函数初始化新元素。考虑一下,该打印什么?

std::vector<std::reference_wrapper<A>> vec;
vec.resize(100); # There now must be 100 valid elements.
std::cout << vec[50].get().getval();
如果要使用默认语义,请使用指针。
如果您将调整大小称为优化,则只需使用 reserve + push_back即可。

关于c++ - 引用 vector 和调整大小的能力,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62960736/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com