如何检查 std::array 已声明但未明确初始化
how to check std::array is declared but not explicitly initialized
std::array
有一个内置方法 empty()
来检查数组是否为空。如从 here:
复制的示例所示
#include <array>
#include <iostream>
int main()
{
std::array<int, 4> numbers {3, 1, 4, 1};
std::array<int, 0> no_numbers;
std::cout << std::boolalpha;
std::cout << "numbers.empty(): " << numbers.empty() << '\n';
std::cout << "no_numbers.empty(): " << no_numbers.empty() << '\n';
}
是否有任何方法可以检查数组是否已声明,具有一些固定大小,但未明确初始化?
说,像这样的?
std::array<int,4> a;
std::array<int,4> b;
a = {1,2,3,4}; //a holds some explicit values
//do not assign values to b
//how to tell the different state of a and b?
没有。 std::array<T, N>::empty()
其中 N!=0 将始终 return false
。 https://en.cppreference.com/w/cpp/container/array/empty“检查容器是否没有元素,即是否 begin() == end()。”
同样 std::array<T, 0>::empty()
将始终 return true
,因为 begin() == end()
.
std::array
对象的大小(即它拥有的元素数量)必须 是编译时常量,不能动态改变。因此,empty()
和 size()
成员函数可能看起来有些多余 †(但出于与 STL 提供的基于容器的通用算法兼容的原因而包括在内) ).
如果你需要一个大小可以改变的容器,那么你应该使用std::vector
而不是std::array
(尽管这有更多的运行时间开销)。
以下说明了其中一个差异:
#include <iostream>
#include <array>
#include <vector>
int main()
{
std::array<int, 4> a;
std::array<int, 4> b;
a = { 1,2,3,4 }; //a holds some explicit values
std::cout << std::boolalpha;
std::cout << "a.empty(): " << a.empty() << '\n'; // false
std::cout << "b.empty(): " << b.empty() << '\n'; // false
// However ...
std::vector<int> v1;
std::vector<int> v2;
v1 = { 1,2,3,4 };
std::cout << "v1.empty(): " << v1.empty() << '\n'; // false
std::cout << "v2.empty(): " << v2.empty() << '\n'; // true
return 0;
}
† 但请注意,这些成员 是 用于将数组传递给函数的情况,否则将无法确定其给定参数的大小(或空性)。
我不明白你在问什么:
Are there any ways to check if the array is declared, with some fixed size, but not explicitly initialized?
如果你有一个std::array
类型的变量,那么它已经构造好了。
如果已经构造完成,那么数组中的元素就已经构造完成。
(这是在 array
构造函数中完成的)
这个变量std::array<std::string, 5> sa;
包含5个默认构造的字符串。
所以我不知道你所说的“已声明,但未明确初始化”是什么意思
std::array
有一个内置方法 empty()
来检查数组是否为空。如从 here:
#include <array>
#include <iostream>
int main()
{
std::array<int, 4> numbers {3, 1, 4, 1};
std::array<int, 0> no_numbers;
std::cout << std::boolalpha;
std::cout << "numbers.empty(): " << numbers.empty() << '\n';
std::cout << "no_numbers.empty(): " << no_numbers.empty() << '\n';
}
是否有任何方法可以检查数组是否已声明,具有一些固定大小,但未明确初始化?
说,像这样的?
std::array<int,4> a;
std::array<int,4> b;
a = {1,2,3,4}; //a holds some explicit values
//do not assign values to b
//how to tell the different state of a and b?
没有。 std::array<T, N>::empty()
其中 N!=0 将始终 return false
。 https://en.cppreference.com/w/cpp/container/array/empty“检查容器是否没有元素,即是否 begin() == end()。”
同样 std::array<T, 0>::empty()
将始终 return true
,因为 begin() == end()
.
std::array
对象的大小(即它拥有的元素数量)必须 是编译时常量,不能动态改变。因此,empty()
和 size()
成员函数可能看起来有些多余 †(但出于与 STL 提供的基于容器的通用算法兼容的原因而包括在内) ).
如果你需要一个大小可以改变的容器,那么你应该使用std::vector
而不是std::array
(尽管这有更多的运行时间开销)。
以下说明了其中一个差异:
#include <iostream>
#include <array>
#include <vector>
int main()
{
std::array<int, 4> a;
std::array<int, 4> b;
a = { 1,2,3,4 }; //a holds some explicit values
std::cout << std::boolalpha;
std::cout << "a.empty(): " << a.empty() << '\n'; // false
std::cout << "b.empty(): " << b.empty() << '\n'; // false
// However ...
std::vector<int> v1;
std::vector<int> v2;
v1 = { 1,2,3,4 };
std::cout << "v1.empty(): " << v1.empty() << '\n'; // false
std::cout << "v2.empty(): " << v2.empty() << '\n'; // true
return 0;
}
† 但请注意,这些成员 是 用于将数组传递给函数的情况,否则将无法确定其给定参数的大小(或空性)。
我不明白你在问什么:
Are there any ways to check if the array is declared, with some fixed size, but not explicitly initialized?
如果你有一个std::array
类型的变量,那么它已经构造好了。
如果已经构造完成,那么数组中的元素就已经构造完成。
(这是在 array
构造函数中完成的)
这个变量std::array<std::string, 5> sa;
包含5个默认构造的字符串。
所以我不知道你所说的“已声明,但未明确初始化”是什么意思