C++ C++;具有多个指针初始值设定项的for循环

C++ C++;具有多个指针初始值设定项的for循环,c++,C++,在下面的for循环中: struct Block { Block(int d) : data(d), next(nullptr) {} int data; Block* next; }; Block* aList = new Block(1); Block* bList = new Block(2); for (Block* a = aList, *b = bList; a != nullptr; a = a->next, b = b->next)

在下面的for循环中:

struct Block
{
    Block(int d) : data(d), next(nullptr) {}

    int data;
    Block* next;
};

Block* aList = new Block(1);
Block* bList = new Block(2);

for (Block* a = aList, *b = bList; a != nullptr; a = a->next, b = b->next)
    if (aList->data != bList->data)
        cout << "Difference found.\n";
struct块
{
块(intd):数据(d),下一个(nullptr){
int数据;
块*下一步;
};
块*aList=新块(1);
Block*bList=新块(2);
对于(块*a=aList,*b=bList;a!=nullptr;a=a->next,b=b->next)
如果(列表->数据!=bList->数据)
不能是的,只需使用:

Block* a = aList, *b = bList

编辑:

选项1-使用Boost 选项3-使用Boost
#包括
/*
就像第一个选择
*/
对于(BOOST_TYPEOF(aList)a=aList,b=bList。。。
// ...

声明指针时,
*
属于名称,而不是类型。这意味着您可以使
b
成为类似

for (Block *a = aList, *b = bList; a != nullptr; a = a->next, b = b->next)

您可以这样做:

for (auto a = aList, b = bList; a != nullptr; a = a->next, b = b->next)
    if (aList->data != bList->data)
        cout << "Difference found.\n";
for(自动a=aList,b=bList;a!=nullptr;a=a->next,b=b->next)
如果(列表->数据!=bList->数据)

您试图在表达式中声明两个指针

Block* a = aList, b = bList;
它恰好是
for
循环的一部分,但与

int * a, * b;
是两个
int
指针,您可以使用

Block* a = aList, * b = bList;

for
循环中。

如果不想重复
*
,则可以使用
using
创建别名
BlockPtr
,而不是
Block*

int main() {
  using BlockPtr = Block*;

  BlockPtr aList = new Block(1);
  BlockPtr bList = new Block(2);

  for (BlockPtr a = aList, b = bList; a != nullptr; a = a->next, b = b->next)
    if (aList->data != bList->data)
      cout << "Difference found.\n";
}

那就定义一个

使用BlockPtr=Block*;
对于(BlockPtr a=aList,b=bList;a!=nullptr;a=a->next,b=b->next)
如果(列表->数据!=bList->数据)

不能完全相同的问题,但仍然非常接近于重复的问题是的,这是一个很好的观点,我已经编辑了该问题。@TopologicalSort当您编辑并更改该问题时,请在单独的部分中进行编辑(如果完全是另一个问题,请打开一个新的部分-此处不是这种情况),因此,与原始问题相关的答案将保持不变…@KorelK不,不要这样做。我们不想要一个变化的时间表。我们可以在《华盛顿邮报》的历史中看到旧版本。将更新整合到文章流中,就像OP在这里所做的那样。这可能是最好的选择,只要你希望它们都是相同的类型+1.我不太喜欢隐藏tbh类型,但是
aList
bList
在词汇上很接近,所以我想这没关系
int * a, * b;
Block* a = aList, * b = bList;
int main() {
  using BlockPtr = Block*;

  BlockPtr aList = new Block(1);
  BlockPtr bList = new Block(2);

  for (BlockPtr a = aList, b = bList; a != nullptr; a = a->next, b = b->next)
    if (aList->data != bList->data)
      cout << "Difference found.\n";
}
int main() {

  auto aList = new Block(1);
  auto bList = new Block(2);

  for (auto a = aList, b = bList; a != nullptr; a = a->next, b = b->next)
    if (aList->data != bList->data)
      cout << "Difference found.\n";
}
using BlockPtr = Block*;

for (BlockPtr a = aList, b = bList; a != nullptr; a = a->next, b = b->next)
    if (aList->data != bList->data)
        cout << "Difference found.\n";