C++ notes
From raju
inherit map
checklist when factoring out a function
possible fixes to common g++ errors
function declarations
Reference
Type of iterators
Complexity of operations
Lists
static variables
IDE reviews
Programming questions
Q. How to convert a string to const char *?
A. Use the c_str() function to get a const char * from std::string.
std::string str; const char * c = str.c_str();
Ref:- http://stackoverflow.com/questions/347949/how-to-convert-a-stdstring-to-const-char-or-char
Q. vector of pairs vs map, which one to use?
A. http://stackoverflow.com/questions/454762/vector-or-map-which-one-to-use
maps
- To remove an element - map::erase
- To find value by key - map::find
... map<string, int> myFruit; myFruit["apple"] = 1; myFruit["pear"] = 2; myFruit["grape"] = 3; ... string key; int value; ... auto it=myFruit.find(key); if (it != myFruit.end()) { cout << key << "'s number is " << it->second << "\n"; } else { cout << key << " is not found in the fruit map.\n"; } ...
What is the use of extern keyword
- http://www.geeksforgeeks.org/understanding-extern-keyword-in-c/
- http://stackoverflow.com/questions/10422034/when-to-use-extern-in-c
unit testing
- cppunit can be used for unit testing in C++
- Where are CPPUNIT_ASSERT_MESSAGE, CPPUNIT_ASSERT_EQUAL_MESSAGE declared?
They are in include/cppunit/TestAssert.h
- To catch exceptions thrown by cppunit
try { ... } catch (CppUnit::Exception& x) { clog << "Exception from CppUnit." << endl << x.what() << endl; return -1; }
vector of strings
- To iterate over vector of strings
vector<string> a; ... for (vector<string>::const_iterator it = a.begin(); it != a.end(); ++it) { const string& elem = *it; .... cout << elem << "\n"; }
In C++11
for (auto i : a) { cout << i << "\n"; }
Ref:- http://stackoverflow.com/questions/11170349/iterating-over-a-vector-of-strings-in-c
general tips
- Let's say you have a member variable that can only be initialized by calling a function and it needs to be a constant private member (ex:- const map<string, int>). To initialize it
Declare it as a reference to a constant static member variable (in .H file) Create a static member function that initializes this type of object and returns a reference to it (ex:- const map<string, int>&) Initialize the static private constant member variable to this function (in .C file)
So the header file looks as
class a { private: static const map<string, int>& _m; static const map<string, int>& _set_m(); };
and the source file looks as
const map<string, int>& a::_m = a::_set_m(); const map<string, int>& a::_set_m() { static map<string, int> m; m['apple'] = 1; m['banana'] = 2; m['cantaloupe'] = 3; return m; }
External links
- A very good FAQ on when to use virtual destructors.
- A very good FAQ on exceptions and error handling.
- An easy to read tutorial on virtual functions - http://www.learncpp.com/cpp-tutorial/122-virtual-functions/ . It contains sample code as well.
- Why do we need virtual functions in C++ - http://stackoverflow.com/a/2392656/6305733
- An easy to understand tutorial on virtual base classes - http://www.learncpp.com/cpp-tutorial/118-virtual-base-classes/ . It contains sample code as well.