【问题标题】:C++ for loop with multiple pointer initializers具有多个指针初始值设定项的 C++ for 循环
【发布时间】:2019-09-27 09:40:11
【问题描述】:

在下面的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";

我不喜欢将 * 放在 b 之前,但当然需要区分 Block 和 Block*。还有另一种方法可以做到这一点吗? for ((Block*) a, b... 不行。

【问题讨论】:

  • 不完全相同的问题,但仍然非常接近重复Declaring multiple object pointers on one line causes compiler error
  • 是的,这是一个很好的观点,我已经编辑了这个问题。
  • @TopologicalSort 当您编辑问题并更改它时,请在单独的部分中进行(或者如果完全是另一个部分,则打开一个新部分 - 女巫不是这里的情况),所以相关的答案对于原来的问题,会一直这样......
  • @KorelK 不,不要那样做。我们不想要变化的时间表。我们可以在帖子的历史中看到旧的修订。 将更新整合到帖子的流程中, 就像 OP 在此处所做的那样。

标签: c++


【解决方案1】:

你可以这样做:

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

【讨论】:

  • 这可能是最好的选择,只要您希望它们都是相同的类型。 +1。
  • 我不是隐藏 tbh 类型的忠实粉丝,但 aListbList 在词汇上很接近,所以我想没关系
【解决方案2】:

如果您不想重复*,那么您可以使用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";
}

或转发auto:

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";
}

【讨论】:

    【解决方案3】:

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

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

    【讨论】:

      【解决方案4】:

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

      Block* a = aList, b = bList;
      

      它恰好是 for 循环的一部分,但就像

      int * a, * b;
      

      是两个int指针,可以使用

      Block* a = aList, * b = bList;
      

      在您的 for 循环中。

      【讨论】:

        【解决方案5】:

        是的,只需使用:

        Block* a = aList, *b = bList
        

        编辑:

        选项 1 - 使用 Boost

        #include <boost/typeof/typeof.hpp>
        /*
            ...
        */
        for (BOOST_TYPEOF_KEYWORD(Block*) a = aList, b = bList;...)
        

        另一种选择是创建一个你想要的类型的变量,并使用它的类型来初始化其他变量(类似于auto):

        选项 2

        Block* aList = new Block(1);
        Block* bList = new Block(2);
        
        for (decltype(aList) a = aList, b = bList; ...) ...
        

        选项 3 - 使用 Boost

        #include <boost/typeof/typeof.hpp>
        /*
            Like the first option
        */
        for (BOOST_TYPEOF(aList) a = aList, b = bList;...) ...
        // ...
        

        【讨论】:

          【解决方案6】:

          只定义一个type alias 怎么样?

          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";
          

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2013-05-27
            • 2020-01-13
            • 1970-01-01
            • 2020-07-20
            • 1970-01-01
            • 2022-11-20
            相关资源
            最近更新 更多