我是 C++ 新手,试图了解声明 vector 的工作原理。以下是一些示例:
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]
最佳答案
std::vector
的固定数组模板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。
关于c++ - 如何在C++中合法地声明 vector 变量?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63259214/