如何在 C++ 中合法地声明向量变量?
Posted
技术标签:
【中文标题】如何在 C++ 中合法地声明向量变量?【英文标题】:How to legally declare vector variables in C++? 【发布时间】:2020-08-05 05:56:29 【问题描述】:我是 C++ 新手,并试图了解声明向量的工作原理。下面是一些例子:
std::vector j;
std::vector<char[256]> k;
std::vector<double> v;
std::vector<std::vector<int>> s;
我的想法是只有 std::vector<double> v
是合法的,我自己测试了它,只有这行代码编译没有错误。但显然所有这些都是允许的。有人可以解释一下其他人是如何工作的,以及为什么其他人在我运行它时会给我错误吗?
所以如果我运行这段代码:
#include <iostream>
#include <vector>
int main()
//std::vector j;
std::vector<char[256]> k;
std::vector<double> v;
std::vector<std::vector<int>> s;
我在编译时遇到一堆错误(我在 Xcode 中使用 Atom g++ 编译器包):
C++ - test.cpp:12
In file included from <built-in>:2:
In file included from /Library/Developer/CommandLineTools/usr/bin/../include/c++/v1/iostream:37:
In file included from /Library/Developer/CommandLineTools/usr/bin/../include/c++/v1/ios:215:
In file included from /Library/Developer/CommandLineTools/usr/bin/../include/c++/v1/__locale:14:
In file included from /Library/Developer/CommandLineTools/usr/bin/../include/c++/v1/string:504:
In file included from /Library/Developer/CommandLineTools/usr/bin/../include/c++/v1/string_view:175:
In file included from /Library/Developer/CommandLineTools/usr/bin/../include/c++/v1/__string:56:
In file included from /Library/Developer/CommandLineTools/usr/bin/../include/c++/v1/algorithm:643:
/Library/Developer/CommandLineTools/usr/bin/../include/c++/v1/memory:1880:61: error: object expression of non-scalar type 'char [256]' cannot be used in a pseudo-destructor expression
_LIBCPP_INLINE_VISIBILITY void destroy(pointer __p) __p->~_Tp();
~~~^
/Library/Developer/CommandLineTools/usr/bin/../include/c++/v1/memory:1742:18: note: in instantiation of member function 'std::__1::allocator<char [256]>::destroy' requested here
__a.destroy(__p);
^
/Library/Developer/CommandLineTools/usr/bin/../include/c++/v1/memory:1595:14: note: in instantiation of function template specialization 'std::__1::allocator_traits<std::__1::allocator<char [256]> >::__destroy<char [256]>' requested here
__destroy(__has_destroy<allocator_type, _Tp*>(), __a, __p);
^
/Library/Developer/CommandLineTools/usr/bin/../include/c++/v1/vector:426:25: note: in instantiation of function template specialization 'std::__1::allocator_traits<std::__1::allocator<char [256]> >::destroy<char [256]>' requested here
__alloc_traits::destroy(__alloc(), _VSTD::__to_raw_pointer(--__soon_to_be_end));
^
/Library/Developer/CommandLineTools/usr/bin/../include/c++/v1/vector:369:29: note: in instantiation of member function 'std::__1::__vector_base<char [256], std::__1::allocator<char [256]> >::__destruct_at_end' requested here
void clear() _NOEXCEPT __destruct_at_end(__begin_);
^
/Library/Developer/CommandLineTools/usr/bin/../include/c++/v1/vector:463:9: note: in instantiation of member function 'std::__1::__vector_base<char [256], std::__1::allocator<char [256]> >::clear' requested here
clear();
^
/Library/Developer/CommandLineTools/usr/bin/../include/c++/v1/vector:495:5: note: in instantiation of member function 'std::__1::__vector_base<char [256], std::__1::allocator<char [256]> >::~__vector_base' requested here
vector() _NOEXCEPT_(is_nothrow_default_constructible<allocator_type>::value)
^
/Users/moeheinag/Desktop/C++/UIUC/Course1/test.cpp:7:24: note: in instantiation of member function 'std::__1::vector<char [256], std::__1::allocator<char [256]> >::vector' requested here
std::vector<char[256]> k;
^
1 error generated.
[Finished in 0.375s]
【问题讨论】:
【参考方案1】:std::vector
的固定数组模板在 C++03 之前(包括 C++03)是不允许的。这是因为数组既不是 CopyAssignable 也不是 CopyConstructible。
所以std::vector<char[256]> k;
在 C++11 之前是无效的。从 C++11 开始,你可以声明这样的类型,尽管你会很难使用这样的实例化——例如push_back
将不起作用,但炮台会。使用std::array<char, 256>
代替char[256]
是一个明显的解决方法。
还要注意,C++11 之前的 std::vector<std::vector<int>> s;
也是无效的 - 您需要在 >>
之间留一个空格,否则编译器会将 >>
视为按位移位!
换句话说,升级到 C++11。
【讨论】:
【参考方案2】:但显然所有这些都是允许的。有人可以解释一下其他人的工作原理吗?
std::vector j; // invalid
std::vector<char[256]> k; // ok
std::vector<double> v; // ok
std::vector<std::vector<int>> s; // ok
实际上第一个是无效的,其他的都是有效的,因为 std::vector 是模板化的。您必须在声明时提供类型。
【讨论】:
以上是关于如何在 C++ 中合法地声明向量变量?的主要内容,如果未能解决你的问题,请参考以下文章