课程: Complete Guide to C++ Programming Foundations

免费学习该课程!

今天就开通帐号,24,100 门业界名师课程任您挑!

Solution: Vector manipulation

Solution: Vector manipulation

(bright electronic music) - [Instructor] For this challenge, your task was to write the code for the SelectKeyPoints function to return a downsized version of the incoming vector. You were given a vector of integers and an index to pick the middle element in the downsized output vector. Let me show you my solution. First, in line 14, I used the push_back function on the result vector to insert the value at the beginning of the distances vector. Remember, since begin returns and iterator, I must de-reference it with the indirection asterisk operator. Then, in line 15, I'm doing the same, but I'm indexing the vector with the square brackets operator. The index is checkpointIndex. And lastly, in line 16, I could have done the same as with the begin function, but using the end function and going back by one position. However, I decided to use the back function, which returns the last element in the vector. This is not an iterator, but the integer at the last position in the vector. That's…

内容