With a compiler that supports the C++11 standard you can use emplace_back
, like this:
vector <Book> books;
books.emplace_back("a", "Jim John", 1000);
books.emplace_back("b", "Jim John", 1001);
books.emplace_back("c", "Billy Bill", 1002);
books.emplace_back("d", "Greg Lumburge", 1003);
books.emplace_back("e", "Dallas Orange", 1004);
books.emplace_back("f", "Old McDonald", 1005);
But easier still, with C++11, is to just list all that data in a curly braces initializer:
vector <Book> books =
{
{"a", "Jim John", 1000},
{"b", "Jim John", 1001},
{"c", "Billy Bill", 1002},
{"d", "Greg Lumburge", 1003},
{"e", "Dallas Orange", 1004},
{"f", "Old McDonald", 1005}
};
And then it's easy to add a const
, if appropriate.
In C++03 you can do this instead:
static Book const data[] =
{
Book("a", "Jim John", 1000),
Book("b", "Jim John", 1001),
Book("c", "Billy Bill", 1002),
Book("d", "Greg Lumburge", 1003),
Book("e", "Dallas Orange", 1004),
Book("f", "Old McDonald", 1005)
};
int const data_size = sizeof( data )/sizeof( *data );
vector <Book> books( data, data + data_size );