将 forward_list::p ush_front() 与结构对象一起使用

Using forward_list::push_front() with a struct object

本文关键字:结构 对象 一起 front list forward ush      更新时间:2023-10-16

我想开始使用STL版本的单链表,但我遇到了一个问题。如果我希望我的列表由结构类型对象组成,而不仅仅是简单的本机类型,如 int、char 等,我对如何使用 push_front() 函数有一个两难的选择,因为它只需要一个参数。那么我如何使用这样的代码插入新对象:

#include <iostream>
#include <forward_list>
using namespace std;
struct Node
{
    double x;
    double y;
};
int main()
{
    forward_list<Node> myList;
    myList.push_front(???);
}

???我感谢给予的任何帮助!!

myList.push_front({3.14, 2.71});myList.push_front(Node{3.14, 2.71});

Node n;
n.x = 3.14;
n.y = 2.71;
myList.push_front(n);

应该都工作。例。