list

From the list that i created.
I'm trying to replace apple with banana and im trying to read the fifth element but i do not know how to approach.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55

#include <iostream>
#include <list>
#include <string>
using namespace std;
void print_list(list<string> somelist);
int main()
{
	list <string> grocery;
	list <string>::iterator it;
	grocery.empty()'
	{if(grocery.empty())
		cout<<"List is empty."<<endl;
	else
		cout<<"List is not empty."<<endl;
	}//checking if list is empty or not
	cout<<endl;
	//adding items
	grocery.push_back("Milk");
	grocery.push_back("Eggs");
	grocery.push_back("Butter");
	grocery.push_back("Apple");
	grocery.push_back("Bread");
	grocery.push_back("Chicken");
	print_list(grocery);//print out of elements
	cout<<endl;

	list<string>::size_type i;
	i=grocery.size();
	cout<<"The size of the grocery list is: "<<i<<endl<<endl;; //displays size of list
	
	grocery.remove("Apple");
	print_list(grocery);
	cout<<endl;

	list<string>::size_type j;
	j=grocery.size();
	cout<<"The size of the Grocery list is: "<<j<<endl<<endl; //displays size of list

	grocery.insert(4,"Banana") ;
	print_list(grocery); //replacing banana instead of Apple
	cout<<endl;

        cout<<"The fifth element is: "<<grocery[5]<<endl;

	return 0;
}
void print_list(list<string> somelist)
{	
	list<string>::iterator itt;
	for (itt=somelist.begin(); itt!=somelist.end(); ++itt)
		cout<<""<< *itt<<endl;
	cout<<endl;
}
 
Last edited on
Topic archived. No new replies allowed.