STL LIST::Random access

List<author*>catalog['Z'+1];
1
2
3
4
5
6
7
8
9
10
11
12
13
 void includebook()
{
book*newbook=new book();
    author*newauthor=new author();
    newbook->name=getstring("Enter name of the book you want to enter");
    newauthor->name=getstring("Enter the name of author who wrote this book");
    list<author*>::iterator oldauthor=findit(catalog[newauthor->name[0]],*newauthor);
    if(oldaauthor==catalog[newauthor->name[0]].end())
    newauthor->books.push_front(newbook);
 catalog[newauthor->name[0]].push_front(newauthor)
 else
 (*oldauthor)->books.push_front(newbook);
}


HI All,
Please find necessary definitions to understand the above code.
1
2
3
4
5
6
7
template<class T1,class T2>
List<T2>::iterator findit(const List<T2>&lst,const T1&el)
{for(List<T2>::iterator ref=lst.begin();ref!=lst.end();ref++)
   if(**ref==el) \\== overloaded
break;
return ref;
}

Author comprises of Two data memebrs:
1
2
char*name
list<book*>books;
The above includebook() function has to include a book into a library,it searches if author exist if he exists it adds a book into the list author already has.If author doesnt exist then it adds new author into the list and add this cuurent book as his first book.

Now My doubts are:
1)List<author*>catalog['Z'+1];what does this mean? what is 'Z' doing?
2)In Includebook() function
while catalog is a list whey are they accessing it randomly with [] subscript while random access is not allowed in List?
catalog[newauthor->name[0]];
and why are they using just the first letter of authir;s name(newauthor->name[0])rather than full name for this search?

Please help

Thanks in advance
Prasad
Last edited on
'Z' resolves to a [ASCII] number:

http://www.asciitable.com/

This List<author*>catalog['Z'+1]; becomes List<author*>catalog[90+1];

In other words: catalog is an array of 91 elements of type List<author*>

while catalog is a list
it's not a list it's an array. See above

why are they using just the first letter of authir
Most likely for quick and easy access. The first letter also resolves to such a [ASCII] number which is (with a bit luck) always smaller than 91
Topic archived. No new replies allowed.