c++ - 我可以在 C++ 的 for 循环中使用指针吗?
问题描述
我知道我可以在 for 循环中使用任何类型来迭代:
#include <fstream>
#include <iostream>
using namespace std;
int main()
{
int ar[] ={ 1, 2, 3 };
for (int i:ar)
{
cout << i << endl;
}
}
但我不能有一个指针类型:
#include <fstream>
#include <iostream>
#include <string>
using namespace std;
int main(int argc, char const *argv[])
{
for (char *p:argv) //or better char const *p
// using auto keyword expands to error-type in vscode
{
ifstream in(p);
}
return 0;
}
会给:
error: ‘begin’ was not declared in this scope
for (char *p:argv)
error: ‘end’ was not declared in this scope
for (char *p:argv)
^~~~
所以我假设,c++ for loop 的语法(auto var : vector/array)
与c
-style, old-fashion loop不同for(int i=0; i<size; i++)
?因为我需要(在使用 c++ 风格的循环的情况下)提供一个具有有效迭代器的结构(因此错误,寻找 begin() 和 end() 迭代器。但是为什么第一种情况有效?第一种情况,一个带有整数的数组,也是一个没有任何迭代器的结构,但是旧指针(访问)。那么为什么第一个可以,但第二个不行?
解决方案
这些样本的差异隐藏在将参数传递给函数的规则中。
在第一个示例中,当您编写 时int ar[] = {1, 2, 3};
,类型ar
是int[3]
- “一个 3 个整数的数组”。std::begin
是为数组定义的,因此代码可以编译并工作。
在第二个示例中,char const *argv[]
实际上与 相同 char const **argv
,因为在函数参数中,您不能按值传递数组,并且语法[]
的编译方式与您使用的完全一样*
。显然,没有std::begin
for指针,所以代码不起作用。
要遍历参数,您必须使用普通的 for 循环。例如,
for (int i = 0; i < argc; ++i) {
char const* arg = argv[i];
};
编辑:只是为了澄清 - 使用基于范围的 for 循环,std::begin(a)
并且std::end(a)
应该是可调用的并返回一个迭代器。(由于 C++17 并不完全正确 -std::end
可以返回与迭代器相当的任何东西,这在 C++20 工作草案中称为哨兵)
如果是具有已知边界的数组(例如int[3]
),std::begin
则返回指向第一个元素的指针,并std::end
返回结束后的指针。C++ 中的 aniterator
不必是某个特殊基类的派生类,它只需要具有正确的语义即可。指针也是迭代器。
推荐阅读
- python - matplotlib 按钮未显示在同一窗口中
- php - PHP:它想要一个“对象”,所以我给它和对象,然后它错误地说它想要一个“数组”,给出了什么?
- javascript - 在实践中确保算法仅在需要时启动函数或仅在 jquery 中启动函数是否更好
- hadoop - Hive 命令行 - 列名中的反引号问题
- react-native - react native 中的 youtube 数据 api
- java - 使用标记将表格添加到同一页面上的现有 PDF - ITEXT
- neo4j - 在线访问 Neo4J 浏览器时出错。我得到 ServiceUnavailable:WebSocket 连接失败
- sql - Impala/Hive 填充类似于 LOCF 的缺失值(最后一次观察结转)
- skype-for-business - Microsoft Teams 用户状态应用程序无法正常工作
- ios - 核心数据 - 多个表视图中的相同实体