链接列表从函数返回变量

时间:2013-11-16 20:15:08

标签: c++ function linked-list return-value

嗨,我正在为大学做一个项目,我被困在一个部分。我在c ++中使用链表。我必须建立一个名为Book的类,它有变量'title','author','ISBN'和'availability'。我在我的主要设置它使用函数的原型和在别处调用的函数。

//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++;
}
}
}

我知道我的文件之间的链接是宏的我只是不能获得'temp'列表来返回值。任何帮助将不胜感激。 感谢

1 个答案:

答案 0 :(得分:1)

在C ++中,大多数容器(如std::list)可以像任何其他基本类型一样进行复制构造或分配。在您的情况下,直接return就足够了。