What's the equivalent of the C++11 vector::assign(InputIterator first, InputIterator last)
function? According to cplusplus.com the C++ version does the following:
the new contents are elements constructed from each of the elements in the range between first and last, in the same order.
My basic attempt at this is the following code. Is there anything better?
let i = 0;
for t in transformed.into_iter() {
if signal.len() <= i {
signal.push(t);
} else {
signal[i] = t;
}
i += 1;
}
答案 0 :(得分:2)
First note that your loop does not emulate the behavior of std::vector::assign
unless the length of the iterator >= the length of the vector. It would be simpler to just clear the vector first, and then push elements (the vector will retain its original capacity, so you don't have to worry about unnecessary allocations). But extending the vector can be done with the extend function.
signal.clear();
signal.extend(transformed.into_iter());
Or, if you are creating a new vector, you can use collect.
let signal: Vec<_> = transformed.into_iter().collect();