We hope to concentrate on OOP and STL today.
Reference:
Nice one on the <vectors>
The tutorial on STL: http://www.mochima.com/tutorials/STL.html
=======================
Note that
class less
{
int val;
public:
less(int v): val(v) {} // this is to assign val = v to initialize the threshold
};
useage:
less less_than_five(5);
For references, they often act like a "alias" -- good new is that when passing the parameter through references, there is not copy of that parameter, such that you don't have to allocate more from the memory.
For template -- it acts like the "decoration"
Below is to get the reference of the smaller one:
template<class T>
T& min(T&a, T&b)
{
return a<b ? a:b;
}
An example of "vector"
template<class T>
class vector
{
T* v;
int sz;
public:
vector(int s) {v = new T[sz = s];}
~vector() {delet []v; }
T & operator[] (int i) {return v[i]; }
int get_size() {return sz;}
}
usage:
vector<int> int_vector(10);
Some STL- FTP:
1) HP ( Alexander Stepanov and Meng Lee) ftp://butler.hpl.hp.com/stl/
2) David Mussers --- http://www.cs.rpi.edu/~musser/stl.html
Some other Resources
[Containers]
set, multiset, map, multimap
Difference between set(multiset) and map(multimap)
set(multiset) --> containd the KEY
map(multimap) --> separate the key and corresponding value
-------------------------------
class employee_data
{
public:
employee_data(): name(""), skill(0); salary(0) {}
employee_data(string n, int s, long sa): name(n), skill(s), salary(sa) {}
string name;
int skill;
long salary;
friend ostream& operater<<(ostream& os, const employee_data& e);
};
ostream& operator<<(ostream& os, const employee_data& e)
{
os<< "employee: " << e.name << " " << e.skill << "" << e.salary;
}
class employee
{
public:
employee(int ii, const employee_data &e): identification_code(ii), description(e){}
int identification_code;
employee_data description;
bool operator<(const employee& e) const
{
return identification_code <e.identification_code;
}
};
if use the set(multiset):
set<employee, less<employee>> employee_set;
multiset<employee, less<employee>> employee_multiset; // notice that the KEY and values are together, which is employee
if use the map(multimap):
map<int, employee_data, less<int>> employee_map;
multimap<int, employee_data, less<int>> employee_multimap;
when using them:
employee_data ed("john", 1, 500); // Jone, skill-1, salari(500)
employee e1(1010, ed); // John wiht identification_code as 1010
employee_set.insert(e1); // to add the information of John
employee_set.insert(e1); // this won't work
employee_multimap.insert(makepair(101, ed1)); // to add the information of John
employee_multimap.insert(makepair(101, ed2)); // to add the information of another one also with the key as 101
--> so one key --> maps to multiple people
--
♥ ¸¸.•*¨*•♫♪♪♫•*¨*•.¸¸♥
No comments:
Post a Comment