C++ 如何将 pair<int, int> 插入队列?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5643659/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-28 18:34:58  来源:igfitidea点击:

how to insert pair<int, int> into queue?

c++templates

提问by jlehenbauer

i am having trouble inserting objects of type pair<int, int>into a queue. i am getting a strange error, and i have no idea how to go about fixing it. Can anyone help? the following is the code for the method, followed by the error messages. The first two errors are for the insert, the last is for the usage of the operator=, help with that too would be appreciated. Thanks!

我在将类型的对象插入pair<int, int>队列时遇到问题。我收到一个奇怪的错误,我不知道如何修复它。任何人都可以帮忙吗?以下是该方法的代码,后跟错误消息。前两个错误是针对插入的,最后一个是针对 的使用,operator=对此也有帮助,我们将不胜感激。谢谢!

pair<int,int>* bfSpanningTree(int theVertex)
{
    queue< pair<int,int> > pairq;
    queue<int> nodeq;
    if(linkedAdjacencyList[theVertex]->value == theVertex && linkedAdjacencyList[theVertex]->adj != NULL)
    {
        Node* whereto;
        whereto = linkedAdjacencyList[theVertex]->adj;
        while(whereto->adj != NULL)
        {
            pairq.push(pair< &whereto->value, &whereto->adj->value >);
            nodeq.push(whereto->value);
            whereto = whereto->adj;
        }
        while(!nodeq.empty())
        {
            whereto = linkedAdjacencyList[theVertex]->adj;
            while(whereto->adj != NULL)
            {
                pairq.push(pair<&whereto->value, &whereto->adj->value>);
                whereto = whereto->adj;
            }
        }
    }
    int i = 0;
    pair<int,int>* retVal;
    pair<int,int> tree[pairq.size()];
    while(!pairq.empty())
    {
        tree[i] = pairq.pop();
        i++;
    }
    retVal = tree;
    return retVal;
}

~UndirectedGraph()
{
    for (int i = 0; i < numVerticies; i++)
        delete[] linkedAdjacencyList[i];
}

errors:

错误:

hw8.h:181: error: wrong number of template arguments (1, should be 2)

/usr/include/c++/4.4/bits/stl_pair.h:67: error: provided for ‘template<class _T1, class _T2> struct std::pair'

hw8.h:190: error: wrong number of template arguments (1, should be 2)

/usr/include/c++/4.4/bits/stl_pair.h:67: error: provided for ‘template<class _T1, class _T2> struct std::pair'

hw8.h:200: error: no match for ‘operator='in ‘tree[i] = pairq.std::queue<_Tp, _Sequence>::pop [with _Tp = std::pair<int, int>, _Sequence = std::deque<std::pair<int, int>, std::allocator<std::pair<int, int> > >]()'

/usr/include/c++/4.4/bits/stl_pair.h:68: note: candidates are: std::pair<int, int>& std::pair<int, int>::operator=(const std::pair<int, int>&)

hw8.h:181: 错误:模板参数数量错误(1,应该是 2)

/usr/include/c++/4.4/bits/stl_pair.h:67: 错误:提供 ‘template<class _T1, class _T2> struct std::pair'

hw8.h:190: 错误:模板参数数量错误(1,应该是 2)

/usr/include/c++/4.4/bits/stl_pair.h:67: 错误:提供 ‘template<class _T1, class _T2> struct std::pair'

hw8.h:200: 错误:不匹配‘operator='in‘tree[i] = pairq.std::queue<_Tp, _Sequence>::pop [with _Tp = std::pair<int, int>, _Sequence = std::deque<std::pair<int, int>, std::allocator<std::pair<int, int> > >]()'

/usr/include/c++/4.4/bits/stl_pair.h:68: 注意:候选对象是: std::pair<int, int>& std::pair<int, int>::operator=(const std::pair<int, int>&)

回答by Michael Burr

Lines of code like so:

像这样的代码行:

pairq.push(pair< &whereto->value, &whereto->adj->value >);

should probably look like:

应该看起来像:

pairq.push(make_pair(whereto->value, whereto->adj->value));

or if the valuemembers aren't of type int:

或者如果value成员不是类型int

pairq.push(pair<int,int>(whereto->value, whereto->adj->value));

Finally, queue::pop()doesn't return anything, so you probably want:

最后,queue::pop()不返回任何东西,所以你可能想要:

tree[i] = pairq.front();
pairq.pop();

回答by Donotalo

Take a look at make_pair(). queue::pop()doesn't return the first element. You need the following:

看看make_pair()queue::pop()不返回第一个元素。您需要以下内容:

tree[i] = pairq.front();
pairq.pop();

回答by Jason

You can't make a template from actual object instantiations like you've done ... they must be TYPES for the objects being instantiated by the template function (in this case the constructor).

你不能像你所做的那样从实际的对象实例化创建模板......它们必须是模板函数(在这种情况下是构造函数)实例化的对象的类型。

So for instance, you can make a pair object using a constructor like:

因此,例如,您可以使用如下构造函数创建一个 pair 对象:

pair<int, int>(whereto->value, whereto->adj->value)

pair<int, int>(whereto->value, whereto->adj->value)

or you can make a pair using the utility function make_pair()as shown by Michael.

或者您可以使用make_pair()Michael 所示的效用函数进行配对。

But if you're going to use the constructor, you have to somewhere declare the types that will replace the types T1and T2in the constructor's declaration, i.e.,

但是如果你要使用构造函数,你必须在某个地方声明将替换类型T1T2构造函数声明的类型,即,

template<typename T1, typename T2>
pair::pair(const T1& object_1, const T2& object_2);

That is done by declaring the object with the template arguments for the desired object types (i.e, for a pair of intobjects, you would use pair<int, int>), and then calling the actual object member function with objects of those types (in your case it would be a constructor for class pair).

这是通过使用所需对象类型的模板参数声明对象来完成的(即,对于一对int对象,您将使用pair<int, int>),然后使用这些类型的对象调用实际的对象成员函数(在您的情况下,它将是类的构造函数pair)。

回答by holtavolt

There's a few things going on here at first glance - can you provide the Node class/struct definition?

乍一看,这里发生了一些事情 - 您能提供 Node 类/结构定义吗?

  • You're returning a pointer to an automatic (stack) element at the end, which will be going out of scope. You'll want to return by value, most likely (alternatively, allocate and return a pointer, preferably shared, to the element)
  • queue.pop() returns void - see http://www.cplusplus.com/reference/stl/queue/pop/
  • 最后返回一个指向自动(堆栈)元素的指针,这将超出范围。您很可能希望按值返回(或者,分配并返回一个指向元素的指针,最好是共享的)
  • queue.pop() 返回 void - 请参阅http://www.cplusplus.com/reference/stl/queue/pop/