热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

std::end怎么知道数组的结束?-Howdoesstd::endknowtheendofanarray?

std::beginandstd::endknowthebeginningandendofacontaineroranarray.开始和结束知道容器或数组的开始和结束。

std::begin and std::end know the beginning and end of a container or an array.

开始和结束知道容器或数组的开始和结束。

It so easy to know the end and begin of a vector for example because it is a class that gives this information. But, how does it know the end of an array like the following?

例如,很容易知道向量的结束和开始,因为它是一个类来提供这些信息。但是,它是如何知道数组的末尾的呢?

int simple_array[5]{1, 2, 3, 4, 5};
auto beg=std::begin(simple_array);
auto en=std::end(simple_array);

std::begin is not that hard to know where the array start. But how does it know where it ends? Will the constant integer 5 be stored somewhere?

开始并不难知道数组从哪里开始。但它如何知道它的终点呢?常数整数5会被存储在某处吗?

I would appreciate if I got an answer with some low-level information.

如果我能得到一些低层次的信息,我将不胜感激。

3 个解决方案

#1


22  

is the constant integer 5 will be stored some where?

常数整数5会被存储在什么地方?

Yes, it's part of the type of the array. But no, it's not stored anywhere explicitly. When you have

是的,它是数组类型的一部分。但不,它没有显式地存储在任何地方。当你有

int i[5] = { };

the type of i is int[5]. Shafik's answer talks about how this length is used to implement end.

i的类型是int[5]。沙菲克的回答谈到了如何使用这个长度来实现end。

If you've C++11, using constexpr would be the simple way to go

如果您有c++ 11,那么使用constexpr将是最简单的方法

template 
inline constexpr size_t
arrLen(const T (&arr) [N]) {
    return N;
}

If you've a pre-C++11 compiler where constexpr isn't available, the above function may not be evaluated at compile-time. So in such situations, you may use this:

如果您有一个前c++ 11编译器,其中constexpr不可用,那么上面的函数可能不会在编译时进行计算。所以在这种情况下,你可以用这个:

template 
char (&arrLenFn(const T (&arr) [N]))[N];

#define arrLen(arr) sizeof(arrLenFn(arr))

First we declare a function returning a reference to an array of N chars i.e. sizeof this function would now be the length of the array. Then we've a macro to wrap it, so that it's readable at the caller's end.

首先,我们声明一个函数返回一个对N个字符的数组的引用,例如,这个函数的sizeof现在是数组的长度。然后我们有一个宏来包装它,这样它在调用者的端是可读的。

Note: Two arrays of the same base type but with different lengths are still two completely different types. int[3] is not the same as int[2]. Array decay, however, would get you an int* in both cases. Read How do I use arrays in C++? if you want to know more.

注意:两个基类型相同但长度不同的数组仍然是两个完全不同的类型。int[3]不同于int[2]。然而,在这两种情况下,数组衰变都会使您得到int*。阅读如何在c++中使用数组?如果你想知道更多。

#2


25  

But, how does it know the end of an array

但是,它如何知道数组的结束

It uses a template non-type parameter to deduce the size of the array, which can then be used to produce the end pointer. The C++11 signature from the cppreference section for std::end is as follows:

它使用一个模板非类型参数来推断数组的大小,然后可以使用它来生成最终指针。std:::的cppreference部分的c++ 11签名如下:

template
T* end( T (&array)[N] );

As hvd notes, since it is passed by reference this prevents decay to a pointer.

正如hvd所指出的,由于它是通过引用传递的,因此可以防止对指针的衰减。

The implementation would be something similar to:

执行情况将类似于:

template
T* end( T (&array)[N] )
{
    return array + N ;
}

Is the constant integer 5 will be stored some where?

常数整数5会被存储在什么地方?

5 or N is part of the type of the array and so N is available at compile time. For example applying sizeof to an array will give us the total number of bytes in the array.

5或N是数组类型的一部分,所以N在编译时可用。例如,将sizeof应用到一个数组中将给出数组中字节的总数。

Many times we see an array passed by value to a function. In that case, the array decays to a pointer to type stored in the array. So now the size information is lost. Passing by reference allows us to avoid this loss of information and extract the size N from the type.

很多时候我们看到一个数组通过值传递给一个函数。在这种情况下,数组会衰减到存储在数组中的类型的指针。所以现在大小信息丢失了。通过引用传递可以避免信息丢失,并从类型中提取大小为N的值。

#3


8  

Because you are passing an array to std::end, and an array has type T [N]. std::end can tell when the array ends by looking at the N in the type.

因为您正在将一个数组传递给std::end,而一个数组的类型是T [N]。end可以通过查看类型中的N来判断数组何时结束。


