c++ - 打印结构双端队列中的第一个数组

标签 c++ arrays for-loop iterator deque

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

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

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

deque<New_Array> Mydeque;

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

我需要为每个 h 打印一个数组。h=0h=steps(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 are 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 takeing 1 arguments

编辑:deque 未排序。对于每个 h,都应打印一个 array。此数组 应该是此刻 更接近双端队列末尾的那个。

最佳答案

记住最后一个值并跳过:

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

关于c++ - 打印结构双端队列中的第一个数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11083308/

相关文章:

javascript - 如何在 Phaser 3 中将一组对象移动相同的距离?

java - 最快的数组比较

javascript - AngularJS:如何创建一个模型来保存输入字段动态列表的数组?

c++ - 在循环中创建 100 个具有不同名称的对象

c++运行并发for循环,甚至可能吗?

c++ - 通用内核扩展无法释放 OSObject 派生类

c++ - 如何使用 C++0x 支持构建 Boost?

c++ - 在 c++ armv8 中是否有用于打开/关闭清零模式的编译器标志?

c++ - C++中使用跳转表实现switch语句

C++基于范围的for循环对valarray rvalue不起作用