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:

Douglas said...

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

cheers

DCPE 2b05 Lim Guan Yan said...

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

Yi Wei said...

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

Yi Wei said...

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

heng gnee dcpe/ft/2b/03 said...

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

Sathis 2b03 said...

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

tintundcpe2b03 said...

http://snipt.net/tintun#add

XuZhou said...

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

Qi Hao(DCPE/2B/03) said...

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

Felix (2b03) said...

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

Yi Wei said...

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

Felix (2b03) said...

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?

sjteo said...

@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.

Felix (2b03) said...

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?

HY said...

"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.

HY said...

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;
}"

alvinthen dcpe/ft/2b/03 said...

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??

HY said...

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++;
}

alvinthen dcpe/ft/2b/03 said...

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

alvinthen dcpe/ft/2b/03 said...

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?

HY said...

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

Felix (2b03) said...

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

sjteo said...

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

->