c ++插入队列向量

时间:2014-12-04 16:00:29

标签: c++ vector insert queue

我尝试这样做是为了在队列矢量中插入一个产品,但只是逐个填充,直到产品的产品已满。

如何使方法插入平衡存储中的产品,并且在最大容量之前不要逐个填充?

队列矢量:

vector < queue <Product*> > depFresh;

我的Deposit类和DepositFresh类之间存在遗产。我的DepositFresh继承了我的Deposit类中声明的所有字段。 在我的存款类中,我声明:

Class Deposit {
private:
    int number_Palets_Storage;
    int max_Capacity_Palet;
    int total_Area_Deposit;
public:
    virtual void insertProduct(const Produto *produto) =0;
}

我的类DepositFresh包含以下方法:

方法插入:

void DepositFresh::insertProduct(const Product *product) {
        if (depFresh.size() == getNumber_Palets_Storage())
            for (int i = 0; i < depFresh.size(); i++) {
                if (depFresh.at(i).size() < getMax_Capacity__Palet())
                    depFresh.at(i).push(product->clone());
                break;
            }
}

3 个答案:

答案 0 :(得分:0)

您是否尝试将Product插入所有队列,直到它们全部满了?听起来像

int i = 0;
while(true)
{
 if (depFresh.at(i).size() < getMax_Capacity__Palet())
                    depFresh.at(i).push(product->clone());
 else
     break;
i = (i + 1) / depFresh.size();
}

但为什么不呢

for(int i = 0; i < depFresh.size(); ++i)
    for (int j = 0; j < getMax_Capacity__Palet() ; ++j)
       depFresh.at(i).push(product->clone());

答案 1 :(得分:0)

Imo最简单的方法是以循环方式存储新对象。为此,您可以存储索引,该索引会记住您上次存储内容的位置。将某些东西存储在队列中后,您可以执行类似

的操作

insertion_index = (insertion_index + 1) % v.size();

在下次通话中,下一个队列将存储下一个产品。

这是人们可以想到的最简单的平衡策略,但是也可以使用优先级队列而不是向量来始终插入具有最少元素的队列,使用队列的大小作为优先级(低=高优先级)。

答案 2 :(得分:0)

如果我了解您的问题,您希望将产品插入向量中的最低容量队列。为什么不搜索最小尺寸并相应推动?

#include <vector>
#include <queue>
#include <iostream>
#include <string>
#include <algorithm>

class myContainer 
{
private:
    std::vector< std::queue< long > > depFresh;
public:
    myContainer( size_t queue_cnt ) : depFresh( queue_cnt )
    {
    }
    void insertProduct( long product )
    {
        auto iter = std::min_element( depFresh.begin(), depFresh.end(), []( std::queue< long > & l, std::queue< long > & r ){ return l.size() < r.size(); } );
        iter->push( product );
    }
    void dump()
    {
        size_t index = 0;
        std::cout << "---------------------------" << std::endl;
        for( const auto & o : depFresh )
        {
            std::cout << ++index << " has size " << o.size() << '\n';
        }
        std::cout << "---------------------------" << std::endl;
    }
};

int main()
{
    myContainer container( 5 );
    for( long x = 0; x < 40; ++x )
    {
        container.insertProduct( x ); 
        container.dump();
    }
}

这会产生:

