Impossible to erase or insert

Hi ; although many trials, impossible to insert or erase somebody. Look at my 3 commented lines (76,77,78), which of course don't work.This program was copied from a Stroustrup example code, named « list ».My compiler is g++.Thanks for your help. My file works well.Here is the code :
1 /*reference.cpp*/
2 #include<list>
3 #include <iostream>
4 #include <string>
5 using namespace std;
6 void pause()
7 {
8 cout <<" ENTER for continuing ";
9 cin.get();
10 }
11 void printString ( string& r)//works with all adresses beginning with r
12 {
13 cout << r;
14 }
15 void print_string (const string& st)//For a cinstant string
16 {
17 cout << st;//legal, doesn't modify:!
18 //st="abc";//not legal !
19 }
20
21 struct Entry {
22 string name;
23 int number;
24 Entry(const string& n, int i) :name(n), number(i) { }
25 };
26
27 list<Entry> phone_book;
28
29 void print_entries()
30 {
31 typedef list<Entry>::const_iterator LI;
32
33 for (LI i = phone_book.begin(); i != phone_book.end(); ++i) {
34 const Entry& e = *i; // reference used as shorthand
35 cout << '{' << e.name << ' ' << e.number << "}\n";
36 }
37 cout << endl;
38 }
39
40 void print_entry(const string& s)
41 /*
42 Is this the right treatment of a string not found?
43 */
44 {
45 typedef list<Entry>::const_iterator LI;
46
47 for (LI i = phone_book.begin(); i != phone_book.end(); ++i) {
48 const Entry& e = *i; // reference used as shorthand
49 if (s == e.name) {
50 cout << e.name << ' ' << e.number << '\n';
51 return;
52 }
53 }
54 }
55
56 void f(const Entry& e, list<Entry>::iterator i, list<Entry>::iterator p)
57 {
58 phone_book.push_front(e); // add at beginning
59 phone_book.push_back(e); // add at end
60 phone_book.insert(i,e); // add before the element referred to by `i'
61 phone_book.erase(p); // remove the element referred to by `p'
62 }
63
64 int main()
65 {
66
67 phone_book.push_back(Entry("dumas",1234567890));
68 phone_book.push_back(Entry("aubertin",493482181));
69 phone_book.push_back(Entry("charlot",76890123));
70 phone_book.push_back(Entry("four",4));
71 phone_book.push_back(Entry("five",5));
72 Entry six("six",6);
73 print_entries();
74 pause();
75 f(six,phone_book.begin(),phone_book.begin());
76 //phone_book.insert(5, (Entry("inserted",40909567)));
77 //phone_book.insert(5, Entry("inserted",40909567));
78 //phone_book.erase(1);
79 print_entries();
80 pause();
81 print_entry("aubertin");
82 print_entry("seven");
83 pause();
84 cout << "SECOND PROGRAM\n";
85 cout << "1 Aubertin, 2 Dumas, 3 Charlot\n";
86 string r1="Nom: Aubertin Prénom: Sylvain Date naissance: 22/11/1928 telephone: +659065731\n";
87 string r2="Nom: Dumas Prénom: Jean Date de naissance: 12/09/1956 Telepnone: 0645327667\n";
88 string r3="Nom: Charlot Prénom: Charles Date de naissance: 3/5/2000 Téléphone: 0665432876\n";
89 printString (r2);
90 cout << "Use of CONST\n";
91 string st1=" INDEX \n";
92 string st2=" SALUT \n";
93 print_string(st1);
94 print_string(st2);
95 }
The insert function takes an iterator as first argument.

If you want to insert at the sixth position you can use phone_book.begin() to get an iterator to the first element and then std::next to advance the iterator five steps.

 
phone_book.insert(std::next(phone_book.begin(), 5), Entry("inserted", 40909567));

http://www.cplusplus.com/reference/list/list/insert/
http://www.cplusplus.com/reference/iterator/next/
Last edited on
reference.cpp:77:19: error: ‘next’ is not a member of ‘std’
phone_book.insert(std::next(phone_book.begin(), 5), Entry("inserted", 40909567));
That is what I got (compiler g++), after I used your above suggestion. Many thanks for your help
To use std::next you need to use C++11 or later. If your compiler is not too old you can enable it by using the compiler flag -std=c++11 (or -std=c++14).

Another option is to use std::advance.

1
2
3
list<Entry>::iterator it = phone_book.begin();
std::advance(it, 5);
phone_book.insert(it, Entry("inserted", 40909567));

http://www.cplusplus.com/reference/iterator/advance/
Last edited on
OK for insert. Thank you very much. Can you help me now for "erase" ? You should be very kind.
Same problem. Erase takes an iterator to the element that you want to remove as argument.

http://www.cplusplus.com/reference/list/list/erase/
Before you answered I found the solution. Now all things are right. Thanks again
Topic archived. No new replies allowed.