Welcome to collectivesolver - Programming & Software Q&A with code examples. A website with trusted programming answers. All programs are tested and work.

Contact: aviboots(AT)netvision.net.il

Buy a domain name - Register cheap domain names from $0.99 - Namecheap

Scalable Hosting That Grows With You

Secure & Reliable Web Hosting, Free Domain, Free SSL, 1-Click WordPress Install, Expert 24/7 Support

Semrush - keyword research tool

Boost your online presence with premium web hosting and servers

Disclosure: My content contains affiliate links.

39,907 questions

51,839 answers

573 users

How to remove item from a list in C++

3 Answers

0 votes
#include <iostream>
#include <list>
#include <algorithm>
#include <iterator>
 
int main()
{
    std::list<int> lst;
 
    for (int i = 1; i <= 6; ++i) {
        lst.push_back(i);
    }
 
    for (auto elem : lst) {
        std::cout << elem << ' ';
    }
    std::cout << std::endl;
 
    lst.remove(2);
 
    for (auto elem : lst) {
        std::cout << elem << ' ';
    }
    std::cout << std::endl;
}
 
 
 
/*
run:
 
1 2 3 4 5 6
1 3 4 5 6
 
*/

 



answered Dec 31, 2017 by avibootz
edited Apr 21, 2024 by avibootz
0 votes
#include <iostream>
#include <list>
#include <algorithm>
 
int main()
{
    int arr[] = { 1, 2, 3, 4, 5 };
    std::list<int> lst(arr, arr + 5);
 
    lst.remove(4);
 
    for (std::list<int>::iterator itr = lst.begin(); itr != lst.end(); itr++) {
        std::cout << *itr << std::endl;
    }
}


 
/*
run:
 
1
2
3
5
 
*/

 



answered Dec 31, 2017 by avibootz
edited Apr 21, 2024 by avibootz
0 votes
#include <iostream>
#include <list>
 
using std::cout;
using std::endl;
using std::list;
 
int main()
{
    list<char> lst;
 
    for (int i = 0; i < 5; i++) {
        lst.push_back('a' + i);
    }
 
    for (auto e : lst) {
        cout << e << "  ";
    }
    cout << endl;
 
    lst.remove('a');
    lst.remove('d');
 
    for (auto e : lst) {
        cout << e << "  ";
    }
    cout << endl;
}
 
 
 
/*
run:
 
a  b  c  d  e
b  c  e
 
*/

 



answered Apr 21, 2024 by avibootz

Related questions

2 answers 231 views
1 answer 115 views
1 answer 116 views
1 answer 100 views
100 views asked Feb 28, 2023 by avibootz
1 answer 177 views
7 answers 800 views
...