Skip to content

Instantly share code, notes, and snippets.

@barthap
Last active January 22, 2019 15:08
Show Gist options
  • Save barthap/5ee648d3e76f75d3aedc82e267bafe51 to your computer and use it in GitHub Desktop.
Save barthap/5ee648d3e76f75d3aedc82e267bafe51 to your computer and use it in GitHub Desktop.
Testing std::list::remove_if() for memory organisation
//
// Created by Barthap on 2019-01-22.
//
#include <utility>
#include <iostream>
#include <vector>
#include <list>
int main()
{
std::list<int> lista = {1,2,3,4,5};
for(auto& e : lista)
std::cout << e << " ";
std::cout << std::endl;
auto it = std::find(lista.begin(), lista.end(), 4);
int &ref = *it;
std::cout << "ref = " << ref << std::endl;
//std::remove_if(lista.begin(), lista.end(), [](auto& el) { return el == 2; });
lista.remove_if([](auto& el) { return el == 2; });
std::cout << "after remove, ref = " << ref << std::endl;
auto it2 = std::find(lista.begin(), lista.end(), 4);
std::cout << "Original address: " << std::addressof(ref)
<< "\nAfter remove: " << std::addressof(*it2) << std::endl;
ref = 16;
for(auto& e : lista)
std::cout << e << " ";
std::cout << std::endl;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment