Posts

Showing posts with the label stl

C++ Break out of a function at an early stage

C++ Break out of a function at an early stage I have two questions. The first is about working with functions. I need to break out of a function at an early stage under a condition. For example: std::string concat(std::string& x, std::string& y, std::vector<std::string>& vec) { if (atoi(x.c_str()) < 0) { return; } else { std::string concatStr = y + x; top_back(vec); top_back(vec); return concatStr; } } As you can see, the function must return a string, but if the string x(which i of course convert to int) is less than 0, then I theoretically should break out of the function. The problem with writing just return; is that the compiler tells me that it needs to return a value. return; The second question is how can I remove the last line from the console? That's connected with the first question, as someone suggested that return ""; is a good workaround, but it writes a blank space into the con...

Why list iterator has three template arguments in SGI STL's implemention?

Why list iterator has three template arguments in SGI STL's implemention? I have a problem when I'm reading SGI STL's implementation of list iterator. template<class T> struct __list_node { void *prev; void *next; T data; }; template<class T, class Ref, class Ptr> struct __list_iterator { typedef __list_iterator<T, T&, T*> iterator; typedef __list_iterator<T, Ref, Ptr> self; ... typedef T value_type; typedef Ptr pointer; typedef Ref reference; typedef __list_node<T>* link_type; ... link_type node; ... reference operator*() const { return (*node).data; } pointer operator-> const { return &(operator*()); } self& operator++() { ... } self operator++(int) { ... } ... ... }; So, why are there three template arguments? What if only class T exists? Something likes below. class T template<class T> struct __list_iterator { typedef __list_iterator<T...