将元素添加到C ++ std :: map中只插入一个键(Adding element into C++ std::map inserts only one key)

我创建了一个简单的库程序,我存储了Book对象的地图及其数量。 我想在地图上添加一些书籍,以便能够出租书籍等。

问题是在我的代码中,名为createLib方法只向地图插入一个图书对象。 这是为什么? 怎么解决这个? 我在这做错了什么? 谢谢。

#include <iostream> #include <string> #include <map> using namespace std; class Author { public: string name; Author (){} Author(string n){name = n;} }; class Book { public: Author author; string title; static int id; Book (Author a, string t){author = a, title = t; id ++;} }; int Book::id = 0; struct comapare { bool operator() (const Book& k1, const Book& k2) { return k2.id < k1.id; } }; class Library { public: map<Book, int, comapare> books; void createLib() { Author a1("Bruce Eckel"); Book b1(a1, "Thinking in Java"); Book b2(a1, "Thinking in C++"); books.insert(std::make_pair(b1, 10)); books.insert(std::make_pair(b2, 2)); std::cout << books.size() << "\n"; } }; int main() { Library l; l.createLib(); return 0; }

编辑:

这是工作版本:

#include <iostream> #include <string> #include <map> using namespace std; class Author { public: string name; Author () {} Author(string n) { name = n; } }; class Book { public: Author author; string title; static int id; int rid; Book (Author a, string t) { author = a, title = t; id ++, rid = id; } }; int Book::id = 0; struct comapare { bool operator() (const Book& k1, const Book& k2) { return k2.rid < k1.rid; } }; class Library { public: map<Book, int, comapare> books; void createLib() { Author a1("Bruce Eckel"); Book b1(a1, "Thinking in Java"); Book b2(a1, "Thinking in C++"); books.insert(std::make_pair(b1, 10)); books.insert(std::make_pair(b2, 2)); std::cout << books.size() << "\n"; for ( std::map<Book, int, comapare>::const_iterator iter = books.begin(); iter != books.end(); ++iter ) cout << iter->first.title << "\t\t" << iter->second << '\n'; } }; int main() { Library l; l.createLib(); return 0; }

I created a simple library program, where I store a map of Book objects and its quantity. I would like to add some books to the map to be further able to rent a book, etc.

The problem is that in my code, method called createLib inserts only one book object to the map. Why is that? How to solve this? What am I doing wrong here? Thanks.

#include <iostream> #include <string> #include <map> using namespace std; class Author { public: string name; Author (){} Author(string n){name = n;} }; class Book { public: Author author; string title; static int id; Book (Author a, string t){author = a, title = t; id ++;} }; int Book::id = 0; struct comapare { bool operator() (const Book& k1, const Book& k2) { return k2.id < k1.id; } }; class Library { public: map<Book, int, comapare> books; void createLib() { Author a1("Bruce Eckel"); Book b1(a1, "Thinking in Java"); Book b2(a1, "Thinking in C++"); books.insert(std::make_pair(b1, 10)); books.insert(std::make_pair(b2, 2)); std::cout << books.size() << "\n"; } }; int main() { Library l; l.createLib(); return 0; }

EDIT:

Here's the working version:

#include <iostream> #include <string> #include <map> using namespace std; class Author { public: string name; Author () {} Author(string n) { name = n; } }; class Book { public: Author author; string title; static int id; int rid; Book (Author a, string t) { author = a, title = t; id ++, rid = id; } }; int Book::id = 0; struct comapare { bool operator() (const Book& k1, const Book& k2) { return k2.rid < k1.rid; } }; class Library { public: map<Book, int, comapare> books; void createLib() { Author a1("Bruce Eckel"); Book b1(a1, "Thinking in Java"); Book b2(a1, "Thinking in C++"); books.insert(std::make_pair(b1, 10)); books.insert(std::make_pair(b2, 2)); std::cout << books.size() << "\n"; for ( std::map<Book, int, comapare>::const_iterator iter = books.begin(); iter != books.end(); ++iter ) cout << iter->first.title << "\t\t" << iter->second << '\n'; } }; int main() { Library l; l.createLib(); return 0; }

最满意答案

问题是,对于Book类的所有实例, id都是相同的。

您需要两个 id成员:一个增加的静态成员,以及一个非静态成员,它是实际的book id并从静态id分配。

The problem is that id is the same for all instances of the Book class.

You need two id members: One static member that you increase, and one non-static member that is the actual book id and is assigned to from the static id.

