Monday, May 23, 2016

Entries impossible in a Stoustrup code example

The mame of this code is "list". It seems to be incomplete (nowhere a cin instruction). It runs but it doesn't work. May any one help me (I am a beginner).
Here is the code:

/*phone_book.cpp*/

#include<list>
#include<iostream>
#include<string>
using namespace std;

struct Entry {
string name;
int number;
Entry(const string& n, int i) :name(n), number(i) { }
};

list<Entry> phone_book;

void print_entries()
/*
this kind of function should use a parameter,
rather then a global name
*/
{
typedef list<Entry>::const_iterator LI;

for (LI i = phone_book.begin(); i != phone_book.end(); ++i) {
const Entry& e = *i; // reference used as shorthand
cout << '{' << e.name << ' ' << e.number << "}\n";
}
}

void print_entry(const string& s)
/*
Is this the right treatment of a string not found?
*/
{
typedef list<Entry>::const_iterator LI;
// 13/
for (LI i = phone_book.begin(); i != phone_book.end(); ++i) {
const Entry& e = *i; // reference used as shorthand
if (s == e.name) {
cout << e.name << ' ' << e.number << '\n';
return;
}
}
}

void f(const Entry& e, list<Entry>::iterator i, list<Entry>::iterator p)
/*
just some nonsense code
*/
{
phone_book.push_front(e); // add at beginning
phone_book.push_back(e); // add at end
phone_book.insert(i,e); // add before the element referred to by `i'

phone_book.erase(p); // remove the element referred to by `p'
}

int main()
{

phone_book.push_back(Entry("one",1));
phone_book.push_back(Entry("two",2));
phone_book.push_back(Entry("three",3));
phone_book.push_back(Entry("four",4));
phone_book.push_back(Entry("five",5));
Entry six("six",6);
print_entries();
f(six,phone_book.begin(),phone_book.begin());
print_entries();
print_entry("four");
print_entry("seven");
print_entry("three");
}
Thanks again

--
--
You received this message from the "vim_use" maillist.
Do not top-post! Type your reply below the text you are replying to.
For more information, visit http://www.vim.org/maillist.php

---
You received this message because you are subscribed to the Google Groups "vim_use" group.
To unsubscribe from this group and stop receiving emails from it, send an email to vim_use+unsubscribe@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

No comments:

Post a Comment