Skip to content
Minsing Log
Go back

English Edition

Algorithm for deleting C++ vector elements without the algorithm library

Programming & CS

GOAT!

template<typename T>
void removeElement(std::vector<T>& vec, const T& valueToRemove) {
    // 벡터를 순회하며 일치하는 요소를 찾아 삭제
    for (auto it = vec.begin(); it != vec.end(); ++it) {
        if (*it == valueToRemove) {
            vec.erase(it); // 일치하는 요소를 삭제
            break; // 하나의 요소만 제거하므로 삭제 후 반복을 멈춤
        }
    }
}

Follow the journey

Get new posts by email.

Short, honest notes on building, startups, learning, failures, and retrospectives, for readers who follow the journey rather than pass through from search.

The newsletter is coming soon. Until then, follow via RSS or email me to get notified.


Share this post:

Comments

Sign in with GitHub to leave a comment.


Previous Post
An Algorithm That Returns the Largest Element Among All Elements in a C++ vector
Next Post
Why the Heck Won’t Debugging Work!