@@ -292,4 +292,130 @@ namespace N {class T {};} // Hide name T
292292N::T t; // Use name T in namespace N
293293using namespace N; // Make T visible without N::
294294~~~~
295-
295+
296+ ## MATH.H, CMATH (Floating point math)
297+ ~~~~
298+ #include <cmath> // Include cmath (std namespace)
299+ sin(x); cos(x); tan(x); // Trig functions, x (double) is in radians
300+ asin(x); acos(x); atan(x); // Inverses
301+ atan2(y, x); // atan(y/x)
302+ sinh(x); cosh(x); tanh(x); // Hyperbolic
303+ exp(x); log(x); log10(x); // e to the x, log base e, log base 10
304+ pow(x, y); sqrt(x); // x to the y, square root
305+ ceil(x); floor(x); // Round up or down (as a double)
306+ fabs(x); fmod(x, y); // Absolute value, x mod y
307+ ~~~~
308+
309+ ## ASSERT.H, CASSERT (Debugging aid)
310+ ~~~~
311+ #include <cassert> // Include iostream (std namespace)
312+ assert(e); // If e is false, print message and abort
313+ #define NDEBUG // (before #include <assert.h>), turn off assert
314+ ~~~~
315+
316+ ## IOSTREAM.H, IOSTREAM (Replaces stdio.h)
317+ ~~~~
318+ #include <iostream> // Include iostream (std namespace)
319+ cin >> x >> y; // Read words x and y (any type) from stdin
320+ cout << "x=" << 3 << endl; // Write line to stdout
321+ cerr << x << y << flush; // Write to stderr and flush
322+ c = cin.get(); // c = getchar();
323+ cin.get(c); // Read char
324+ cin.getline(s, n, '\n'); // Read line into char s[n] to '\n' (default)
325+ if (cin) // Good state (not EOF)?
326+ // To read/write any type T:
327+ istream& operator>>(istream& i, T& x) {i >> ...; x=...; return i;}
328+ ostream& operator<<(ostream& o, const T& x) {return o << ...;}
329+ ~~~~
330+
331+ ## FSTREAM.H, FSTREAM (File I/O works like cin, cout as above)
332+
333+ ~~~~
334+ #include <fstream> // Include filestream (std namespace)
335+ ifstream f1("filename"); // Open text file for reading
336+ if (f1) // Test if open and input available
337+ f1 >> x; // Read object from file
338+ f1.get(s); // Read char or line
339+ f1.getline(s, n); // Read line into string s[n]
340+ ofstream f2("filename"); // Open file for writing
341+ if (f2) f2 << x; // Write to file
342+ ~~~~
343+
344+ ## STRING (Variable sized character array)
345+ ~~~~
346+ #include <string> // Include string (std namespace)
347+ string s1, s2="hello"; // Create strings
348+ s1.size(), s2.size(); // Number of characters: 0, 5
349+ s1 += s2 + ' ' + "world"; // Concatenation
350+ s1 == "hello world" // Comparison, also <, >, !=, etc.
351+ s1[0]; // 'h'
352+ s1.substr(m, n); // Substring of size n starting at s1[m]
353+ s1.c_str(); // Convert to const char*
354+ getline(cin, s); // Read line ending in '\n'
355+ ~~~~
356+
357+ ## VECTOR (Variable sized array/stack with built in memory allocation)
358+
359+ ~~~~
360+ #include <vector> // Include vector (std namespace)
361+ vector<int> a(10); // a[0]..a[9] are int (default size is 0)
362+ vector<int> b{1,2,3}; // Create vector with values 1,2,3
363+ a.size(); // Number of elements (10)
364+ a.push_back(3); // Increase size to 11, a[10]=3
365+ a.back()=4; // a[10]=4;
366+ a.pop_back(); // Decrease size by 1
367+ a.front(); // a[0];
368+ a[20]=1; // Crash: not bounds checked
369+ a.at(20)=1; // Like a[20] but throws out_of_range()
370+ for (int& p : a)
371+ p=0; // C++11: Set all elements of a to 0
372+ for (vector<int>::iterator p=a.begin(); p!=a.end(); ++p)
373+ *p=0; // C++03: Set all elements of a to 0
374+ vector<int> b(a.begin(), a.end()); // b is copy of a
375+ vector<T> c(n, x); // c[0]..c[n-1] init to x
376+ T d[10]; vector<T> e(d, d+10); // e is initialized from d
377+ ~~~~
378+
379+ ## DEQUE (array/stack/queue)
380+
381+ deque<T > is like vector< ; T> ; , but also supports:
382+ ~~~~
383+ #include <deque> // Include deque (std namespace)
384+ a.push_front(x); // Puts x at a[0], shifts elements toward back
385+ a.pop_front(); // Removes a[0], shifts toward front
386+ ~~~~
387+
388+ ## UTILITY (Pair)
389+ ~~~~
390+ #include <utility> // Include utility (std namespace)
391+ pair<string, int> a("hello", 3); // A 2-element struct
392+ a.first; // "hello"
393+ a.second; // 3
394+ ~~~~
395+
396+ ## MAP (associative array - usually implemented as red-black trees)
397+ ~~~~
398+ #include <map> // Include map (std namespace)
399+ map<string, int> a; // Map from string to int
400+ a["hello"]=3; // Add or replace element a["hello"]
401+ for (auto& p:a)
402+ cout << p.first << p.second; // Prints hello, 3
403+ a.size(); // 1
404+ ~~~~
405+
406+ ## ALGORITHM (A collection of 60 algorithms on sequences with iterators)
407+ ~~~~
408+ #include <algorithm> // Include algorithm (std namespace)
409+ min(x, y); max(x, y); // Smaller/larger of x, y (any type defining <)
410+ swap(x, y); // Exchange values of variables x and y
411+ sort(a, a+n); // Sort array a[0]..a[n-1] by <
412+ sort(a.begin(), a.end()); // Sort vector or deque
413+ ~~~~
414+
415+
416+
417+
418+
419+
420+
421+
0 commit comments