Skip to content

Latest commit

 

History

History
20 lines (17 loc) · 663 Bytes

10.7.md

File metadata and controls

20 lines (17 loc) · 663 Bytes

(a)

vector<int> vec; list<int> lst; int i;
while (cin >> i)
  lst.push_back(i);
vec.resize(lst.size());
// We must resize the vector before copying the elements so that it can hold
// at least the same amount of elements.
copy(lst.cbegin(), lst.cend(), vec.begin());

(b)

vector<int> vec;
//vec.reserve(10);
// Reserve only allocate memory, while not add elements to the vector, but
// the function `fill_n` need `n` elements in container.
vec.resize(10);  // Use `resize` to add elements
//fill_n(std::back_inserter(vec), 10, 0);
// Or use `back_inserter` to add elements
fill_n(vec.begin(), 10, 0);