如何找出数组的长度? [英] How to find out the length of an array?
本文介绍了如何找出数组的长度?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
std::string array[] = { "one", "two", "three" };
如何找出数组的长度$ c $
您可以使用 std :: begin c>在代码中
解决方案
推荐答案
int len = std::end(array)-std::begin(array);
// or std::distance(std::begin(array, std::end(array));
或者,您可以编写自己的模板函数:
Alternatively, you write your own template function:
template< class T, size_t N >
size_t size( const T (&)[N] )
{
return N;
}
size_t len = size(array);
这将在C ++ 03中工作。如果你在C ++ 11中使用它,将值得使它 constexpr
。
This would work in C++03. If you were to use it in C++11, it would be worth making it a constexpr
.
这篇关于如何找出数组的长度?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文