Skip to content

Instantly share code, notes, and snippets.

@ArchangelSDY
Last active June 29, 2016 06:18
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ArchangelSDY/5889ac4dca76fc2bc30cf7615f5f6cd3 to your computer and use it in GitHub Desktop.
Save ArchangelSDY/5889ac4dca76fc2bc30cf7615f5f6cd3 to your computer and use it in GitHub Desktop.
initializer_list
#include <initializer_list>
#include <string>
#include <list>
#include <iostream>
using namespace std;
class Book
{
public:
string bookname;
double price;
string author;
Book(const string &b, double p, const string &a) :
bookname(b),
price(p),
author(a) {}
};
class S
{
public:
S(initializer_list<Book> _books)
{
books.insert(books.end(), _books.begin(), _books.end());
}
void append(initializer_list<Book> _books)
{
books.insert(books.end(), _books.begin(), _books.end());
}
list<Book> books;
};
int main()
{
S s = {{"aaa", 1, "bbb"}};
cout << s.books.size() << endl;
cout << s.books.back().author << endl;
s.append({{"ccc", 2, "ddd"}, {"eee", 3, "fff"}});
cout << s.books.size() << endl;
cout << s.books.back().author << endl;
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment