Question: Trying to figure out how to place objects in a map where the author is the key but can't seem to find how to do it.

heres my class object with a constructor

```
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);
}
};

```

and here is the vector and the map

```
    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;

```

Answer: 
You can try Insert API like this.

```
authorMap.insert ( "Dallas Orange",book5);

authorMap.insert ( "Jim John", Book ("a", "Jim John", 1001));

```

and you can use Iterators for accessing Book objects using author and vice versa.
So, you want to fill the map from a vector. You can do this by

```
for_each(books.begin(), books.end(), [&authorMap] (Book & b) { authorMap[b.author]=b; } );

```

Don't forget ´#include < algorithm>´ though.

If you are new to lambdas (or are not using C++11), this is the same thing as

```
for(vector<Book>::const_iterator it = books.begin(); it!=books.end(); ++it)
{
    authorMap[it->author] = *it;
}

```

I should also point out that your solution will be problematic in case one author has written more than one book.