Thursday, October 26, 2017

C++ primer notes

1.1.1. Compiling and Executing Our Program
The value returned from main is accessed in a system-dependent manner. On both UNIX and Windows systems, after executing the program, you must issue an appropriate echo command.
On UNIX systems, we obtain the status by writing
$ echo $?
To see the status on a Windows system, we write
$ echo %ERRORLEVEL%

By default, the command to run the GNU compiler is g++:
$ g++ -o prog1 prog1.cc
The -o prog1 is an argument to the compiler and names the file in which to put the executable file.

The command to run the Microsoft Visual Studio 2010 compiler is cl:
C:\Users\me\Programs> cl /EHsc prog1.cpp
The cl command invokes the compiler, and /EHsc is the compiler option that turns on standard exception handling.

Compilers usually include options to generate warnings about problematic constructs. Our preference is to use -Wall with the GNU compiler, and to use /W4 with the Microsoft compilers.

1.2. A FIRST LOOK AT INPUT/OUTPUT
C++ includes an extensive standard library that provides IO (and many other facilities).
Fundamental to the iostream library are two types named istream and ostream , which represent input and output streams, respectively. A stream is a sequence of characters read from or written to an IO device.
standard input,  istream object cin
standard output, ostream object cout
standard error,  warning and error messages ostream object cerr
general information, clog

Because the operator << returns its left-hand operand, the result of the first operator becomes the left-hand operand of the second. As a result, we can chain together output requests.
std::cout << "Enter two numbers:" << std::endl;
is equivalent to

(std::cout << "Enter two numbers:") << std::endl;

The second operator prints endl, which is a special value called a manipulator. Writing endl has the effect of ending the current line and flushing the buffer associated with that device. Flushing the buffer ensures that all the output the program has generated so far is actually written to the output stream, rather than sitting in memory waiting to be written.

The prefix std:: indicates that the names cout and endl are defined inside the namespace named std . Namespaces allow us to avoid inadvertent collisions between the names we define and uses of those same names inside a library. All the names defined by the standard library are in the std namespace.

Writing std::cout uses the scope operator (the :: operator) to say that we want to use the name cout that is defined in the namespace std.

Comment pairs:
std::cout << "/*";
std::cout << "*/";
std::cout << /* "*/" */;     <- only this line is illegal
std::cout << /* "*/" /* "/*" */;

Monday, October 23, 2017

Customized hash function

https://stackoverflow.com/questions/32685540/c-unordered-map-with-pair-as-key-not-compiling

You need to provide a suitable hash function for your key type. A simple example:
#include <unordered_map>
#include <functional>
#include <string>
#include <utility>

// Only for pairs of std::hash-able types for simplicity.
// You can of course template this struct to allow other hash functions
struct pair_hash {
    template <class T1, class T2>
    std::size_t operator () (const std::pair<T1,T2> &p) const {
        auto h1 = std::hash<T1>{}(p.first);
        auto h2 = std::hash<T2>{}(p.second);

        // Mainly for demonstration purposes, i.e. works but is overly simple
        // In the real world, use sth. like boost.hash_combine
        return h1 ^ h2;  
    }
};

using namespace std;
using Vote = pair<string, string>;
using Unordered_map = unordered_map<Vote, int, pair_hash>;

int main() {
    Unordered_map um;
}