我知道如何在 for 循环中使用 auto 关键字来按值或引用迭代此数组。
struct A {
void fun() {};
};
int main() {
A a[2];
// Value
for (auto x : a) {
x.fun();
}
// Ref
for (auto& x : a) {
x.fun();
}
// Pointer
//for (...) {
x->fun();
}
}
所以我正在寻找这个约定的第三个版本。我如何在这里使用指针?
最佳答案
A a[2];
for(auto& x_:a){
auto* x = &x_;
// code
}
关于C++11 基于范围的按值、引用和指针的自动 for 循环,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20239507/