---------------------------
1 has size 1
2 has size 0
3 has size 0
4 has size 0
5 has size 0
---------------------------
---------------------------
1 has size 1
2 has size 1
3 has size 0
4 has size 0
5 has size 0
---------------------------
---------------------------
1 has size 1
2 has size 1
3 has size 1
4 has size 0
5 has size 0
---------------------------
---------------------------
1 has size 1
2 has size 1
3 has size 1
4 has size 1
5 has size 0
---------------------------
---------------------------
1 has size 1
2 has size 1
3 has size 1
4 has size 1
5 has size 1
---------------------------
---------------------------
1 has size 2
2 has size 1
3 has size 1
4 has size 1
5 has size 1
---------------------------
---------------------------
1 has size 2
2 has size 2
3 has size 1
4 has size 1
5 has size 1
---------------------------
---------------------------
1 has size 2
2 has size 2
3 has size 2
4 has size 1
5 has size 1
---------------------------
---------------------------
1 has size 2
2 has size 2
3 has size 2
4 has size 2
5 has size 1
---------------------------
---------------------------
1 has size 2
2 has size 2
3 has size 2
4 has size 2
5 has size 2
---------------------------
---------------------------
1 has size 3
2 has size 2
3 has size 2
4 has size 2
5 has size 2
---------------------------
---------------------------
1 has size 3
2 has size 3
3 has size 2
4 has size 2
5 has size 2
---------------------------
---------------------------
1 has size 3
2 has size 3
3 has size 3
4 has size 2
5 has size 2
---------------------------
---------------------------
1 has size 3
2 has size 3
3 has size 3
4 has size 3
5 has size 2
---------------------------
---------------------------
1 has size 3
2 has size 3
3 has size 3
4 has size 3
5 has size 3
---------------------------
---------------------------
1 has size 4
2 has size 3
3 has size 3
4 has size 3
5 has size 3
---------------------------
---------------------------
1 has size 4
2 has size 4
3 has size 3
4 has size 3
5 has size 3
---------------------------
---------------------------
1 has size 4
2 has size 4
3 has size 4
4 has size 3
5 has size 3
---------------------------
---------------------------
1 has size 4
2 has size 4
3 has size 4
4 has size 4
5 has size 3
---------------------------
---------------------------
1 has size 4
2 has size 4
3 has size 4
4 has size 4
5 has size 4
---------------------------
---------------------------
1 has size 5
2 has size 4
3 has size 4
4 has size 4
5 has size 4
---------------------------
---------------------------
1 has size 5
2 has size 5
3 has size 4
4 has size 4
5 has size 4
---------------------------
---------------------------
1 has size 5
2 has size 5
3 has size 5
4 has size 4
5 has size 4
---------------------------
---------------------------
1 has size 5
2 has size 5
3 has size 5
4 has size 5
5 has size 4
---------------------------
---------------------------
1 has size 5
2 has size 5
3 has size 5
4 has size 5
5 has size 5
---------------------------
---------------------------
1 has size 6
2 has size 5
3 has size 5
4 has size 5
5 has size 5
---------------------------
---------------------------
1 has size 6
2 has size 6
3 has size 5
4 has size 5
5 has size 5
---------------------------
---------------------------
1 has size 6
2 has size 6
3 has size 6
4 has size 5
5 has size 5
---------------------------
---------------------------
1 has size 6
2 has size 6
3 has size 6
4 has size 6
5 has size 5
---------------------------
---------------------------
1 has size 6
2 has size 6
3 has size 6
4 has size 6
5 has size 6
---------------------------
---------------------------
1 has size 7
2 has size 6
3 has size 6
4 has size 6
5 has size 6
---------------------------
---------------------------
1 has size 7
2 has size 7
3 has size 6
4 has size 6
5 has size 6
---------------------------
---------------------------
1 has size 7
2 has size 7
3 has size 7
4 has size 6
5 has size 6
---------------------------
---------------------------
1 has size 7
2 has size 7
3 has size 7
4 has size 7
5 has size 6
---------------------------
---------------------------
1 has size 7
2 has size 7
3 has size 7
4 has size 7
5 has size 7
---------------------------
---------------------------
1 has size 8
2 has size 7
3 has size 7
4 has size 7
5 has size 7
---------------------------
---------------------------
1 has size 8
2 has size 8
3 has size 7
4 has size 7
5 has size 7
---------------------------
---------------------------
1 has size 8
2 has size 8
3 has size 8
4 has size 7
5 has size 7
---------------------------
---------------------------
1 has size 8
2 has size 8
3 has size 8
4 has size 8
5 has size 7
---------------------------
---------------------------
1 has size 8
2 has size 8
3 has size 8
4 has size 8
5 has size 8
---------------------------
</pre>