从函数返回变量的链表
Linked list returning variables from a function
嗨,我正在为大学做一个项目,但我被困在一个部分。我在 c++ 中使用链表。我必须设置一个名为 Book 的类,其中包含变量"标题"、"作者"、"ISBN"和"可用性"。我在我的主要版本中使用函数的原型和在其他地方调用的函数来设置它。
//the prototype
list<Book> bookSetUp();
int main()
{
//the variable in main that will have the list
list<Book> bookList;
//the list being populated in function elsewhere so as to not mess up the main
bookList = bookSetUp();
// more stuff in main
}
//sets up the book vector list by populating it
//title, author, ISBN, availability
list<Book> bookSetUp()
{
//creates a temp vector to pass it back to the actual vector to be used in the main
list<Book> temp;
//The items that populate the list
Book a("A Tale of Two Cities", "Charles Dickens", 1203456, true);
Book b("Lord of the rings", "J.R.R Tolkein", 123456, true);
Book c("Le Petit Prince", "Antoine de Saint-Exupéry", 123457, true);
Book d("And Then There Were None", "Agatha Christie", 123458, true);
Book e("Dream of the Red Chamber","Cao Xueqin",123459, true);
Book f("The Hobbit","J.R.R Tolkein",123467, true);
//pushes the items into the vector
temp.push_back(a);
temp.push_back(b);
temp.push_back(c);
temp.push_back(d);
temp.push_back(e);
temp.push_back(f);
//returns the list
list<Book>::iterator pos;
pos = temp.begin();
while(pos != temp.end())
{
return pos;
if(pos != temp.end())
{
pos++;
}
}
}
我知道文件之间的链接很大,我只是无法获得"临时"列表来返回值。任何帮助将不胜感激。谢谢
C++大多数容器(如std::list
)都可以像任何其他基元类型一样进行复制构造或分配。在您的情况下,直接return
就足够了。
相关文章:
- 反向给定链表中的K节点
- 如果没有malloc,链表实现将失败
- 文本文件中的单词链表
- 努力将整数转换为链表。不知道我在这里做错了什么
- 链表,反向函数,数据结构
- 使用std::list创建循环链表
- 我们可以在不使用head指针的情况下通过使用head的简单变量而不是head的指针来实现链表吗
- 在 C++ 中使用 temp 变量将节点添加到链表的末尾
- 嵌套的类成员变量不可访问.链表设置.V、下一个和新的问题
- 在 for 循环(链表)中删除两次后,变量不可用
- 多进程 c++(11),链表指针作为全局变量
- 为什么在链表中使用指向结构的指针而不是常规结构类型变量呢
- 从函数返回变量的链表
- 从类外部访问链表私有变量
- C++实现具有多个变量的链表
- 在链表形成中,在开头添加变量的代码是什么
- 复制链表,每个节点都有随机链接,每个节点都有一个变量,随机指向列表中的另一个节点
- 链表节点结构的第一个成员变量总是下一个节点
- 如何使用一次通过引用变量来反转链表
- C++全局变量与局部变量作为链表中的表头