작성자가 키이지만 어떻게 할 수없는지도에 개체를 배치하는 방법을 알아 내려고합니다.개체를 std :: map에 넣는 방법?
http://www.cplusplus.com/reference/map/map/insert/
경우 :
을 heres 생성자 여기
class Book
{
public:
string title = "";
string author = "";
int ISBN = 0;
Book(string t, string a, int num)
{
title = t;
author = a;
ISBN = num;
if (ISBN < 1000 || ISBN > 9999)
throw ("Invalid ISBN");
assert(ISBN > 1000 || ISBN < 9999);
}
};
및 내 클래스 객체는 벡터와지도
vector <Book> books;
try
{
Book book1("a", "Jim John", 1001);
books.push_back(book1);
Book book2("b", "Jim John", 1002);
books.push_back(book2);
Book book3("c", "Billy Bill", 1003);
books.push_back(book3);
Book book4("d", "Greg Lumburge", 1004);
books.push_back(book4);
Book book5("e", "Dallas Orange", 1005);
books.push_back(book5);
Book book6("f", "Old McDonald", 1006);
books.push_back(book6);
}
catch (char*e)
{
cout << "invalid ISBN" << endl;
return(EXIT_FAILURE);
}
map<string, Book> authorMap;
for (auto x : authorMap)
cout << x.first<< " : "<< x.second << endl;
그래서 코드에 어떤 문제가 있습니까? – riklund
아직 내 벡터로 채울 때까지 아무 것도 시도하지 않으면 일치하지 않는다고 말하기 시작합니다. (Book b : books) {++ authorMap [b];} – user3549747
키는 고유해야합니다 (cf "짐 존"). 그렇지 않으면 std :: multimap을 고려하십시오. 또한 [] 연산자는 키 유형을 인수로 사용합니다 (도서가 아닌 케이스의 문자열 유형). – SleuthEye