【问题标题】:Printing the First Array in a Deque of Structs在结构的双端队列中打印第一个数组
【发布时间】:2012-06-18 12:52:53
【问题描述】:

我有一个包含这种结构的双端队列。

struct New_Array {                    
    array<array<int,4>,4> mytable;       
    int h;
};

在这个结构中,2 个不同的数组可能具有相同的 h 值。

deque<New_Array> Mydeque;

我也知道双端队列中有多少个不同的 h(steps 的值)。以及双端队列中有多少个结构(Mydeque.size())。

我需要为每个 h 打印一个数组。h=0h=steps(步数是已知的 int 值)。 要打印的每个数组都必须离双端队列的末尾越近。

我尝试过这样的事情:

void foo(deque<New_Array> Mydeque, int steps)
 for(int i=0; i<steps; i++)
     {
        deque<New_Array>::iterator it;
        it = find(Mydeque.begin(),Mydeque.end(),i);
        PrintBoard(*it); // This if a function where you enter the New_Array struct 
                         // and it prints the array         
     }
}

上面给了我:error C2679: binary '==' : no operator found which takes a right-hand operand of type 'const bool' (or there is no acceptable conversion)

或者是这样的:

void foo(deque<New_Array> Mydeque, int steps)
for(int i=0; i<steps; i++)
     {
        deque<New_Array>::iterator it;
        for(unsigned int j=0;j<Mydeque.size();j++)
            {
            it = find_if(Mydeque.begin(),Mydeque.end(),Mydeque[j].h==i);
            PrintBoard(*it);
            break;
            }           

     }

上面给了我:error C2064: term does not evaluate to a function taking 1 arguments

编辑deque 未排序。对于每个h,应打印一个array。这个array 应该是此时接近双端队列末尾的那个。

【问题讨论】:

    标签: c++ arrays for-loop iterator deque


    【解决方案1】:

    记住最后一个值并跳过:

    assert(!Mydeque.empty());
    int old_h = Mydeque[0].h + 1; // make sure it's different!
    
    for (std::size_t i = 0, end != Mydeque.size(); i != end; ++i)
    {
        if (Mydeque[i].h == old_h) continue;
    
        print(Mydeque[i]);
        old_h = Mydeque[i].h;
    }
    

    【讨论】:

    • 假设双端队列没有排序,没有比从头开始更快的方法了。
    • 仅当deque 已排序时才有效,我看不到任何迹象。
    • @Kerrek 如果双端队列中 h 的值是这样的: 0, 5 , 2, 1 ,4 ,您的代码将只打印双端队列的第一个和第四个元素。
    • 好吧,那没关系。我以为原来的集合已经排序了。
    【解决方案2】:

    首先,请注意您在堆栈上声明了您的std::array,因此存储空间也将在堆栈上。这意味着迭代结构涉及为每次比较加载 (4*4+1)*int。如果这对性能敏感,我建议使用std::vector,因为在仅比较h 时,负载将仅是外部向量指针和h

    struct New_Array {                    
        vector<vector<int,4>,4> mytable;
        int h;
    };
    

    其次,如果您需要通过它们的h 值访问这些表,或者一次访问具有给定h 的所有表,请让每个人都更容易,并将它们作为向量存储在地图中,或者排序向量的向量:

    std::map<int,std::vector<New_Array> > rolodex;
    rolodex[someNewArray.h].push_back(someNewArray);
    

    如果你按顺序构造,那么每个向量中的第一项将是要打印的:

    for(auto it : rolodex) {
        vector<New_Array> tablesForThisH = it->second;
        if(tablesForThisH.begin() != tablesForThisH.end())
           PrintBoard(it->second[0]);
    }
    

    由于std:map 按升序(我认为)存储(并迭代)其键,这将按升序遍历不同的h 值。同样,它只需要加载堆栈存储的结构,它只是 h int 和向量头(可能是 12 个字节,如 this question 中所述)。

    如果代码有误,请见谅,我的stl有点生疏了。

    【讨论】:

      【解决方案3】:

      循环遍历deque,并将所有元素插入map,使用h 作为键。由于您的 h 值集似乎是连续的,因此您可以改用 vector,但测试是否已找到元素会更加困难。

      【讨论】:

        【解决方案4】:

        解决办法是:

        void Find_Solution_Path(deque<New_Array> Mydeque, int steps)
        {
        for(int i=0; i<steps+1; i++)
            {
                for(int j=Mydeque.size()-1;j>-1;j--)
                {
                    if (Mydeque[j].h==i)
                    {
                        PrintBoard(Mydeque[j]);
                        cout<<endl;
                        break;
                    }    
                }    
            }
        }
        

        【讨论】:

          猜你喜欢
          • 2023-04-02
          • 2020-01-07
          • 1970-01-01
          • 2017-04-03
          • 2016-05-11
          • 2015-04-26
          • 2018-08-23
          • 1970-01-01
          • 2016-09-25
          相关资源
          最近更新 更多