推荐阅读
  • 先看官方文档TheJavaTutorialshavebeenwrittenforJDK8.Examplesandpracticesdescribedinthispagedontta ... [详细]
  • 详解 Python 的二元算术运算,为什么说减法只是语法糖?[Python常见问题]
    原题|UnravellingbinaryarithmeticoperationsinPython作者|BrettCannon译者|豌豆花下猫(“Python猫 ... [详细]
  • 本文介绍了Hyperledger Fabric外部链码构建与运行的相关知识,包括在Hyperledger Fabric 2.0版本之前链码构建和运行的困难性,外部构建模式的实现原理以及外部构建和运行API的使用方法。通过本文的介绍,读者可以了解到如何利用外部构建和运行的方式来实现链码的构建和运行,并且不再受限于特定的语言和部署环境。 ... [详细]
  • C++字符字符串处理及字符集编码方案
    本文介绍了C++中字符字符串处理的问题,并详细解释了字符集编码方案,包括UNICODE、Windows apps采用的UTF-16编码、ASCII、SBCS和DBCS编码方案。同时说明了ANSI C标准和Windows中的字符/字符串数据类型实现。文章还提到了在编译时需要定义UNICODE宏以支持unicode编码,否则将使用windows code page编译。最后,给出了相关的头文件和数据类型定义。 ... [详细]
  • Python爬虫中使用正则表达式的方法和注意事项
    本文介绍了在Python爬虫中使用正则表达式的方法和注意事项。首先解释了爬虫的四个主要步骤,并强调了正则表达式在数据处理中的重要性。然后详细介绍了正则表达式的概念和用法,包括检索、替换和过滤文本的功能。同时提到了re模块是Python内置的用于处理正则表达式的模块,并给出了使用正则表达式时需要注意的特殊字符转义和原始字符串的用法。通过本文的学习,读者可以掌握在Python爬虫中使用正则表达式的技巧和方法。 ... [详细]
  • 本文讨论了微软的STL容器类是否线程安全。根据MSDN的回答,STL容器类包括vector、deque、list、queue、stack、priority_queue、valarray、map、hash_map、multimap、hash_multimap、set、hash_set、multiset、hash_multiset、basic_string和bitset。对于单个对象来说,多个线程同时读取是安全的。但如果一个线程正在写入一个对象,那么所有的读写操作都需要进行同步。 ... [详细]
  • 本文介绍了Python语言程序设计中文件和数据格式化的操作,包括使用np.savetext保存文本文件,对文本文件和二进制文件进行统一的操作步骤,以及使用Numpy模块进行数据可视化编程的指南。同时还提供了一些关于Python的测试题。 ... [详细]
  • 本文介绍了使用数据库管理员用户执行onstat -l命令来监控GBase8s数据库的物理日志和逻辑日志的使用情况,并强调了对已使用的逻辑日志是否及时备份的重要性。同时提供了监控方法和注意事项。 ... [详细]
  • 超级简单加解密工具的方案和功能
    本文介绍了一个超级简单的加解密工具的方案和功能。该工具可以读取文件头,并根据特定长度进行加密,加密后将加密部分写入源文件。同时,该工具也支持解密操作。加密和解密过程是可逆的。本文还提到了一些相关的功能和使用方法,并给出了Python代码示例。 ... [详细]
  • ejava,刘聪dejava
    本文目录一览:1、什么是Java?2、java ... [详细]
  • 本文介绍了利用ARMA模型对平稳非白噪声序列进行建模的步骤及代码实现。首先对观察值序列进行样本自相关系数和样本偏自相关系数的计算,然后根据这些系数的性质选择适当的ARMA模型进行拟合,并估计模型中的位置参数。接着进行模型的有效性检验,如果不通过则重新选择模型再拟合,如果通过则进行模型优化。最后利用拟合模型预测序列的未来走势。文章还介绍了绘制时序图、平稳性检验、白噪声检验、确定ARMA阶数和预测未来走势的代码实现。 ... [详细]
  • Introduction(简介)Forbeingapowerfulobject-orientedprogramminglanguage,Cisuseda ... [详细]
  • 本博文基于《Amalgamationofproteinsequence,structureandtextualinformationforimprovingprote ... [详细]
  • Non-ASCIIhelponitsownisOK: ... [详细]
  • 3.5.2Calc的公式语法:使用Calc计算一个公式可用是任何能够被Emacs的calc包所识别的代数表达式.注意,在Calc中,的操作符优先级要比*低,因此ab*c会被解释为a ... [详细]
author-avatar
小晶晶妮妮宝贝
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有