Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/typescript/9.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在结构之间复制特定值 使用C++而不使用STL容器的函数,我试图以特定的方式将一个结构的值复制到另一个结构。_C++_Pointers_Data Structures_Struct_Singly Linked List - Fatal编程技术网

在结构之间复制特定值 使用C++而不使用STL容器的函数,我试图以特定的方式将一个结构的值复制到另一个结构。

在结构之间复制特定值 使用C++而不使用STL容器的函数,我试图以特定的方式将一个结构的值复制到另一个结构。,c++,pointers,data-structures,struct,singly-linked-list,C++,Pointers,Data Structures,Struct,Singly Linked List,我有这样的结构(单链表),我就是这样向其中添加新元素的: struct Node { Node *next; std::string name; std::string surname; }; void AddElement(Node *&head, const std::string &name, const std::string &surname) { head = new Node {head, name, surname}; }

我有这样的结构(单链表),我就是这样向其中添加新元素的:

struct Node {
    Node *next;
    std::string name;
    std::string surname;
};

void AddElement(Node *&head, const std::string &name, const std::string &surname)
{
    head = new Node {head, name, surname};
}
示例用法:

Node *pointer = nullptr; 
AddElement(pointer, "Alex", "Smith"); 
AddElement(pointer, "Robert", "Johnson"); 
AddElement(pointer, "George", "Smith"); 
AddElement(pointer, "Oscar", "Williams");
我还有另一个结构:

struct Result {
    Result *next;
    std::string surname;
};
我希望每个给定的“姓氏”值只将第一个节点列表中的所有“姓氏”值复制到新的结果列表中一次

也就是说,如果我的节点链接列表中有这样的姓氏:
“Smith”、“Johnson”、“Smith”、“Johnson”、“Williams”
,我希望在结果列表中有
“Smith”、“Johnson”、“Williams”


<我对C++、指针和结构是新的。如何实现这一点?

以下是一种可能的算法,可以在不更改基本结构定义的情况下减少搜索:

保持结果结构列表按字母顺序排序。无论何时检查姓氏,都要在结果结构中进行搜索,直到:(1)找到一个完全匹配的姓氏,因此不要添加它(2)按字母顺序查找超出搜索姓氏的姓氏。在本例中,在找到的姓氏之前直接添加新姓氏


这可能不是最好或唯一的解决方案,但我提供它作为一个方向。

我不使用标准库。那么什么是
std::string
呢?对不起,我指的是STL容器的函数。这是否意味着no
std::set
?好的“暴力”方法是遍历原始列表,并在每个项目上询问您自己之前是否添加了该姓氏。检测您是否在之前添加了姓氏意味着要遍历结果列表。所以你会在另一个循环中找到一个循环。这并不漂亮,但却是最简单的方法。只是好奇:为什么你不能使用std容器?锻炼目的?教师要求?嵌入式系统中没有足够的空间。。。?