In the class, we went over important iterators such as front_insert_iterator (/back_insert_iterator) and their corresponding function objects front_inserter (/back_inserter). These iterators conform to the STL conventions, and could be used with other STL algorithms such as copy. These come in handy when there is a situation such as,
vector
list
std::copy(x.begin(), x.end(), back_inserter(y));
This will result into copying of all the elements of x into y, after which y.size() == 15. However, to have the copy() and other STL algorithms work, the front and back inserters have a peculiar design. The operator *(), operator ++ () return *this of the same type as the class, while operator = (const T& v) also returns itself, but does x.push_front(v) (or x.push_back(v)). A similar design is followed by the ostream_iterator and istream_iterator. However, istream_iterator do maintain data member istream* in to consider the !=EOF criteria.
We learned about few more subtleties of C++ (rather I should say, very important subtleties that if not aware of can lead to programmer's frustration). We studied as to when an argument is taken as a function pointer, and when it's not. For eg.
A x(2,3,4); //constructor
A t; //default constructor
A z(); //function declaration whose return type is A
//LEGAL function declarations
int f(int);
int g(int x); //argument with a name
int h(int (x));
int q((int x)); //ILLEGAL
Likewise, few other examples,
int f(int (*p) ()); //named parameter, with a pointer notation
int g(int p()); //named parameter
int h(int ()); //no name
//all of above mean the same
int m (int (int)); valid
int n (int (int x)); probably unreasonable
In case of ambiguity, the declaration of function wins.
No comments:
Post a Comment