0

I am creating a vector b1 with fixed size (like c-style array), for copying a1 followed a2. I thought of using assign as below, But size of b1 is reduced to 7(to size of a1) after b1.assign(). How can I keep the b1 size 20 (fixed size) even after assign(). Any suggestions, I am using memcpy() now.

std::vector<int> a1{1,2,3,4,5,6,7};
std::vector<int> a2{ 10,11,12,13,14,15,16,17 };
std::vector<int> b1(20);
b1.assign(a1.begin(),a1.end());
Susanth
  • 29
  • 2
  • 2
    Why not construct the vector from the iterators and then `resize` the vector to the final size you want? – NathanOliver Aug 23 '17 at 14:25
  • What happens if the total length of `a1` and `a2` exceeds 20? What do you do with the excess elements? – PaulMcKenzie Aug 23 '17 at 14:28
  • Note: if all want to do is concatenate two vectors, then see https://stackoverflow.com/questions/3177241/what-is-the-best-way-to-concatenate-two-vectors – juanchopanza Aug 23 '17 at 14:31

1 Answers1

3

If you want to copy things, you can use std:copy:

std::copy(a1.begin(), a1.end(), b1.begin());
juanchopanza
  • 223,364
  • 34
  • 402
  • 480