the element that the iterator points to is changed after the iterator is deleted

example about std::vector::iterator.

#include <iostream>
#include <string>
#include <algorithm>
#include <vector>
#include <utility>

int main() {
    std::vector<std::pair<int,std::string>> container;
    container.push_back( std::make_pair( 1, "one" ) );
    container.push_back( std::make_pair( 2, "two" ) );
    container.push_back( std::make_pair( 3, "three" ) );
    container.push_back( std::make_pair( 4, "four" ) );

    auto itr = std::find_if(container.begin(), container.end(), //std::vector<std::pair<int,std::string>>::iterator
                       [](const std::pair<int, std::string>& p) { return p.first == 2; });

    container.erase( itr );

    std::cout << "result: " << itr->first << ", " << itr->second << std::endl;  // result: 3, three

    return 0;
}

std::remove_if moves all elements with key 2 to the end of the sequence based on the condition [p.first == 2].

int main() {
    std::vector<std::pair<int,std::string>> container;
    container.push_back( std::make_pair( 1, "one" ) );
    container.push_back( std::make_pair( 2, "two" ) );
    container.push_back( std::make_pair( 3, "three" ) );
    container.push_back( std::make_pair( 4, "four" ) );
    container.push_back( std::make_pair( 2, "two two" ) );

    container.erase(
        std::remove_if(container.begin(), container.end(),
                       [](const std::pair<int, std::string>& p) { return p.first == 2; }),
        container.end());

    for( auto itr = container.begin(); itr != container.end(); ++itr )
    {
        std::cout << itr->first << ", " << itr->second << std::endl;
    }
    
    /*
    1, one
    3, three
    4, four
    */
    
    return 0;
}

std::vector::erase returns an iterator pointing to the element immediately following the last element erased. If the erase call removes the last element or clears the entire vector, it returns end().

The standard library’s std::vector has a regular iterator that can be converted to a reverse_iterator, but it requires the use of std::make_reverse_iterator or a constructor for explicit conversion.

int main() {
    std::vector<std::pair<int,std::string>> container;
    container.push_back( std::make_pair( 1, "one" ) );
    container.push_back( std::make_pair( 2, "two" ) );
    container.push_back( std::make_pair( 3, "three" ) );
    container.push_back( std::make_pair( 4, "four" ) );
    container.push_back( std::make_pair( 2, "two two" ) );

    auto itr = container.erase(
        std::remove_if(container.begin(), container.end(),
                       [](const std::pair<int, std::string>& p) { return p.first == 2; }),
        container.end());
    auto rev_itr =  std::vector<std::pair<int,std::string>>::reverse_iterator( itr );

    for( ; rev_itr != container.rend(); ++rev_itr )
    {
        std::cout << rev_itr->first << ", " << rev_itr->second << std::endl;
    }

    /*
    4, four
    3, three
    1, one
    */

    return 0;
}
Categories: CPlusPlus

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Inline Feedbacks
View all comments

Tex To PDF
: convert the Latex file which suffix is tex to a PDF file

X
0
Would love your thoughts, please comment.x
()
x