Welcome to Westonci.ca, the place where your questions are answered by a community of knowledgeable contributors. Join our Q&A platform and get accurate answers to all your questions from professionals across multiple disciplines. Discover detailed answers to your questions from a wide network of experts on our comprehensive Q&A platform.

Write a program that uses an STL List of integers. a. The program will insert two integers, 5 and 6, at the end of the list. Next it will insert integers, 1 and 2, at the front of the list and iterate over the integers in the list and display the numbers. b. Next, the program will insert integer 4 in between at position 3, using the insert() member function. After inserting it will iterate over list of numbers and display them. c. Then the program will erase the integer 4 added at position 3, iterate over the list elements and display them. d. Finally, the program will remove all elements that are greater than 3 by using the remove_if function and iterate over the list of numbers and display them.
Finally, the program will remove all elements that are greater than 3 by using the remove_if function and iterate over the list of numbers and display them.

Sagot :

Answer:

answer:

#include <iostream>

#include<list>

using namespace std;

bool Greater(int x) { return x>3; } int main() { list<int>l; /*Declare the list of integers*/ l.push_back(5); l.push_back(6); /*Insert 5 and 6 at the end of list*/ l.push_front(1); l.push_front(2); /*Insert 1 and 2 in front of the list*/ list<int>::iterator it = l.begin(); advance(it, 2); l.insert(it, 4); /*Insert 4 at position 3*/ for(list<int>::iterator i = l.begin();i != l.end();i++) cout<< *i << " "; /*Display the list*/ cout<<endl; l.erase(it); /*Delete the element 4 inserted at position 3*/ for(list<int>::iterator i = l.begin();i != l.end();i++) cout<< *i << " "; /*Display the list*/ cout<<endl;

l.remove_if(Greater); for(list<int>::iterator i = l.begin();i != l.end();i++) cout<< *i << " ";

/*Display the list*/

cout<<endl; return 0;

}