서의 공간

std::vector::erase(), std::vector::clear(), std::remove() 본문

C++/정보

std::vector::erase(), std::vector::clear(), std::remove()

홍서의 2020. 12. 16. 14:22

1. vector::erase - C++ Reference (cplusplus.com)

Removes from the vector either a single element (position) or a range of elements ([first, last)).

This effectively reduces the container size by the number of elements removed, which are destroyed.

Because vectors use an array as their underlying storage, erasing elements in positions other than the vector end causes the container to relocate all the elements after the segment erased to their new positions. This is generally an inefficient operation compared to the one performed for the same operation by other kinds of sequence containers(such as list or forward_list).

 

2. vector::clear - C++ Reference (cplusplus.com)

Removes all elements from the vector (which are destroyed), leaving the container with a size of 0.

A reallocation is not guaranteed to happen, and the vector capacity is not guaranteed to change due to calling this function. A typical alternative that forces a reallocation is to use swap:

vector<T>().swap(x); // clear x reallocating

3. remove - C++ Reference (cplusplus.com)

Transforms the range [first, last) into a range with all the elements that compare equal to val removed, and returns an iterator to the new end of that range.

The function cannot alter the properties of the object containing the range of elements (i.e., it cannot alter the size of an array or a container): The removal is done by replacing the elements that compare equal to val by the next element that does not, and signaling the new size of the shortened range by returning an iterator to the element that should be considered its new past-the-end element.
The relative order of the elements not removed is preserved, while the elements between the returned iterator and last are left in a valid but unspecified state.
The function uses operator== to compare the individual elements to val.

'C++ > 정보' 카테고리의 다른 글

optional 예제  (0) 2021.02.03
클래스에 대해  (0) 2021.02.03
c++ 함수안에 함수 정의  (0) 2021.01.19
C++용 XML 설명서  (0) 2020.12.13
vector::resize()와 vector::reserve() 차이  (0) 2020.12.12
Comments