gpt4 book ai didi

c++ - 如何在 C++ 中合法声明 vector 变量?

转载 作者:行者123 更新时间:2023-12-01 15:10:16 25 4
gpt4 key购买 nike

我是 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/

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