Monday, November 15, 2010

[DSA] STL

My favorite quote from Issac Newton "I am Standing on the shoulders of giants" greatly illustrate the idea of STL. Code have been written by other programmers, using the template feature to greatly help other programmers to overcome programming difficulties. I.e, reduce the time from concept to code.


Being a lazy programmer I am (yes I do admit I like to find the easiest and shortest possible way of writing a piece of code), I do not like to reinvent the wheel.

As in write my own custom code that serves the same purpose as those already written/tested/proven best code by the c++ community.

Why make your life suffer when there are already ready code for you to use?

Programming, in general, is the ability to synthesize code to solve real-world problem by making use of available code. Only in the very dire situation (think high security, high accuracy and the need for real time operation), custom code is needed. *read=> proprietary software*
By learning all the basic building blocks of programming such as template, variable passing etc. When one day this dire state occur, YOU have the ability to create the code library to save the day~!

The STL contains three main components:


Containers
Iterators
Algorithms


Containers, as the name suggested is some form of storage for our data.

Iterators, instead of the programmer manually keeping track on 
ALL pointers declared and used (not an easy feat when the code is in the range of >10k lines ), this STL-Iterator helps the programmer to achieve the same objective.

Algorithms, fancy writing some code to solve the question in the math tutorial? Here is the partial solution. You need to piece this building blocks together.


Drill question



1. We have written some code to check whether a given word is a palindrome in the previous week, by using some member functions of the string class. Good work guys. 
Now, use STL containers, iterators to achieve the same objective.

2. Write a phonebook directory that let the user to store
1. first name
2. phone number

(Store some dummy data into the phone book for testing,)
and the ability to "scroll" the phone book

and the ability to "search" for a particular entry in the phone book.

hint: use ready code from
-Containers
-Iterators
-Algorithms

*non-OOP implementation is acceptable for now.*


http://www.cppreference.com/wiki/stl/algorithm/start
http://www.cplusplus.com/reference/stl/

23 comments:

  1. sir i dono how to put in sue before betty!!!

    cheers

    ReplyDelete
  2. DCPE 2b05 Lim Guan YanNovember 16, 2010 at 10:39 AM

    Sir, how do i meet you at thursday for the revision quiz ???

    ReplyDelete
  3. http://snipt.net/jellybanana/week-5-class-practice-1

    ReplyDelete
  4. http://snipt.net/jellybanana/week-5-class-exercise-1a

    ReplyDelete
  5. http://snipt.net/hengnee/class-exercise-week-5

    ReplyDelete
  6. http://snipt.net/Stish/stl-2

    ReplyDelete
  7. http://snipt.net/tintun#add

    ReplyDelete
  8. http://snipt.net/bigshow/vector-4

    ReplyDelete
  9. http://snipt.net/sa198/qi-haodcpe2b03-8?key=9619d5c57cbd9dbdb66b262ced3f6d18

    ReplyDelete
  10. http://snipt.net/UJuzGtJacked/vector-5?key=d3919713cc0075f00c4b85df5503628c

    ReplyDelete
  11. http://snipt.net/jellybanana/week-5-class-exercise-1b/

    ReplyDelete
  12. http://snipt.net/UJuzGtJacked/phonebook-6?key=8c1547a6b29f027e4874733f66e5aa70


    got a problem with this assignment. i cant seem to display the phone number. any idea y?

    ReplyDelete
  13. @felix,
    you have 2 vector, one is to track name and the other to track phone num.

    and your "it" only tracks the name vector.

    to quickly solve ur prob. def it2 and points to phone.begin()

    cout in the same for loop.

    ALT, prolly you want to look at using OOP to implement the phone book.

    catchup with me on how to write it.

    ReplyDelete
  14. http://snipt.net/UJuzGtJacked/phonebook-6?key=8c1547a6b29f027e4874733f66e5aa70

    sorry to bother again. i am stuck. cant seem to tie the name together with the number. so end up i can only search the name. i was thinking of using the memory position number(not memory address) but got no idea how to write it. any ideas?

    ReplyDelete
  15. "at = phone.begin();
    for (it=name.begin();it!=name.end(); it++)
    {
    cout << *it<<" "<<endl;
    cout <<*at<<endl;
    at++;
    }"

    Do you mean this when you said "tie up"? The name and number won't appear together if you're outputting the names first and then the numbers.

    What do you mean by searching the name? Do you mean finding the phone number with the name? If so, use a search algorithm and have a count setup to find it's location in the vector. Then just iterate through the number vector and output it.

    ReplyDelete
  16. Oh and you have to use that chunk of code to replace this part.

    "for(it=name.begin();it!=name.end(); it++)
    {
    cout << *it<<" "<<endl;

    }
    for(at=phone.begin();at!=phone.end(); at++)
    {
    cout <<*at<<endl;
    }"

    ReplyDelete
  17. alvinthen dcpe/ft/2b/03November 20, 2010 at 5:26 PM

    http://snipt.net/alvinthen/stl-drill-qn?key=2930fa511168a0f7999ebbed06039e8e


    any idea why when i type in the name and number and it returned not 1x but instead 5x??

    ReplyDelete
  18. You placed output code inside a nested for loop. You only need to increment "nit" inside the previous for loop.

    nit = Num.begin();
    for (it=Name.begin();it!=Name.end();it++){
    cout<<*it<<" "<<*nit<<" "<<endl;
    nit++;
    }

    ReplyDelete
  19. alvinthen dcpe/ft/2b/03November 20, 2010 at 7:15 PM

    to HY;
    thx alot for the advice..its working well

    ReplyDelete
  20. alvinthen dcpe/ft/2b/03November 20, 2010 at 10:38 PM

    http://snipt.net/alvinthen/stl-drill-qn?key=2930fa511168a0f7999ebbed06039e8e

    i manage to get the correct name but i am unable to get the correct number assigned to the name. Instead i get some ramdom numbers. Anyone can advise me on how to corectly assign the number to the names?

    ReplyDelete
  21. That's because the iterator is not pointing at the correct address. You need to move it to the correct address.

    ReplyDelete
  22. http://snipt.net/UJuzGtJacked/phonebook-6?key=8c1547a6b29f027e4874733f66e5aa70

    ReplyDelete
  23. @felix, now that is what i call a pointer~

    ->

    ReplyDelete