将元素添加到C ++ std :: map中只插入一个键(Adding element into C++ std::map inserts only one key)

我创建了一个简单的库程序,我存储了Book对象的地图及其数量。 我想在地图上添加一些书籍,以便能够出租书籍等。

问题是在我的代码中,名为createLib方法只向地图插入一个图书对象。 这是为什么? 怎么解决这个? 我在这做错了什么? 谢谢。

#include <iostream> #include <string> #include <map> using namespace std; class Author { public: string name; Author (){} Author(string n){name = n;} }; class Book { public: Author author; string title; static int id; Book (Author a, string t){author = a, title = t; id ++;} }; int Book::id = 0; struct comapare { bool operator() (const Book& k1, const Book& k2) { return k2.id < k1.id; } }; class Library { public: map<Book, int, comapare> books; void createLib() { Author a1("Bruce Eckel"); Book b1(a1, "Thinking in Java"); Book b2(a1, "Thinking in C++"); books.insert(std::make_pair(b1, 10)); books.insert(std::make_pair(b2, 2)); std::cout << books.size() << "\n"; } }; int main() { Library l; l.createLib(); return 0; }

编辑:

这是工作版本:

#include <iostream> #include <string> #include <map> using namespace std; class Author { public: string name; Author () {} Author(string n) { name = n; } }; class Book { public: Author author; string title; static int id; int rid; Book (Author a, string t) { author = a, title = t; id ++, rid = id; } }; int Book::id = 0; struct comapare { bool operator() (const Book& k1, const Book& k2) { return k2.rid < k1.rid; } }; class Library { public: map<Book, int, comapare> books; void createLib() { Author a1("Bruce Eckel"); Book b1(a1, "Thinking in Java"); Book b2(a1, "Thinking in C++"); books.insert(std::make_pair(b1, 10)); books.insert(std::make_pair(b2, 2)); std::cout << books.size() << "\n"; for ( std::map<Book, int, comapare>::const_iterator iter = books.begin(); iter != books.end(); ++iter ) cout << iter->first.title << "\t\t" << iter->second << '\n'; } }; int main() { Library l; l.createLib(); return 0; }

I created a simple library program, where I store a map of Book objects and its quantity. I would like to add some books to the map to be further able to rent a book, etc.

The problem is that in my code, method called createLib inserts only one book object to the map. Why is that? How to solve this? What am I doing wrong here? Thanks.

#include <iostream> #include <string> #include <map> using namespace std; class Author { public: string name; Author (){} Author(string n){name = n;} }; class Book { public: Author author; string title; static int id; Book (Author a, string t){author = a, title = t; id ++;} }; int Book::id = 0; struct comapare { bool operator() (const Book& k1, const Book& k2) { return k2.id < k1.id; } }; class Library { public: map<Book, int, comapare> books; void createLib() { Author a1("Bruce Eckel"); Book b1(a1, "Thinking in Java"); Book b2(a1, "Thinking in C++"); books.insert(std::make_pair(b1, 10)); books.insert(std::make_pair(b2, 2)); std::cout << books.size() << "\n"; } }; int main() { Library l; l.createLib(); return 0; }

EDIT:

Here's the working version:

#include <iostream> #include <string> #include <map> using namespace std; class Author { public: string name; Author () {} Author(string n) { name = n; } }; class Book { public: Author author; string title; static int id; int rid; Book (Author a, string t) { author = a, title = t; id ++, rid = id; } }; int Book::id = 0; struct comapare { bool operator() (const Book& k1, const Book& k2) { return k2.rid < k1.rid; } }; class Library { public: map<Book, int, comapare> books; void createLib() { Author a1("Bruce Eckel"); Book b1(a1, "Thinking in Java"); Book b2(a1, "Thinking in C++"); books.insert(std::make_pair(b1, 10)); books.insert(std::make_pair(b2, 2)); std::cout << books.size() << "\n"; for ( std::map<Book, int, comapare>::const_iterator iter = books.begin(); iter != books.end(); ++iter ) cout << iter->first.title << "\t\t" << iter->second << '\n'; } }; int main() { Library l; l.createLib(); return 0; }

最满意答案

问题是,对于Book类的所有实例, id都是相同的。

您需要两个 id成员:一个增加的静态成员,以及一个非静态成员,它是实际的book id并从静态id分配。

The problem is that id is the same for all instances of the Book class.

You need two id members: One static member that you increase, and one non-static member that is the actual book id and is assigned to from the static id.