C++11 initializer_list constructor with header and cpp file for custom vector class

痴心易碎 提交于 2019-12-08 02:18:26

Assuming that you are passing the std::initializer_list argument by value (and you can, it is lightweight), you could do something like:

myvec(std::initializer_list<unsigned int> l) : myvec(l.size()) {
  std::copy(std::begin(l), std::end(l), arr);
}

That is, you initialize your internal array with the size() of the list, and you iterate over it with std::begin() and std::end() to copy over its elements.

std::initializer_list has 3 member functions:

And that's all you need.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!