C

From 太極
Jump to navigation Jump to search

Socket Programming

Terms

There are two types of address domains.

  • AF_UNIX: the unix domain for two processes which share a common file system, and
  • AF_INET: the Internet domain for any two hosts on the Internet.

There are two types of sockets.

  • a stream socket in which characters are read in a continuous stream as if from a file or pipe, and
  • a datagram socket, in which messages are read in chunks.

The two symbolic constants are SOCK_STREAM and SOCK_DGRAM.

There are two types of ports:

  • well known Ports | those that rarely change overtime. For instance, servers that provide mail, file transfer, remote login,

etc.

  • dynamic ports | typically used only for the life of a process. For instance, pipes can be implemented using message passing

Examples of servers:

  • mail server
  • login server
  • file server
  • web server
  • streaming server

TCP vs UDP

  • TCP is used for services with a large data capacity, and a persistent connection
  • „ UDP is more commonly used for quick lookups, and single use query-reply actions.
  • Some common examples of TCP and UDP with their default ports:
    • DNS lookup UDP 53
    • FTP TCP 21
    • HTTP TCP 80
    • POP3 TCP 110
    • Telnet TCP 23

General Idea

The steps involved in establishing a socket on the client side are as follows:

  1. Create a socket with the socket() system call
  2. Connect the socket to the address of the server using the connect() system call
  3. Send and receive data. There are a number of ways to do this, but the simplest is to use the read() and write() system calls.

The steps involved in establishing a socket on the server side are as follows:

  1. Create a socket with the socket() system call
  2. Bind the socket to an address using the bind() system call. For a server socket on the Internet, an address consists of a port number on the host machine.
  3. Listen for connections with the listen() system call
  4. Accept a connection with the accept() system call. This call typically blocks until a client connects with the server.
  5. Receive and send data by using read() and write().

Internet Hearsay

View all TCP sockets currently active
$ netstat --tcp

View all UDP sockets
$ netstat --udp

View all TCP sockets in the listening state
$ netstat --listening

View the multicast group membership information
$ netstat --groups

Display the list of masqueraded connections
$ netstat --masquerade

View statistics for each protocol
$ netstat --statistics

Display all traffic on the eth0 interface for the local host
$ tcpdump -l -i eth0

Show all traffic on the network coming from or going to host plato
$ tcpdump host plato

Show all HTTP traffic for host camus
$ tcpdump host camus and (port http)

View traffic coming from or going to TCP port 45000 on the local host
$ tcpdump tcp port 45000

Make http request via telnet

Below, we only input two lines. One is telnet linus.nci.nih.gov 80 and the other is HEAD / HTTP/1.0\n\n. Remember the one carriage character and one line feed at the end of request line. We can change the HTTP method in the 2nd input to GET /HTTP/1.0\n\n to fetch the full page. See the book HTTP: The Definitive Guide and wikipedia.

$ telnet linus.nci.nih.gov 80
Trying 137.187.182.124...
Connected to ncias-p942-v-1.nci.nih.gov.
Escape character is '^]'.
HEAD / HTTP/1.0

HTTP/1.1 200 OK
Date: Thu, 21 Mar 2013 14:47:26 GMT
Server: Apache
Last-Modified: Tue, 12 Mar 2013 13:52:32 GMT
ETag: "302a-4d7ba99db0800"
Accept-Ranges: bytes
Content-Length: 12330
Connection: close
Content-Type: text/html

Connection closed by foreign host.
$

Socket Programming Examples using C/C++/Qt

Example 1 - Linux/Unix

http://www.linuxhowtos.org/C_C++/socket.htm. The codes are saved under here.

$ gcc server.c -o server
$ gcc client.c -o client
$ ./server 51717
$ ./client 192.168.0.21 51717
Please enter the message: Who are you?
I got your message

where 192.168.0.21 is the ip address on the server. The server side will show

Here is the message: Who are you?

If everything works correctly, the server will display your message on stdout, send an acknowledgement message to the client and terminate. The client will print the acknowledgement message from the server and then terminate.

  • We can actually run the client on a different machine (eg server on my Ubuntu and client on my Windows) although we can also run both client and server on the same machine.
  • Once we use the port (51717) one time, we can not use the same port to run it again??? The screen shows an error "ERROR on binding: Address already in use". The problem is we may need to wait until 4 minutes for avoiding this message. See the solution in here or SO_REUSEADDR option in setsockopt(). That is, we just need to change server.c to add the following
 ...
 int iOption = 1; // Turn on keep-alive, 0 = disables, 1 = enables
 ...
 // Immediately after the declaration of sockfd, we do
 if (setsockopt(sockfd, SOL_SOCKET, SO_REUSEADDR, (const char *) &iOption,  sizeof(int)) == -1) {
    error("setsockopt");
    exit(1);
 }
  • On Windows, we can use TCPView to see which process is listening on which port, socket status. On Linux, we can use netstat -a command (it gives a long output)
mli@PhenomIIx6:~/Downloads$ sudo netstat -a | grep 51717
tcp        0      0 *:51717                 *:*                     LISTEN
  • We can choose any port number between 2000 and 65535.
  • If we use 51717 port for example, the server will open that port. But once the program is finished, the port will be closed immediately. Use linux command netstat -lp --inet to check which ports are opened.

Similar examples:

Example 2 (in C)

http://www.prasannatech.net/2008/07/socket-programming-tutorial.html

Server side (The code is here):

$ ./tcpserver

TCPServer Waiting for client on port 5000
 I got a connection from (127.0.0.1 , 36123)
 SEND (q or Q to quit) : yes

 RECIEVED DATA = Got it.
 SEND (q or Q to quit) : how are you

 RECIEVED DATA = I am fine.
 SEND (q or Q to quit) : q

q
^C
$

Client side (The code is here):

$ ./tcpclient

Recieved data = yes
SEND (q or Q to quit) : Got it.

Recieved data = how are you
SEND (q or Q to quit) : I am fine.

Example 3 - Simple HTTP server

The example is modified (hear files only) from http://rosettacode.org/wiki/Hello_world/Web_server (This is a wonderful website which include creating hello world web server using different programming languages). PS. the instruction in http://mwaidyanatha.blogspot.com/2011/05/writing-simple-web-server-in-c.html is worth a look but 4 lines of creating HTML standard headlines is not successful and too complicated.

We can test the server by

#include<netinet/in.h>
#include<stdio.h>
#include<stdlib.h>
#include<sys/socket.h>
#include<sys/stat.h>
#include<sys/types.h>
#include<unistd.h>

char response[] = "HTTP/1.1 200 OK\r\n"
"Content-Type: text/html; charset=UTF-8\r\n\r\n"
"<html>\r\n"
"<head><title>Bye-bye baby bye-bye</title>\r\n"
"<style>\r\n"
"   body { background-color: #111 }\r\n"
"   h1 { font-size:4cm; text-align: center; color: black;"
"   text-shadow: 0 0 2mm red} \r\n"
"</style></head>\r\n"
"<body><h1>Goodbye, world!</h1></body></html>\r\n";
 
int main()
{
	int one = 1, client_fd;
	struct sockaddr_in svr_addr, cli_addr;
	socklen_t sin_len = sizeof(cli_addr);
 
	int sock = socket(AF_INET, SOCK_STREAM, 0);
	if (sock < 0)
		err(1, "can't open socket");
 
	setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, &one, sizeof(int));
 
	int port = 8080;
	svr_addr.sin_family = AF_INET;
	svr_addr.sin_addr.s_addr = INADDR_ANY;
	svr_addr.sin_port = htons(port);
 
	if (bind(sock, (struct sockaddr *) &svr_addr, sizeof(svr_addr)) == -1) {
		close(sock);
		err(1, "Can't bind");
	}
 
	listen(sock, 5);
	while (1) {
		client_fd = accept(sock, (struct sockaddr *) &cli_addr, &sin_len);
		printf("got connection\n");
 
		if (client_fd == -1) {
			perror("Can't accept");
			continue;
		}
 
		write(client_fd, response, sizeof(response) - 1); /*-1:'\0'*/
		close(client_fd);
	}
}

Compile and run it by gcc testServer.c; ./a.out.

Example 4 - Mimic browser request

The code is based on the post. http://codebase.eu/tutorial/linux-socket-programming-c/. My local copy of [1].

This is another similar post. http://www.binarytides.com/receive-full-data-with-recv-socket-function-in-c/ which teaches how to receive full data with recv socket function in C.

Testing tcpclient.c

The result is a program that connects to google and downloads (the first 1000 bytes of) the google homepage.

$ g++ tcpclient.cpp
$ ./a.out
Setting up the structs...
Creating a socket...
Connect()ing...
send()ing message...
Waiting to recieve data...
1000 bytes recieved :
HTTP/1.1 200 OK
Date: Wed, 20 Mar 2013 14:41:54 GMT
Expires: -1
Cache-Control: private, max-age=0
Content-Type: text/html; charset=ISO-8859-1
Set-Cookie: PREF=ID=78ef359985426090:FF=0:TM=1363790513:LM=1363790514:S=UO5PtdM9ETqX6Mm_; 
Set-Cookie: 
Server: gws
X-XSS-Protection: 1; mode=block
X-Frame-Options: SAMEORIGIN
Transfer-Encoding: chunked

8000
<!doctype html><html itemscope="itemscope" itemtype="http://schema.org/WebPage">
<head><meta content="Search the world's information,
Receiving complete. Closing socket...
$

Testing tcpserver.c and tcpclient2.c

Server side:

$ ./tcpserver
Setting up the structs...
Creating a socket...
Binding socket...
Listen()ing for connections...
Connection accepted. Using new socketfd : 4
Waiting to recieve data...
37 bytes recieved :
GET / HTTP/1.1
host: www.google.com


send()ing back a message...
Stopping server...
$

Client side (modify tcpclient.c to use IP 127.0.0.1 and port 5556):

$ ./tcpclient2
Setting up the structs...
Creating a socket...
Connect()ing...
send()ing message...
Waiting to recieve data...
10 bytes recieved :
thank you.#▒
Receiving complete. Closing socket...
$

Example 5 - Windows socket (almost implies C++)

Example 6 Get image using Qt

See Chapter 14. Foundation of Qt Development. The code is on https://github.com/arraytools/Qt/tree/master/FQD/Chapter14.

Example 7 Trip planner using Qt

See Chapter 15. Networking on C++ GUI Programming Qt 4. The code is on http://taichi.selfip.net:81/lang/c/qt-book/chap15/.

Basic(s)

Cheat sheet, Tutorial, Crash course

Books for C++

Books for C

C++11 for Ubuntu 12.04

http://askubuntu.com/questions/113291/how-do-i-install-gcc-4-7

sudo add-apt-repository ppa:ubuntu-toolchain-r/test
sudo apt-get update
sudo apt-get install gcc-4.7 g++-4.7

# Also, don't forget to update-alternatives, as suggested here
sudo update-alternatives --install /usr/bin/gcc gcc /usr/bin/gcc-4.6 60 --slave /usr/bin/g++ g++ /usr/bin/g++-4.6 
sudo update-alternatives --install /usr/bin/gcc gcc /usr/bin/gcc-4.7 40 --slave /usr/bin/g++ g++ /usr/bin/g++-4.7 
sudo update-alternatives --config gcc

When issuing the last command, it will ask what version of gcc to use.

Hello World

#include <iostream>
#include <string>

using namespace std;

int main()
{
	string name; 
	cout << "Enter your name: "; 
	cin >> name; 
	cout << "\nHello, " << name << "! It's nice to meet you!";
	return 0;
}

Note that following the keyword cout is the insertion operator '<<' and following the keyword cin is the extraction operator '>>'. Both are public member functions in iostream class.

Naming convension

Some Words about Standalone Application vs Web Application

  • Users can make use of the hardware power on my own machine
  • Users don't worry his/her data will be used by 3rd party
  • Users don't need to worry about potential network problem
  • Users don't need to worry his/her jobs need to be waited in queue
  • Maintainer don't need to worry the server can be hacked (purposely or incidentally). The server has to pass some security exam before it can be opened to public.

Header/Include guard, Preprocessor

#ifndef ... #define ... #endif. See wikipedia and cplusplus.com.

Basic Arithmetic

   double a = 10, b=3;
   cout << int (a/b) << endl;       // 3
   cout << int (a/b + .5) << endl;   // 3
   a= 11;
   cout << int (a/b) << endl;    // 3
   cout << int (a/b + .5) << endl;  // 4

Data Type Ranges

Increment ++ operator

Note that ++variable is slightly faster than its alternative variable++ since the alternative needs to make a copy of itself before returning the result. (see SeqAn tutorial here)

Create Utilities Function

Don't put them in a class; just make them non-member functions at namespace scope.

//utility.hpp
namespace utility
{
   void function1();
   void function2();
   void function3();

   template<typename T>
   void function4()
   {
      //function template definition should be here!
   }
}

//utility.cpp

#include "utility.hpp"
namespace utility
{
   void function1() 
   {
        //code
   }
   void function2() 
   {
        //code
   }
   void function3() 
   {
        //code
   }
}

Memory Management

MIP Open Course

On page 25 of the memory management lecture note, it mentions

  • If using int x; the allocation occurs on a region of memory called the stack.
  • If using new int; the allocation occurs on a region of memory called the heap.

Heap is outside the scope of a given function and must be explicitly cleaned up with free. When free is called, the memory is released back to system but the pointer is not set to be null. Therefore if malloc and free are used within a loop the pointer should be set to null afterwards, to allow a test on the pointer after the next malloc call. See issue 17 of The MagPi.

Access the memory allocation of variable out of scope of function will cause compilation warnings and may result in unexpected crashes.

include <stdio.h>
int* fun() {
  int i=0;
  return &i; /* Return the memory address of i. Do not do this! */
}

int main() {
  printf("%p\n",fun()); /* Print the memory address of i within fun() */
  return 0;
}

However, if a pointer is assigned the address of memory on the heap inside a function, then it can be accessed afterwards. After the function call, it is still necessary to call free to release the dynamically allocated memory.

#include <stdio.h>
#include <stdlib.h>
int* fun() {
  int *i=0;
  i=(int*)malloc(sizeof(int));
  return i;
}
int main() {
  int *p = 0;
  p = fun();
  printf("%p\n",p);
  free(p);
  return 0;
}

Heap vs Stack

When an object is needed within several different functions calls, it might be helpful to create it on the heap instead. The difference between creating an object on the stack and the heap is that objects on the stack are automatically cleaned up when they go out of scope, whereas objects on the heap stay in memory. To clean up an object on the heap, it has to be explicitly deleted. See The MagPi issue 23.

// Using stack
#include <iostream>
#include "Square. h"
using namespace std;
int main() {
  Square s; // Using the default constructor
  Square s2(3. 0, ' b' ); // Using the second constructor
  cout << " s.area()=" << s.area() << " , s.colour()=" << s.colour() << endl ;
  cout << " s2.area()=" << s2.area() << " , s2.colour()=" << s2.colour() << endl ;
  return 0;
}

// Using heap
#include <iostream>
#include "Square. h"
using namespace std;
int main() {
  Square *s = new Square(); // Using the default constructor
  Square *s2 = new Square(3. 0, ' b' ); // Using the second constructor
  cout << "s->area()=" << s->area() << " , s->colour()=" << s->colour() << endl ;
  cout << "s2->area()=" << s2->area() << " , s2->colour()=" << s2->colour() << endl ;
  delete s;
  delete s2;
return 0;
}

new, delete operators, pointers and dynamic memory

Use new/delete instead of malloc()/free() which depends on <cstdlib> in C++. For example, the following code was a modification from Listing 4.22 (p169) <delete.cpp> from the book 'C++ Primer Plus'.

The memory created by new operator is called heap or free store. Forgetting to use delete operator will cause a memory leak. This kind of storage is called dynamic storage which differs from automatic storage and static storage/stack.

#include <iostream>
#include <cstring> // strlen() 

using namespace std;

int main()
{
   char * pn = new char(strlen("/home") + strlen("/Downloads") + 1);
   strcpy(pn, "/home");
   cout << pn << endl;
   cout << strlen(pn) << endl;

   strcat(pn, "/Downloads");
   cout << pn << endl;
   cout << strlen(pn) << endl;
   delete [] pn;
   return 0;
}
  • new allows to allocate a variable length arrays while allocating arrays on the stack stack size must be constant. See p52 of MIT Open Course or p33 of TheMagPi issue 23.
  • new and delete should be used together. See p62 to p67 of the above MIT note.
  • Pay attention to the scope of variables. If a variable is declared within a parenthesis, it will evaporate once it exists the parenthesis. See p69 of the above MIT note for the problem and p75 for the solution.
  • See Chapter 4, 9, 12 of C++ Primer Plus. Or Chapter 14 of C++ Without Fear
  • When a program interacts with other programs in a GUI or network environment, it typically passes or receives pointers to objects.

scalar

int *pn = new int;  // dynamic
delete pn;

VS

int higgens;
int *pt = &higgens;  // not dynamic

array

int *pn = new int[10]; // OR   int *pn; pn = new int[10];
pn[1] =3;
delete[] pn;

int *parr[40]; // array of 40 pointers to int

function

# Listing 4.22.1 <delete.cpp> from C++ Prime Plus (5th ed)
char *name;
char *getname(); // prototype
name = getname();
delete [] name;
# 1. It is possible to use 'new' in a function and use 'delete' in the main function.
# 2. This memory is not controlled by scope. It means new & delete gives you more
#     power to control over how a program uses memory.

use new(delete) in a class constructor(destructor)

# Listing 12.4, 12.5 and p591-593 of C++ Primer Plus (5th ed)
String::String (const String & st)
{
  len = st.len;
  str = new char [len + 1];
  std::strcpy(str, st.str);
}
String::~String()
{
  delete [] str;
}

dereference

Fraction *pFrac = new Fraction(1, 2);
(*pFrac).get_num(); // OR pFrac->get_num();
(*pFrac).member;   //  pFrac->member;

combining dereference and increment in a single expression

auto pbeg = v.begin();
// print elements up to the first negative value
while (pbeg != v.end() && *pbeg >= 0)
    cout << *pbeg++ << endl; // print the current value and advance pbeg

The precedence of postfix increment is higher than that of the dereference operator, so *pbeg++ is equivalent to *(pbeg++) . The operand of * is the unincremented value of pbeg. Thus, the statement prints the element to which pbeg originally pointed and increments pbeg.

variable array size in new

int n;
cout << "How many elements?";
cint >> n;
int *p = new int[n];
....
delete [] p;

null pointer

There are several ways to create a null pointer.

int *p1 = nullptr; // C++11
int *p2 = 0;
int *p3 = NULL; // must #include cstdlib header which defines the preprocessor variable NULL as 0.

Modern C++ programs generally should avoid using NULL and use nullptr instead.

To test whether a pointer is valid, we can use either

if (p0 != nullptr)  // consider p0 valid

OR

if (p0)  // consider p0 valid; p0 is not zero.

dealing with problems with memory allocation

If the memory requested is not available, the new operator returns a null pointer.

You can test for this possibility and take the appropriate action.

int *p = new int[1000];
if (!p) {
cout << "Insufficient memory.";
exit(-1);
}

Test memory leak

The following C++ code was used to test a memory leak and also the capacity of memory. Note that when 'n' is declared as unsigned int, the maximum value can be 4,294,967,295=2^32-1 which corresponds to 32,767 MB (about 32 GB) in size for a double vector (assume 8 bytes). If I change the type of n to "unsigned long long", its range can go up to 18,446,744,073,709,551,615=2^64-1 or 137438953472 GB in size for a double vector.

Note that the program should be built in x64 instead of win32 version in Visual Studio if we like to test it on non-Windows-XP OS on Windows. Linux OS does not need to worry...

#include <iostream>

using namespace std;

void foo(unsigned long long n)
{
    double *ptr = new (std::nothrow) double[n];
    if (!ptr) {
        cout << "Failed to allocate double[n]" << endl;
    } else {
        ptr[1] = 1.0;
        ptr[n-1] = 2.0;
        cout << "double[n] is allocated successfully!" << endl;
    }
}

int main()
{
    cout << "The program is used to test memory leak. \n";
    cout << "Do not worry. It won't crash your computer.\n";
    cout << "The source code is available on https://gist.github.com/arraytools/6689581 \n" << endl;
    unsigned long long n;
    int testChoice;
    cout << "Enter your choice 1=main, 2=sub: ";
    cin >> testChoice;
    cout << "\nNext enter the array size (<= 18,446,744,073,709,551,615) : ";
    cout << "\nSome common scenarios";
    cout << "\n268,435,456-1 = 2GB";
    cout << "\n536,870,912-1 = 4GB";
    cout << "\n1,073,741,824-1 = 8GB";
    cout << "\n2,147,483,648-1 = 16GB";
    cout << "\n4,294,967,296-1 = 32GB";
    cout << "\n8,589,934,592-1 = 64GB";
    cout << "\n";
    cin >> n;
    if (testChoice == 1)
    {
        double *ptr = new (std::nothrow) double[n];
        if (!ptr)
        {
            cout << "Failed to allocate double[n]" << endl;
        } else {
            ptr[1] = 1.0;
            ptr[n-1] = 2.0;
            cout << "double[n] is allocated successfully!" << endl;
        }
    } else {
        foo(n);
    }
    return 0;
}

and my testing result on Windows XP with 2GB physical memory & 2GB virtual memory. The program is compiled into 32-bit console application. It is a different story when tested on 64-bit Windows 7.

Array Size Main Function
20,000,000 =150 MB OK OK
200,000,000=1.5 GB OK OK
2000,000,000=15 GB Not OK Not OK
500,000,000 =4 GB Not OK Not OK
250,000,000=1.91 GB OK OK
270,000,000=2.06 GB Not OK Not OK

this keyword

The this keyword is valid only inside a member function, where it denotes a pointer to the object on which the member function is operating. For example, inside Vec::operator=, the type of this is Vec*, because this is a pointer to the Vec object of which operator= is a member. For a binary operator, such as assignment, this is bound to the left-hand operand. Ordinarily, this is used when we need to refer to the object itself, as we do here both in the initial if test and in the return.

Communication between objects

See Pi magazine issue 24. The secret is to use this pointer to pass a pointer to the data member object. In header files,

// Child.h
#ifndef CHILD_H
#define CHILD_H
class Parent; // forward declaration to reduce precompile time
class Child {
....
}
#endif
// Parent.h
#ifndef PARENT_H
#define PARENT_H
class Child; // forward declaration to reduce precompile time
class Parent {
...
}
#endif

In C++ files,

// Child.cpp
Child::Child(Parent *parent):
  m_parent(parent) {
}

void Child::run() {
  cout << m_parent->x() << ", " << m_parent->y() << endl;
}

// Parent.cpp
Parent::Parent(unsigned int x, unsigned int y):
  m_child(0),
  m_x(x),
  m_y(y) {
  if (!m_child) m_child = new Child(this);
  m_child->run();
}

bad_alloc error

http://stackoverflow.com/questions/6833143/how-to-check-memory-allocation-failures-with-new-operator

In C++ there are 2 primary ways in which new allocates memory and each requires different error checking.

The standard new operator will throw a std::bad_alloc exception on failure and this can be handled like a normal exception

try {
  char* c = new char[100];
} catch (std::bad_alloc&) {
  // Handle error
}

Or alternative the nothrow version of new will simply return NULL on failure

char* c = new (std::nothrow) char[100];
if (!c) {
  // Handle error
}

Virtual functions

may be actual functions or merely placeholders for real functions that derived classes must provide. If you define a virtual function without a body, that means the derived class must provide it (it has no choice, and the program will not compile otherwise). Classes with such functions are called abstract classes, because they aren’t complete classes and are more a guideline for creating actual classes. (For example, an abstract class might state “you must create the Display() method.”) In C++, you can create a virtual function without a body by appending =0 after its signature (also known as a pure virtual function). See OOP Demystified Chapter 4.3: Run-time polymorphism.

C++ videos

Type casting

by declaiming explicitly like (float)5 or using suffix like 5f.

Global variables, header

http://stackoverflow.com/questions/9702053/how-to-declare-a-global-variable-in-c

1. Put extern int myvar in the header file 2. Put int myvar in the cpp file

C vs C++ with functions

In non-object programming, we use

function(x, parameter)

In C++ programming, we use

x->function(parameter) // if x is a pointer
x.function(parameter)   // if x is not a pointer

<cstdlib> vs <stdlib.h>

The first one is for C++ and the other is for C. See here.

Function Prototypes

For example,

void cheers(int);    // prototype: no return value
double cube(double x);   // prototype: returns a double
int main()
{
...
}

Functon prototyping is often hidden in the include files.

Argument Passing in Functions

References as a function argument seems more popular for numerical values and Pointers as a function argument seems popular for char arrays. It is introduced in C++ Primer Chapter 6.2.2.

The argument can be a scalar or an array. Also it be numerical values or character strings.

Pointers as a function argument

The following example is coming from http://www.nongnu.org/c-prog-book/online/x641.html. See also cplusplus.com tutorial about pointers.

#include <stdio.h>

int swap_ints(int *first_number, int *second_number);

int main()
{
  int a = 4, b = 7;
  printf("pre-swap values are: a == %d, b == %d\n", a, b);
  swap_ints(&a, &b);
  printf("post-swap values are: a == %d, b == %d\n", a, b);
  return 0;
}

int swap_ints(int *first_number, int *second_number)
{
  int temp;
  temp = *first_number;
  *first_number = *second_number;
  *second_number = temp;
  return 0;
}

References as a function argument

The new variable is called Reference Variables which is a name that acts as an alias. This is described in C++ Primer Plus Chapter 8. See also

Here we create a reference that looks and acts like a standard C++ variable except that it operates on the same data as the variable that it references.

The following example is modified from C++ pitalls.

int foo = 3;     // foo == 3
int &bar = foo;  // foo == 3. bar is of type int &. The ampersand & is NOT the address operator.
bar = 5;         // foo == 5
int * prats = &foo;  // prats is a pointer. *prats and bar can be used interchangeably with foo
                               // and use the expression &bar and prats interchangeably with &foo.                               

and the same concept of references is used when passing variables.

#include <iostream>
using namespace std;

void foo( int &i )
{
    i++;
}

int main()
{
    int bar = 5;   // bar == 5
    cout << bar << endl;
    foo( bar );    // bar == 6
    cout << bar << endl;
    foo( bar );    // bar == 7
    cout << bar << endl;
    return 0;
}

However, reference variable 1. it is necessary to initialize the reference when you declare it 2. a reference is rather like a const pointer; you have to initialize it when you create it and once a reference pledges its allegiance to a particular variable, it sticks to its pledge.

More examples from this post.

// OK case.
main: int i=6;
         chgInt(&i);
function:  void chgInt(int *p);
                   *p = 10 + *p;

// Not OK case.
main: char *name = "old";
          chgStr(name);
function: void chgStr(char *n);
              n = "new"; // This will make a new string, not changing the original one

// Correction
main: SAME
function: void chgStr(char* &n);
              n = "new";

In fact, the way of using char* &n in function argument is also used by Foundation of Qt Development List 1.1 and 1.2 where string is used instead of char*. Below is the code of List 1.1:

class MyClass
{
public:
    MyClass( const string& text );
    const string& text() const;
    void setText( const string& text );
    int getLengthOfText() const;
private:
    string m_text;
};

Comparison of Reference and Pointer

Reference (pass by reference) Pointer (pass by address)
Main
int a;
int & bar = a;
foo(a); 
// pass variable (not pass address of the variable)
// it 'can' be passing 'values' of variables; 
// determined by prototype of the function def.
int a;
int * bar = &a;
foo(&a); 
// pass address of variables
Function
foo(int &b) 
// b is an alias 
foo(int *b)
// b is a pointer to an int

rvalue reference, && and Move semantics

This is used to avoid 'copy' data. The situation happened in copy constructor or assignment. It is important especially for large data.

  • See Programming: Principal and Practice (2nd ed) 18.3.4. It is a new feature in C++11.
  • Google it.

Pass the (address) of the pointer (char array) as function argument

http://stackoverflow.com/questions/1217173/how-to-have-a-char-pointer-as-an-out-parameter-for-c-function.

#include <string.h>
#include <iostream>
using namespace std;

void SetName( char **pszStr )
{
    char* pTemp = new char[10];
    strcpy(pTemp,"Mark");
    *pszStr = pTemp; // assign the address of the pointer to this char pointer
}

void SetNameBetter(char *& pszStr )
{
    char* pTemp = new char[10];
    strcpy(pTemp,"MarkBetter");
    pszStr = pTemp; // this works because pxzStr *is* the pointer in main
}

void SetNameNotOK( char *pszStr )
{
    char* pTemp = new char[10];
    strcpy(pTemp,"Mark");
    pszStr = pTemp;
}

int main(void){
    
    char* pszName = NULL;
    // SetName( pszName );
    SetName( &pszName ); // pass the address of this pointer so it can change
    cout<<"Name - "<< pszName<<endl; // not *pszName

    SetNameBetter( pszName ); // pass the pointer into the function, using a reference
    cout<<"Name - "<< pszName<<endl; // not *pszName

    delete [] pszName;
    return 0;
}

Function and Arrays

(C++ Primer Ch 6.2.4 Array Parameters) Because arrays are passed as pointers, functions ordinarily don't know the size of the array they are given. There are three techniques used to manage pointer parameters

  1. using a marker to specify the extent of a character array (null character for C-style strings)
  2. using the standard library conventions (begin and end pointers) and
  3. explicitly passing a size parameter.

See Chapter 7 of C++ Primer Plus (5th ed).

# Version 1.
int sumarray(int arr[], int n)
{
  int total = 0;
  for(int i=0; i<n; i++) total += arr[i];
  return total;
}

# Version 2.
int sumarray(int * arr, int n)
{
  int total = 0;
  for(int i=0; i<n; i++) total += arr[i];
  // arr[i] is equivalent to *(arr + i)
  return total;
}

# Version 3a.
int sumarray(const int arr[], int n); //protect input array

# Version 3b.
int sumarray(const int *begin, const int *end)
{
  const int *pt;
  int total = 0;
  for(pt=begin; pt != end; pt++) total += *pt;
  return total;
}

Pointer and const

See Chapter 7 of C++ Primer Plus (5th ed).

int sloth = 3;
const int * ps = &sloth;  
// a pointer to const int, prevent using the pointer to change the pointed-to value

int * const finger = &sloth;  
// a const pointer to int, prevent from changing where the pointer points.

Functions and Two-Dimensional Arrays

int data[3][4] = {{1,2,3,4}, {5,6,7,8}, {9,10,11,12}}; 
// data is an array with 3 elements. 
int total = sum(ar, 3); 
// Since the first element of data is an array of 4 int values. so ar should be a pointer to array-of-four-int.
// What is the prototype?

int sum(int (*ar)[4], int size); 
// the parentheses are needed because '''int *ar[4]''' should declare 
// an array of 4 pointers to int.
OR
int sum(int ar[][4], int size);

int sum(int ar[][4], int size) 
{
  int total = 0;
  for(int r=0; r< size; r++)
    for(int c=0; c<4; c++)
      total += ar[r][c];
  return total;
}

Note that the parentheses around *ar are necessary:

int *matrix[10]:    // array of 10 pointers
int (*matrix)[10]:  // pointer to an array of ten ints

command line arguments

int main(int argc, char *argv[]) { ... } // argv is an array of pointers to C-style character strings.
int main(int argc, char **argv) { ... }  // argv points to a char *.
argv[0] = "prog";
argv[1] = "-i";
argv[2] = "inputfile";
argv[3] = "-o";
argv[4] = "outputfile";

Varying parameters

(C++11) initializer_list parameters.

void error_msg(initializer_list<string> i1)
{
  for (auto beg = i1.begin(); beg != i1.end(); ++beg)  cout << *beg << " ";
  // OR for (const auto &elem : i1)  cout << elem << " ";
  cout << endl;
}
...
if (expected != actual) {
  error_msg({"foo", expected, actual});
else
  error_msg({"foo", "Okay"});
}

Ellipsis parameters

Ellipsis parameters are in C++ to allow programs to interface to C code that uses a C library facility named varargs.

void foo(param_list, ...);
void foo(...);

Return a pointer to an array

See C++ Primer Ch 6.3.3.

A function cannot return an array. It can return a pointer or a reference to an array. However, the syntax used to define functions that return pointers or references to arrays can be intimidating.

(C++11) Trailing return type. Trailing returns can be defined for any function with complicated return types, such as pointers (or references) to arrays. A trailing return type follows the parameter list and is preceded by ->. To signal that the return follows the parameter list, we use auto where the return type ordinarily appears.

// fcn takes an int argument and returns a pointer to an array of 10 ints
auto func(int i) -> int(*)[10];


Alternativly, if we know the array(s) to which our function can return a pointer, we can use decltype to declare the return type. For example, the following function returns a pointer to one of two arrays, depending on the value of its parameter.

int odd[] = {1, 3, 5, 7, 9};
int even[] = {2, 4, 6, 8, 10};
// returns a pointer to an array of 5 int elements
decltype(odd) *arrptr(int i)
{
  return(i % 2) ? &odd : &even; // return a pointer to the array
}

Container class in C++ vs simple array in C

https://isocpp.org/wiki/faq/containers

String, string, string

http://stackoverflow.com/questions/11322200/unable-to-build-my-c-code-with-g-4-6-3

http://stackoverflow.com/questions/2258561/getting-the-length-of-an-array-using-strlen-in-g-compiler

  • C++ uses <cstring> for char*, strlen, strcpy ...
  • C uses <string.h> for char*
  • C++ uses <string> for string class

try { ... } catch { ... } for exception handling

try
{
    throw 20;
}
catch (int e)
{
    cout << "An exception occurred. Exception Nr. " << e << endl;
}

Second example.

   char *buf;
   try {
      buf = new char[512];
      if( buf == 0 )
         throw "Memory allocation failure!";
   }
   catch( char * str ) {
      cout << "Exception raised: " << str << '\n';
   }

Third example.

try {
    return grade(s);
} catch (domain_error) {
    return grade(s.midterm, s.final, 0);
}

Fourth example,

#include <iostream>
using namespace std;

int divide_numbers(int a, int b)
{
    if(b==0)
        throw 1;
    return a/b;
}

int main()
{
    int a, b;

    cout << "One: ";
    cin >> a;
    cout << "Two: ";
    cin >> b;

    try
    {
        cout << divide_numbers(a, b);
    }
    catch(int& code)
    {
        cout << "ERROR CODE: " << code;
    }
    catch(...)
    {
        cout << "An unknown error has occurred.";
    }
    //Continue doing whatever afterwards like normal

    return 0;
}

Fifth example,

#include <iostream>
using namespace std;

int main()
{
    try
    {
        int* myarray = new int[100000000000000];
        delete [] myarray;
    }
    catch(exception& e) //Takes a reference to an 'exception' object
    {
        cout << "Error allocating memory: " << e.what() << endl;
    }
}

map

The following example is from Chapter 7 of Accelerated C++. See also the example and a list of member types/functions from cplusplus.com. The interesting thing is the elements are ordered by their key at all times (see the example below).

#include <iostream>
#include <map>
#include <string>

using std::cin;
using std::cout;
using std::endl;
using std::map;
using std::string;

int main()
{
	string s;
	map<string, int> counters; // store each word and an associated counter

	// read the input, keeping track of each word and how often we see it
	while (cin >> s)
		++counters[s];

	// write the words and associated counts
	for (map<string, int>::const_iterator it = counters.begin();
	     it != counters.end(); ++it) {
		cout << it->first << "\t" << it->second << endl;
	}
	return 0;
}

Class

The fundamental ideas behind classes are data abstraction and encapsulation.

Data abstraction separate interface and implementation. The interface of a class consists of the operations that users of the class can execute. The implementation includes the class's data members, the bodies of the functions.

Encapsulation enforces the separation of a class' interface and implementation.

Class vs struct

The only difference between struct and class is the default access level.

Members in a class are private by default while members defined in struct are public.

Defining class member functions and member initializer list

See 9.4.4 of Programming: principles and practice.

class Date {
public:
    Date(int yy, int mm, int dd) 
        :y(yy), m(mm), d(dd)        
    {  // ...  }
    void add_day(int n)
    {  // ...  }
    int month() { return m; }
    // ...
private:
    int y, m, d;            // year, month, day
};

The :y(yy), m(mm), d(dd) notation is how we initialize members. It is called a member initializer list (See also this article in cplusplus.com. We could have written (not as good as the above way)

    Date(int yy, int mm, int dd) 
    {
        y = yy;
        m = mm;
        d = dd;
        // ...
    }

explicit keyword and implicit conversion to resolve the parameters to a function

http://stackoverflow.com/questions/121162/what-does-the-explicit-keyword-in-c-mean

Class constructor can take parameter via assignment

For example (see p6 & p76 of the [MIT lecture note)

class Integer {
  public:
  int val;
  Integer(int v) {
    val = v; cout << "constructor with arg " << v << endl;
  }
};
int main() {
  Integer i(3);
  Integer j = 5;
}

The output will be

constructor with arg 3 
constructor with arg 5

copy constructor and copy assignment (operator=)

  • See Chapter 11.3 of Accelerate C++
  • Cplusplus.com
  • See Chapter 18.3.1 and 18.3.2 of Programming: Principles and Practice (2nd ed).

copy constructor

MyClass(const MyClass& arg);

To use it,

MyClass v1(3);
MyClass v2 = v1; // OR MyClass v2 {v1};

copy assignment

MyClass& MyClass::operator=(const MyClass& a)

To use it,

MyClass v1(3);
v1.set(2, 2.2);
MyClass v2(4);
v2 = v1;

Polymorphism

A function's signature is its argument list. You can define two functions having the same name, provided that they have different signatures. This is called function polymorphism.

Function polymorphism is also called function overloading.

Template

C++ uses templates to enable generic programming techniques. The C++ Standard Library includes the Standard Template Library (STL) that provides a framework of templates for common data structures and algorithms.

There are two kinds of templates: function templates (e.g. algorithm library) and class templates (e.g. array, vector, list containers).

Function templates

http://www.cplusplus.com/doc/tutorial/templates/

// function template
#include <iostream>
using namespace std;

template <class T>
T GetMax (T a, T b) {
  T result;
  result = (a>b)? a : b;
  return (result);
}

int main () {
  int i=5, j=6, k;
  long l=10, m=5, n;
  k=GetMax<int>(i,j);
  n=GetMax<long>(l,m);
  cout << k << endl;
  cout << n << endl;
  return 0;
}

Another example is exchanging two variables. See Listing 8.11 <funtemp.cpp> in C++ Primer Plus (5th ed).

template <class Any>
void Swap(Any &a, Any &b)
{
 // References as Function Arguments
  Any temp;
  temp = a;
  a = b;
  b = temp;
}

int main()
{
  int i = 10, j= 20;
  Swap(i, j);
  cout << "New i, j = " << i << ", " << j << ".\n";
  double x = 24.5, y = 81.7;
  Swap(x, y);
  cout << "New x, y = " << x << ", " << y << ".\n";
}

Or Overloaded Templates. See Listing 8.12 <twotemps.cpp> in C++ Primer Plus (5th ed).

template <class Any>
void Swap(Any &a, Any &b)

template <class Any>
void Swap(Any *a, Any *b, int n);

int main()
{
  int i=10, j=20;
  Swap(i, j);

  int d1[8] = {0, 7, 0, 4, 1, 7, 7, 6};
  int d2[8] = {0, 6, 2 ,0, 1, 9, 6, 9};
  Swap(d1, d2, 8);
}

template <class Any>
void Swap(Any &a, Any &b)
{
  Any temp; temp=a; a=b; b=temp;
}

template <class Any>
void Swap(Any a[], Any b[], int n)
{
  Any temp;
  for (int i=0; i<n; i++) 
  {
    temp = a[i]; a[i] = b[i]; b[i]=temp;
  }
}

Class templates

http://www.cplusplus.com/doc/tutorial/templates/

// class templates
#include <iostream>
using namespace std;

template <class T>
class mypair {
    T a, b;
  public:
    mypair (T first, T second)
      {a=first; b=second;}
    T getmax ();
};

template <class T>
T mypair<T>::getmax ()
{
  T retval;
  retval = a>b? a : b;
  return retval;
}

int main () {
  mypair <int> myobject (100, 75);
  cout << myobject.getmax();
  return 0;
}

String, iterator and printing

The iterator is one of string's member types and begin() & end() are two string's member functions with return type of iterators.

Iterator is sort of pointers. You can print the value by using star (*) to deference, use +/- to move it and use != to compare different iterators. Moreover, we can use subset operator [] to get elements (eg iter[-1]).

// string::begin/end
#include <iostream>
#include <string>

int main ()
{
  std::string str ("Test string");
  for ( std::string::iterator it=str.begin(); it!=str.end(); ++it)
    std::cout << *it;
  std::cout << '\n';

  return 0;
}

snprintf() and sprintf()

See http://www.cplusplus.com/reference/cstdio/sprintf/

  • int snprintf(char *s, size_t n, const char * format, ...)
  • int sprintf(char *s, const char * format, ...)

Convert

Danger of implicit type conversion

Implicit type conversion (coercion) can result in unexpected results. See the following example.

#include <iostream>
#include <iomanip>
using namespace std;
int main()
{
  float myFloat;
  long int myInt = 123456789; 
  // long int  are 32-bit signed integers with a range -2147483648 to 2147483647.
  myFloat = myInt;
  cout << setprecision(9) << myFloat;  // 123456792
  return 0;
}

Implicit types conversion rules:

  • long int to float can cause wrong results
  • float to int removes the decimal part
  • double to float rounds digit of double
  • long int to int drops the encoded higher bits

Convert std::string to c-string/char *

See stackoverflow A/stackoverflow B and www.cplusplus.com and codeguru.com.

Convert char* to string

std::string has a constructor that takes a char*.

char *path = "Eggs on toast.";
std::string str = std::string(path);

Convert string to char*

http://stackoverflow.com/questions/12862739/convert-string-to-char

char * S = new char[R.length() + 1];
std::strcpy(S,R.c_str());

Convert an integer to character string

See www.cplusplus.com.

Convert a numerical number to char

Use

char mychar[256]="";
double number;
sprintf_s(mychar, "%0.2f", number);

This will save a number for example -7.035425 to -7.03 as characters.

C++ IO Streams

The first example is to read the input filename using command line argument (Accelerated C++ Chapter 10.5).

int main(int argc, char **argv)
{
    int fail_count = 0;
    // for each file in the input list
    for (int i = 1; i < argc; ++i) {
        ifstream in(argv[i]);
        // if it exists, write its contents, otherwise generate an error message
        if (in) {
            string s;
            while (getline(in, s))
                cout << s << endl;
        } else {
            cerr << "cannot open file " << argv[i] << endl;
            ++fail_count;
        }
    }
    return fail_count;
}

The 2nd example is to hard-code the input filename.

#include <fstream>
#include <string>

using std::endl;
using std::getline;
using std::ifstream;
using std::ofstream;
using std::string;

int main()
{
    ifstream infile("in");
    ofstream outfile("out");

    string s;

    while (getline(infile, s))
   	outfile << s << endl;

    infile.close();
    outfile.close();

    return 0;
}

No matching function - ifstream open()

See here. It Change to:

file.open(name.c_str());

Support for std::string argument was added in c++11.

Read from console

#include <iostream>
int main()
{
    vector<double> temps;         // temperatures
    double temp;
    while (cin >> temp)             // read 
        temps.push_back(temp);    // put into vector
    cout << "length of the vector is " << temps.size() << endl;
    for (const auto tmp : temps)
       cout << tmp << " ";
    cout << "\n";
}

Hit the 'Enter' key first and then use 'ctrl+d' to end the input.

$ g++ -std=c++11 foo.cpp
$ cat > input.txt
4 3 2 1 9.0
$ ./a.out < input.txt
length of the vector is 5
4 3 2 1 9 

Reading Files

Read a text file with one row only (using getline())

#include <iostream>
#include <fstream>
#include <string>
using namespace std;
ifstream fin;
fin.open("combobox.txt"); // assume the file uses tab as delimiter
if (fin.is_open() == false)
{
    cout << "Can't open file. Bye." << endl;
    exit(EXIT_FAILURE);
}
string item;
int count = 0;
getline(fin, item, '\t');
while (fin)
{
    ++count;
    cout << count << ": " << item << endl;
    getline(fin, item, '\t');
}
cout << "Done\n";
fin.close();

Read a text file with multiple rows (using stringstream and getline())

With the following examples, we can count the number of columns and number of rows of a text file.

#include <iostream>
#include <fstream>
#include <string>
#include <sstream> // stringstream
using namespace std;
ifstream fin;
fin.open("combobox.txt"); // step 1. open a file
if (fin.is_open() == false)
{
    cout << "Can't open file. Bye." << endl;
    exit(EXIT_FAILURE);
}
string line, item;
int ncol = 0, nrow = 0;
stringstream iss;
while(getline(fin, line)) {  // step 2. extract a line ("\n" is the default delimiter) from a (file) stream
    nrow++;                  //         accumulate row number.
    iss << line;             // step 3. insert a string to a stringstream  
    // Assume data is tab delimited
    // http://www.cplusplus.com/reference/string/string/getline/
    while (getline(iss, item, '\t')) { // step 4. extract a string from a stringstream
        if (nrow == 1 ) ++ncol;        //         accumulate column number. 
        cout << item << endl;
    }
    iss.clear();              // step 5.  clear the error state of the stream?
}
cout << "There are " << nrow << " rows and " << ncol " columns\n";
fin.close();

If we want to assign the column names to an array of string and elements to 2 2D string arrays, we need to determine the dimension and then declare the variables first.

col_names = new string[ncol];
for(int i=0;i<ncol;i++){
    getline(iss, col_names[i], '\t');
}
element = new string *[nrow];
stringstream iss;
string str;
for(int i=0;i<nrow;i++){
    getline(fin, line);
    iss << line;
    element[i] = new string[ncol];
    for(int j=0;j< ncol;j++){
        if(getline(ts3, str, '\t') )	{
	    element[i][j]=str;
        }else{
	    element[i][j]=string("");
	}
    }

reset position to the beginning of file

    ifstream orderfile;
    orderfile.open (fullpath);

    std::string line;
    long total_count=0;

    while (orderfile.good()){
        getline(orderfile, line);
        if (!line.empty()) total_count++;
    }

    orderfile.clear();
    orderfile.seekg(0,ios::beg);
    getline(orderfile, line);

Write a Function to Contain an Argument of Output Device (eg file, screen)

See Listing 8.8 <filefunc.cpp> in C++ Primer Plus (5th ed). It uses ostream & as the class of output device type in the function definition. For the main function, we can use objects of class ofstream or istream. Recall, ostream is a base class and ofstream is a derived class.

The program teaches 1. use reference (ostream &) as a function argument to refer to an ostream object such as cout and to an ofstream object. 2. how ostream formatting methods can be used for both types.

Count number of lines in a text file

int numLines = 0;
ifstream in("combobox.txt");
std::string unused;
while ( std::getline(in, unused) )
   ++numLines;
in.close();

Sorting only

#include <iostream>
#include <string>
#include <iterator>
#include <algorithm>

int main() {

   std::string obj[4] = {"fine", "ppoq", "tri", "get"};
   std::sort(obj, obj + 4);
   std::copy(obj, obj + 4, std::ostream_iterator<std::string>(std::cout, "\n"));
// And for vector
// #include <vector>
// std::vector<std::string> stringarray;
// std::sort(stringarray.begin(), stringarray.end());
}

and

#include <stdlib.h>
#include <string.h>

int compare_cstr(const void* c1, const void* c2) 
{ 
   return strcmp(*(const char**)(c1), *(const char**)(c2)); 
}

int main() {

   const char* obj[4] = {"fine", "ppoq", "tri", "get"};
   qsort(obj, 4, sizeof(obj[0]), compare_cstr);
   std::copy(obj, obj + 4, std::ostream_iterator<const char*>(std::cout, "\n"));
}

Note: The use of ostream_iterator<string>(cout, "\n") was explained & used in Accelerated C++ Chapter 8.3 (Input and output iterators) & 8.4. It was also explained in cplusplus.com.

Return permutation (R's order() function) using 3 approaches

Good example. This is using lambda from C++0x but it can be replaced with simple functor object.

#include <vector>
#include <algorithm>
#include <iostream>

template<class Vals>
void sortingPermutation(const Vals& values, std::vector<int>& v){
  int size = values.size(); 
  v.clear(); v.reserve(size);
  for(int i=0; i < size; ++i)
    v.push_back(i);

  std::sort(v.begin(), v.end(), [&values](int a, int b) -> bool { 
    return values[a] < values[b];
  });
}

int main()
{
    std::vector<double> values;
    values.push_back(24);
    values.push_back(55);
    values.push_back(22);
    values.push_back(1);

    std::vector<int> permutation;
    sortingPermutation(values, permutation);

    typedef std::vector<int>::const_iterator I;
    for (I p = permutation.begin(); p != permutation.end(); ++p)
        std::cout << *p << " ";
    std::cout << "\n";
}

This will return values 3, 2, 0, 1. In fact, the code is so general: if I add #include <string> and change double to std::string in the declaration of values, the code works for string data type.

Method 2. You can use std::sort to sort the list of pairs {(24, 0), (55, 2), (22, 0), (1, 1)}.

#include <vector>
#include <algorithm>
#include <utility>

typedef std::pair<double, int> Pair;

struct CmpPair
{
    bool operator()(const Pair& a, const Pair& b)
    { return a.first < b.first; }
};

void sortingPermutation(
    const std::vector<double>& values,
    std::vector<int>& permutation)
{
    std::vector<Pair> pairs;
    for (int i = 0; i < (int)values.size(); i++)
        pairs.push_back(Pair(values[i], i));

    std::sort(pairs.begin(), pairs.end(), CmpPair());

    typedef std::vector<Pair>::const_iterator I;
    for (I p = pairs.begin(); p != pairs.end(); ++p)
        permutation.push_back(p->second);
}

#include <iostream>

int main()
{
    std::vector<double> values;
    values.push_back(24);
    values.push_back(55);
    values.push_back(22);
    values.push_back(1);

    std::vector<int> permutation;
    sortingPermutation(values, permutation);

    typedef std::vector<int>::const_iterator I;
    for (I p = permutation.begin(); p != permutation.end(); ++p)
        std::cout << *p << " ";
    std::cout << "\n";
}

This will give the same result as above. In fact, the code is so general: if I add #include <string> and change double to std::string in the declaration of values, the code works for string data type. See also c version http://stackoverflow.com/questions/2804493/finding-unique-elements-in-an-string-array-in-c.

Method 3. Create a vector of ints 0..N and then sort that array with a comparison function that compares the corresponding elements of the vector you're trying to find the sorted permutation of. Something like:

#include <iostream>
#include <algorithm>
#include <vector>
 
template<class T> class sorter {
    const std::vector<T> &values;
public:
    sorter(const std::vector<T> &v) : values(v) {}
    bool operator()(int a, int b) { return values[a] < values[b]; }
};

template<class T> std::vector<int> order(const std::vector<T> &values)
{
    std::vector<int> rv(values.size());
    int idx = 0;
    for (std::vector<int>::iterator i = rv.begin(); i != rv.end(); i++)
        *i = idx++;
    std::sort(rv.begin(), rv.end(), sorter<T>(values));
    return rv;
} 
 
int main()
{
    std::vector<double> values;
    values.push_back(24);
    values.push_back(55);
    values.push_back(22);
    values.push_back(1);
 
    std::vector<int> permutation;
    permutation = order(values);
 
    typedef std::vector<int>::const_iterator I;
    for (I p = permutation.begin(); p != permutation.end(); ++p)
        std::cout << *p << " ";
    std::cout << "\n";
}

This also gives the same result. In fact, the code is so general: if I add #include <string> and change double to std::string in the declaration of values, the code works for string data type.

Infinity value

http://en.cppreference.com/w/cpp/types/numeric_limits/infinity

    double max = std::numeric_limits<double>::max();
    double inf = std::numeric_limits<double>::infinity();
    if(inf > max)
        std::cout << inf << " is greater than " << max << '\n';

Scope

Method 1: Created object cannot be used in other places.

Class aClass {
  aClass();
  void foo();
};
aClass::aClass() {
   bClass *obj = new bClass;
}
void aClass::foo() {
   obj->myfunction(); // Won't work!!
}

Method 2: Created object can be used within the class.

Class aClass {
  aClass();
  void foo();
};
aClass::aClass() {
   obj = new bClass;
}
void aClass::foo() {
   obj->myfunction();
}

(C++11) Lambda functions

A lambda function is essentially an anonymous function (a function without a name) that’s defined inline.

A simple example from an article from oracle.com.

#include <stdlib.h>
#include <stdio.h>
#include <algorithm>

int main()
{
  int a[10] = { 9, 8, 7, 6, 5, 4, 3, 2, 1, 0 };
  std::sort( a, &a[10], [](int x, int y){ return x < y; } );
  for(int i=0; i<10; i++) { printf("%i ", a[i]); }
  printf("\n");
  return 0;
}

C++11

To compile a code containing C++11 features (gcc 4.7 and up), using -std=c++11 or -std=c++0x option in g++

g++ -std=c++11 MYCODE.cc

STL vector vs C++ new

Basic STL vector

Vectors examples:

#include <vector>
using std::vector;

vector<int> ivec;             // ivec holds objects of type int
vector<Sales_item> Sales_vec; // holds Sales_items
vector<vector<string>> file;  // vector whose elements are vectors

# Initialize vectors
vector<int> ivec;             // initially empty
vector<int> ivec2(ivec);      // copy elements of ivec into ivec2
vector<int> ivec3 = ivec;     // copy elements of ivec into ivec3
vector<string> svec(ivec2);   // error: svec holds strings, not ints
vector<string> articles = {"a", "an", "the"};
vector<int> ivec(10, -1);       // ten int elements, each initialized to -1
vector<string> svec(10, "hi!"); // ten strings; each element is "hi!"
vector<int> ivec(10);    // ten elements, each initialized to 0
vector<string> svec(10); // ten elements, each an empty string
vector<int> v1(10);    // v1 has ten elements with value 0
vector<int> v2{10};    // v2 has one element with value 10
vector<int> v3(10, 1); // v3 has ten elements with value 1
vector<int> v4{10, 1}; // v4 has two elements with values 10 and 1

# Add an element to a vector
vector<int> v2;         // empty vector
for (int i = 0; i != 100; ++i)
    v2.push_back(i);    // append sequential integers to v2
                  // at end of loop v2 has 100 elements, values 0 . . . 99

# vector operators
# v.empty()
# v.size()
# v.push_back(t)
# v[n]
# v1 = v2
# v1 = {a, b, c}
# v1 == v2
# v1 != v2
# <, <=, >, >=
vector<int> v{1,2,3,4,5,6,7,8,9};
for (auto &i : v)     // for each element in v (note: i is a reference)
    i *= i;           // square the element value
for (auto i : v)      // for each element in v
    cout << i << " "; // print the elemen
vector<int> ivec;   // empty vector
for (decltype(ivec.size()) ix = 0; ix != 10; ++ix)
    ivec.push_back(ix);  // ok: adds a new element with value ix

Iterator examples:

auto b = v.begin(), e = v.end(); // begin and end are two iterator members. 
                                 // Return type is an iterator.
# standard container iterator operators
# *iter         returns a reference to the element denoted by the iterator iter
# iter->mem
# ++iter
# --iter
# iter1 == iter2
# iter1 != iter2
string s("some string");
if (s.begin() != s.end()) { // make sure s is not empty
    auto it = s.begin();    // it denotes the first character in s
    *it = toupper(*it);     // make that character uppercase
}
// changed the case of the first word in a string to use iterators instead
// process characters in s until we run out of characters or we hit a whitespace
for (auto it = s.begin(); it != s.end() && !isspace(*it); ++it)
    *it = toupper(*it); // capitalize the current character
# Combining Dereference and Member Access
(*it).empty() // dereferences it and calls the member empty on the resulting object
# Iterator arithmetic
# iter + n
# iter -n
# iter1 += n
 iter1 -= n
# iter1 - iter2
# >, >=, <, <=
// compute an iterator to the element closest to the midpoint of vi
auto mid = vi.begin() + vi.size() / 2;

convert a C-style array to a c++ vector

#include <vector>

int data[] = { 1, 2, 3, 4, 4, 3, 7, 8, 9, 10 };
std::vector<int> v(data, data+10);

WINVER

http://stackoverflow.com/questions/1439752/what-is-winver WINVER determines the minimum platform SDK required to build your application, which in turn will determine at compile time which routines are found by the headers.

You can use this to verify, at compile time, that your application will work on Windows 2000 (0x0500), for example, or on Windows XP (0x0501).

This was used in win32 disk imager program.

Colon

  • Single colon ":" was used in inheritance. For instance,
class fourwheeler {}
class car : public fourwheeler {}
  • Double colons was used to define/refer a class's function/method. Sometimes it can be used to resolve namespace problem. See here.
void MyClass::setText() {}

Period

What is the difference between the dot (.) operator and -> in C++?

foo->bar() is the same as (*foo).bar().

Member Access Operators: . and -> from msdn.microsoft.com

Protected vs private members

See here or C++ FAQ

Private members are only accessible within the class defining them.

Protected members are accessible in the class that defines them and in classes that inherit from that class.

Edit: Both are also accessible by friends of their class, and in the case of protected members, by friends of their derived classes.

Edit 2: Use whatever makes sense in the context of your problem. You should try to make members private whenever you can to reduce coupling and protect the implementation of the base class, but if that's not possible then use protected members. Check C++ FAQ Lite for a better understanding of the issue. This question about protected variables might also help.

'friend' class and function

alignment of pointers

https://stat.ethz.ch/pipermail/r-devel/2013-August/067314.html

Find sample quantiles (percentiles) using STL

STL algorithms defined in the <algorithm> and <numeric> libraries.

#include <iostream>
#include <numeric>      // accumulate
#include <algorithm>    // std::sort, std::nth_element
#include <vector>       // std::vector
using namespace std;

int main()
{
  int grades[]={89, 74, 89, 63, 100};
  
  size_t elements=sizeof(grades)/sizeof(grades[0]);
  double res= accumulate(grades, grades+ elements, 0)/double(elements);
  std::cout << "Raw data:";
  for(int i=0; i< elements; i++) std::cout << ' ' << grades[i];
  std::cout << '\n';
  cout << res << " is the mean" << endl << endl;
    
  // the iterator constructor can also be used to construct from arrays:  
  std::vector<int> myvector (grades, grades + elements);  
  std::sort(myvector.begin(), myvector.end());
  std::cout << "myvector contains:";
  for (std::vector<int>::iterator it = myvector.begin(); it!=myvector.end(); ++it)
    std::cout << ' ' << *it;
  std::cout << '\n';
  double median= *(myvector.begin()+myvector.size()/2); //89
  cout<<median << " is the median" << endl  << endl;

  std::vector<int> myvector2 (grades, grades + elements);  
  // None of the elements preceding nth (25-th perct) are greater than it, 
  // and none of the elements following it are less.
  nth_element(myvector2.begin(),
              myvector2.begin()+ (int)(myvector2.size()*0.25), 
              myvector2.end());
  std::cout << "myvector2 contains:";
  for (std::vector<int>::iterator it = myvector2.begin(); it!=myvector2.end(); ++it)
     std::cout << ' ' << *it;
  std::cout << '\n';            
  // This method will select one data as the percentile!  
  int p_25= *(myvector2.begin()+ (int)(myvector2.size()*.25)); 
  cout<<p_25<< " is the 25th percentile" << endl;

  return 0;
}

The output looks like (note in myvector2 data preceding 74 is less than 74 and data following 74 is greater than 74)

$ g++ example.cpp 
$ ./a.out
Raw data: 89 74 89 63 100
83 is the mean

myvector contains: 63 74 89 89 100
89 is the median

myvector2 contains: 63 74 89 89 100
74 is the 25th percentile
$

Calculate execulate time

http://stackoverflow.com/questions/5248915/execution-time-of-c-program. The following code is from Programming Arduino Next Steps. It took .04 seconds on Xeon W3690 @ 3.47GHz and 28 seconds on Arduino Uno.

#include <stdio.h>
#include <time.h>  

main()
{
  printf("\nStarting Test\n");
  time_t startTime = clock();
  
  // test code here
  long  i = 0;
  long j = 0;
  for (i = 0; i < 20000000; i ++)
  {
    j = i + i * 10;
    if (j > 10) j = 0;
  }
  // end of test code
  time_t endTime = clock();
  
  printf("%ld\n", j); // prevent loop being optimized out
  printf("Finished Test\n");
  double timeSpent = (double)(endTime - startTime) / CLOCKS_PER_SEC;
  printf("seconds taken: %f\n", timeSpent); 
  return 0;
}

execute a command line command from a C++ program

Use system() function.

#include <stdlib.h>

int main() {
  system("cp ~/Downloads/testInt.cpp ~/Downloads/testInt2.cpp");
}

Executing a shell command in c/c++ and getting its output?

FILE* pipe = popen("your shell command here", "r");
if (!pipe)
{
cerr<<"popen error"<<endl;
}    
char buffer[128];
    while(!feof(pipe)) 
     {
      if(fgets(buffer, 128, pipe) != NULL){}
     }
pclose(pipe);
buffer[strlen(buffer)-1] = '\0';

C++ standard libraries

Containers

array

deque

list

map

queue

set

stack

unordered_map

unordered_set

vector

algorithms

all_of/any_of/none_of/for_each

count/count_if/find/find_if/mismatch/search/search_n

Example of returning the index of a searched element in a vector

#include <algorithm>
#include <iostream>
#include <vector>
// #include <iterator>
int main()
{
    int data[] = { 1, 2, 3, 4, 4, 3, 7, 8, 9, 10 };
    std::vector<int> v(data, data+10);
    std::vector<int> indexResult;

    std::cout << "Original data is ";
    for(std::vector<int>::iterator it=v.begin(); it != v.end(); ++it)
      std::cout << *it << " ";
    std::cout << '\n';

    for(std::vector<int>::iterator it=v.begin(); it != v.end(); ++it) {
      // std::cout << "Begin shift= " << std::distance(v.begin(), it) << ", ";
      std::cout << "Current data is ";
      for(std::vector<int>::iterator it2=it; it2 != v.end(); ++it2) std::cout << *it2 << " ";
      std::cout << ", ";

      auto p = std::find(it, v.end(), 3);
      if (p != std::end(v))
        std::cout << "element 3 was found in v at position (starting from 0): " << std::distance(it, p) $
      else
        std::cout << "element 3 was not found in Data\n";
    }  
}

And the output

$ g++ -std=c++11 count.cpp; ./a.out
Original data is 1 2 3 4 4 3 7 8 9 10 
Current data is 1 2 3 4 4 3 7 8 9 10 , element 3 was found in v at position (starting from 0): 2
Current data is 2 3 4 4 3 7 8 9 10 , element 3 was found in v at position (starting from 0): 1
Current data is 3 4 4 3 7 8 9 10 , element 3 was found in v at position (starting from 0): 0
Current data is 4 4 3 7 8 9 10 , element 3 was found in v at position (starting from 0): 2
Current data is 4 3 7 8 9 10 , element 3 was found in v at position (starting from 0): 1
Current data is 3 7 8 9 10 , element 3 was found in v at position (starting from 0): 0
Current data is 7 8 9 10 , element 3 was not found in Data
Current data is 8 9 10 , element 3 was not found in Data
Current data is 9 10 , element 3 was not found in Data
Current data is 10 , element 3 was not found in Data

Note that the same idea cannot be applied to strings.

#include <algorithm>
#include <iostream>
#include <vector>
#include <string>
// #include <iterator>
int main()
{
    std::vector<std::string> v = {"x1", "yx12", "x3", "x4"};
    std::vector<int> indexResult;

    std::cout << "Search position (starting from 0)" << std::endl;
    std::cout << "Original data is ";
    for(auto it : v)
      std::cout << it << " ";
    std::cout << '\n';

    for(std::vector<std::string>::iterator it=v.begin(); it != v.end(); ++it) {
      std::cout << "Current data is ";
      for(std::vector<std::string>::iterator it2=it; it2 != v.end(); ++it2) 
          std::cout << *it2 << " ";
      std::cout << ", ";
      auto p = std::find(it, v.end(), "x1");
      if (p != std::end(v))
        std::cout << "element x1 was found in v at position: " << 
                     std::distance(it, p) << std::endl;
      else
        std::cout << "element x1 was not found in Data\n";
    }
}

After compiling & running it, we will see the find() function cannot pick up the 2nd case.

$ g++ -std=c++11 count.cpp; ./a.out
Search position (starting from 0)
Original data is x1 yx12 x3 x4 
Current data is x1 yx12 x3 x4 , element x1 was found in v at position: 0
Current data is yx12 x3 x4 , element x1 was not found in Data
Current data is x3 x4 , element x1 was not found in Data
Current data is x4 , element x1 was not found in Data

copy/copy_if/swap/swap_ranges/remove/remove_if/fill/replace/replace_if/shuffle/unique

sort/stable_sort/is_sorted/partial_sort

merge/set_difference/set_intersection/set_union

max/max_element/min/min_element/next_permutation

accumulate/inner_product/partial_sum

iterator

Using iterators

auto b=v.begin(), e=v.end();

string s("some string");
for (auto it = s.begin(); it != s.end() && !isspace(*it); ++it) {
    *it = toupper(*it);      // make that char uppercase
}

Iterator types

vector<int>::iterator it;  // it can read and write vector<int> elements
string::iterator it2;      // it2 can read and write characters in a string
vector<int>::const_iterator it3; // it3 can read but not write elements
string::const_iterator it4;      // it4 can read but not write characters

vector<int> v;
const vector<int> cv;
auto it1 = v.begin();  // it1 has type vector<int>::iterator
auto it2 = cv.begin(); // it2 has type vector<int>::const_iterator
auto it3 = v.cbegin(); // it3 has type vector<int>::const_iterator

Assuming it is an iterator into this vector, we can check whether the string that it denotes is empty as follows:

(*it).empty()  # *it.empty() will gives an error

Print each element of a vector of strings.

vector<string> text;
for (auto it = text.cbegin(); it != text.cend() && !it->empty(); ++it)
    cout << *it << endl;

numeric

regex (C++11)

regex_match() function

#include <algorithm>
#include <iostream>
#include <string>
#include <regex>
int main()
{
    if (std::regex_match("yx12", std::regex("(.*x1.*)")))
        std::cout << "string yx12 contains x1.\n";
    if (std::regex_match("yx12", std::regex("(.*x2.*)")))
        std::cout << "string yx12 contains x2.\n";
    if (std::regex_match("yx12", std::regex("(.*yx1.*)")))
        std::cout << "string yx12 contains yx1.\n";
}

See this post to match a string that does not contain some string.

string

utilities

Function objects

pair

Debugging

valgrind

http://valgrind.org/docs/manual/quick-start.html

For example,

valgrind --leak-check=yes build-Qheatmap-Desktop_Qt_4_8_5-Debug/Qheatmap /home/brb/Qt/example/BRCACC/

gdb/DDD

For example,

g++ -Wall -g -o XXX.o -c XXX.cpp
g++ -o XXX.exe XXX.o -lstdc++
ddd XXX.exe

Qt Creator

A screenshot based on Qt Creator 3.3 and Qt 5.4.

QtCreatorDebug.png

Tools

GCC

Introduction to GCC

Show all libraries used (dynamically linked) by an executable program

Use ldd (list dynamic dependencies) command.

debian@beaglebone:~$ ldd /usr/bin/netsurf
	libjpeg.so.8 => /usr/lib/arm-linux-gnueabihf/libjpeg.so.8 (0xb6f1c000)
	libz.so.1 => /lib/arm-linux-gnueabihf/libz.so.1 (0xb6f02000)
	libxml2.so.2 => /usr/lib/arm-linux-gnueabihf/libxml2.so.2 (0xb6e22000)
        ....
	libcurl.so.4 => /usr/lib/arm-linux-gnueabihf/libcurl.so.4 (0xb6dd7000)
	libtasn1.so.3 => /usr/lib/arm-linux-gnueabihf/libtasn1.so.3 (0xb5d5b000)
	libp11-kit.so.0 => /usr/lib/arm-linux-gnueabihf/libp11-kit.so.0 (0xb5d47000)

Linking with external libraries

$ gcc -Wall calc.c /usr/lib/libm.a -o calc
$ gcc -Wall calc.c -lm -o calc

To specify the library path, we can use "-L", and/or "-Wl,-rpath" in gcc/g++.

When shared libraries are present in nondefault directories, the option "-Wl,-rpath" is needed in linker options. See here. If we don't specify "-Wl,-rpath" in linker options, we need to define "LD_LIBRARY_PATH" environment variable. See an example in RInside.

Header files/Include path

The list of directories for header files is often referred to as the include path, and the list of directories for libraries as the library search path or link path.

When additional libraries are installed in other directories it is necessary to extend the search paths, in order for the libraries to be found. The compiler options -I and -L add new directories to the beginning of the include path and library search path respectively.

$ gcc -Wall -I/opt/gdbm-1.8.3/include -L/opt/gdbm-1.8.3/lib dbmain.c -lgdbm



Environment variables

We can use some environment variables to replace -I and -L flags. See some basic introduction on

Header files (CPATH, C_INCLUDE_PATH, CPLUS_INCLUDE_PATH, OBJC_INCLUDE_PATH)

$ CPLUS_INCLUDE_PATH=/opt/gdbm-1.8.3/include 
$ export CPLUS_INCLUDE_PATH

Library files during link time

$ LIBRARY_PATH=/opt/gdbm-1.8.3/lib
$ export LIBRARY_PATH

Library files during run time (needed only for dynamic libraries). On Windows platform, the PATH variable will be used.

$ export LD_LIBRARY_PATH=/opt/gdbm-1.8.3/lib

Check environment variables

echo $SHELL
echo $PATH
echo $LIBRARY_PATH
env

Makefile

This is an example of <Makefile>. The source code files are Parent.cpp, Parent.h, Child.cpp, Child.h, and main.cpp. Remember the indents should be a single tab.

CC=g++
TARGET=pc
OBJECTS=main.o Parent.o Child.o

$(TARGET): $(OBJECTS)
	@echo "** Linking Executable"
	$(CC) $(OBJECTS) -o $(TARGET)
	
clean:
	@rm -f *.o *~
		
veryclean: clean
	@rm -f $(TARGET)
	
%.o: %.cpp
	@echo "** Compiling C++ Source"
	$(CC) -c $(INCFLAGS) $<

minimal make

http://kbroman.org/minimal_make/

cmake

On Windows, it will be installed on C:\Program Files (x86)\Cmake 2.8 folder. By default, it is not added to system PATH. The 'Cmake' program will ask for source, binary folders and the Compiler option. After clicking 'configure' and 'generate' buttons, it will create VS solution file and we can double click the solution file to open the project in Visual Studio. In Visual Studio, we can just build the solution (Ctrl + Shift + B). When we want to debug the code, we should 1. right click on project and select property. Change the working directory to the source code (note that .exe file will be generated there). 2. Set the project as the starting project.

Autotools

http://stackoverflow.com/questions/2531827/what-are-makefile-am-and-makefile-in

Makefile.am is a programmer-defined file and is used by automake to generate the Makefile.in file. The ./configure script typically seen in source tarballs will use the Makefile.in to generate a Makefile.

The ./configure script itself is generated from a programmer-defined file named either configure.ac or configure.in , I prefer .ac (for AutoConf) since it differentiates it from the generated Makefile.in files and that way I can have rules such as make dist-clean which rm -f *.in . Since it is a generated file it is not typically stored in a revision system such as SVN or CVS, rather the .ac file would be.

Read more on GNU build system/Autotools. Read about make and Makefile first, then learn about automake, autoconf, libtool, etc.

C++ Libraries

Wt

C++ library for developing web applications

SeqAn

C++ for sequencing data. On Windows OS, it requires Python 2 and CMake in addition to VS.

Boost

Boost is a set of C++ libraries for numerical computation that provide support for tasks and structures such as linear algebra, pseudorandom number generation, multithreading, image processing, regular expressions, and unit testing. Release 1.52 contains over eighty individual libraries.

The R library BH provides template use in R. See http://gallery.rcpp.org/articles/using-boost-with-bh/ for a gallery.

Boost contains a lot of libraries.

brb@brb-VirtualBox:~/Downloads/boost_1_56_0$ ./bootstrap.sh --show-libraries
Building Boost.Build engine with toolset gcc... tools/build/src/engine/bin.linuxx86/b2

The following Boost libraries have portions that require a separate build
and installation step. Any library not listed here can be used by including
the headers only.

The Boost libraries requiring separate building and installation are:
    - atomic
    - chrono
    - container
    - context
    - coroutine
    - date_time
    - exception
    - filesystem
    - graph
    - graph_parallel
    - iostreams
    - locale
    - log
    - math
    - mpi
    - program_options
    - python
    - random
    - regex
    - serialization
    - signals
    - system
    - test
    - thread
    - timer
    - wave

build under Ubuntu OS

To install via apt-get is 'sudo apt-get install libboost-all-dev'. The software center shows the version is 1.48 (kind of old) on Ubuntu 12.04 and 1.54 on Ubuntu 14.04.

We can install it by downloading its source code and build it by ourselves. See this post on ubuntuforums.org.

./bootstrap.sh 
./b2 install

At the end of buliding, it will show the paths to header files (sometimes it goes to /usr/local/include) and the libraries themselves (sometimes it is /usr/local/lib). If we use apt-get to install boost, the header files go to /usr/include and the libraries files libboost*.a and libboost*.so go to /usr/lib.

The Boost C++ Libraries were successfully built!

The following directory should be added to compiler include paths:

    /home/brb/Downloads/boost_1_55_0

The following directory should be added to linker library paths:

    /home/brb/Downloads/boost_1_55_0/stage/lib

The source directory contains <index.html> file. It tells us how to run a simple test program. For example, the following example test headers-only libraries and require no separately-compiled library binaries or special treatment when linking.

g++ -I /home/brb/Downloads/boost_1_55_0 example.cpp -o example
echo 1 2 3 | ./example

To link to boost binary libraries, we can do

$ g++ -I /home/brb/Downloads/boost_1_55_0 example.cpp -o example2 \
   -L~/Downloads/boost_1_55_0/stage/lib/ -lboost_regex
$ ./example2 < jayne.txt
Will Success Spoil Rock Hunter?

Note that under boost_1_55_0/stage/lib directory, both static and dynamic libraries are available for boost_regex. My system picks the static library. If we want to link to dynamic library, we can

$ g++ -I /home/brb/Downloads/boost_1_55_0/ example2.cpp -o example2 \
    ~/Downloads/boost_1_55_0/stage/lib/libboost_regex.so
$ ./example2 < jayne.txt
./example2: error while loading shared libraries: libboost_regex.so.1.55.0: cannot open shared object file:
 No such file or directory
$ export LD_LIBRARY_PATH=/home/brb/Downloads/boost_1_55_0/stage/lib

Here we see the purpose of specifying the environment variable LD_LIBRARY_PATH.

To try an example (Math and numerics > math/statistical distributions > Calculating confidence intervals on the mean with the Students-t distribution), we can compile and generate the executable file by (no need to link to library in this case)

g++ -I /home/brb/Downloads/boost_1_55_0/ \
    ~/Downloads/boost_1_55_0/libs/math/example/students_t_single_sample.cpp 

If we like the headers and libraries automatically available in linux environment without using -I and -L parameters in g++, we can run

./bootstrap.sh --prefix=/usr/local
sudo ./b2 install

Running ./b2 took about 1 hours on my single core VM. At the end, it will create /usr/local/include/boost subdirectory and a bunch of libboost*.a and libboost*.so will be created under /usr/local/lib directory.

Check BOOST version

  • On Ubuntu, use ' tail /usr/include/boost/version.hpp'
  • IN boost C++ code, use boost version macro 'BOOST_LIB_VERSION'
#include <boost/version.hpp>
#include <iostream>

using namespace std;

int main()
{
    cout << "Boost version: " << BOOST_LIB_VERSION << endl;
    return 0;
}

Build boost from source on Windows OS

MinGW

boostrap --with-toolset=gcc
.\b2 --build-type=complete toolset=gcc link=shared runtime-link=shared install 

use of statistical distributions in Boost

See an article in www.quantnet.com

For each of distribution functions, we can compute cdf, quantile, pdf, mean, mode, median, variance, etc.

To compute t(alpha/2, df), we can

#include <boost/math/distributions/students_t.hpp>
#include <iostream>

using namespace std;
using boost::math::students_t;

int main()
{
  int df = 195;
  double alpha = .1;
  students_t dist(df);
  double T = quantile(complement(dist, alpha / 2));
  cout << T << endl;
  return 0;
}

We can compile the code and get the result (check by using R statement qt(.95, 195)).

$ g++ example.cpp; ./a.out
1.65271

PS For some reason, I don't need to worry about environment variable.

Googling shows we can use g++ --print-search-dirs to show LIBRARY search paths and adding -v to show include search paths to g++ command.

Boost thread example

http://ashishgrover.com/boost-multi-threadingfor-c/

#include <boost/thread.hpp>
void readerApi()
{
  for (int i=0; i < 10; i++) {
    usleep(400);
    std::cout << "readerApi: " << i
              << std::endl;
  }
}
void writerApi()
{
  for (int i=0; i < 10; i++) {
    std::cout << "writerApi: " << i
              << std::endl;
    usleep(400);
  }
}

int main()
{
  boost::thread readerThread(readerApi);
  boost::thread writerThread(writerApi);

  readerThread.join();
  writerThread.join();
}

Then compile the code by

$ g++ -o b1 boost1.cpp -lboost_thread

Before you can run it, specify LD_LIBRARY_PATH env variable if you build boost library by yourself.

$ export LD_LIBRARY_PATH=/usr/local/lib:$LD_LIBRARY_PATH
$ ./b1
writerApi: 0
readerApi: 0
writerApi: 1
readerApi: 1
writerApi: 2
readerApi: 2
writerApi: 3
writerApi: 4
readerApi: 3
writerApi: 5
readerApi: 4
writerApi: 6
readerApi: 5
writerApi: 7
readerApi: 6
writerApi: 8
readerApi: 7
writerApi: 9
readerApi: 8
readerApi: 9

To make changes permanent in Ubuntu you have to add a new configuration file for ldconfig:

sudo nano /etc/ld.so.conf.d/libboost.conf

(my editor of choice is nano, and the name of the file itself doesn’t matter) Add the library path to that file, i.e. /usr/local/lib/. Save the file, quit and reload your configuration by calling

sudo ldconfig

Note that your LD_LIBRARY_PATH won’t change, but your program will now run!

Another working example is from http://www.codeproject.com/Articles/279053/How-to-get-started-using-Boost-threads

#include <iostream>  
#include <boost/thread.hpp>   
#include <boost/date_time.hpp>       
      
void workerFunc()  
{  
    boost::posix_time::seconds workTime(3);          
    std::cout << "Worker: running" << std::endl;    
      
    // Pretend to do something useful... 
    boost::this_thread::sleep(workTime);          
    std::cout << "Worker: finished" << std::endl;  
}    
      
int main(int argc, char* argv[])  
{  
    std::cout << "main: startup" << std::endl;          
    boost::thread workerThread(workerFunc);    
      
    std::cout << "main: waiting for thread" << std::endl;          
    workerThread.join();    
      
    std::cout << "main: done" << std::endl;          
    return 0;  
}

It can be built by g++ test.cpp -lboost_thread -lboost_system.

Another excellent example is from Jeff Benshetler. Check out this page http://advancedcplusplus.com/5min-threads/ (I cannot build it successfully).

Books

Boost and Rcpp package

Boost.python

http://edyfox.codecarver.org/html/boost_python.html

Rcpp was inspired by Boost.python. See the Rcpp module vignette.

The Scythe Statistical Library

Scythe: An Open Source C++ Library for Statistical Computation from J of Stat Software.

Armadillo

Armadillo is a C++ linear algebra library (matrix maths) aiming towards a good balance between speed and ease of use.

GNU Scientific Library

BLAS, LAPACK

IMSL

Numerical Recipes

Some C++ Projects

Approximate nearest neighbor search

The R wrap is here

NGS++

A programming library in C++11 specialized in manipulating both next-generation sequencing (NGS) datasets and genomic information files. See the paper.

LIBSVM

Tophat

It also requires the packages

Parana2

It also depends on a few other tools.

  • Bio++ - a set of C++ libraries for Bioinformatics, including sequence analysis, phylogenetics, molecular evolution and population genetics.
  • Boost
  • GMP - The GNU Multiple Precision Arithmetic Library
  • MPFR - C library for multiple-precision floating-point computations with correct rounding.
  • pugixml - light-weight C++ XML processing library.

Short read alignment with populations of genomes

https://github.com/viq854/bwbble

Janus-comprehensive tool investigating the two faces of transcription

It depends on bamtools.

RNA-Pareto

Interactive Analysis of Pareto-optimal RNA Sequence-Structure Alignments

The software is written in Java 6 (graphical user interface) and C++ (dynamic programming algorithms). To run, a Java Runtime Environment, version ≥1.6.0 is required. It is well tested with GCC 4.6.

ANGSD

Analysis of Next Generation Sequencing Data

Open MS

http://open-ms.sourceforge.net/. It used external libraries such as: (i) Qt, which provides visualization and database support; (ii) Xerces for XML file parsing; (iii) libSVM, for machine learning algorithms; and (iv) the GNU Scientific Library (GSL), used for mathematical and statistical analysis. One of the strong points of OpenMS is a complete set of examples to extend and use the libraries, the TOPP (The OpenMS Proteomics Pipeline) and TOPPView tutorials describe in detail the OpenMS.

GUI Programming

Windows Programming

Resource

Difference between Win32 project and CLR (common language runtime) project

See here.

A Win32 project is used if you want to end up with a DLL or a Win32 application usually using the bare WinAPI. A CLR project is used to create C++/CLI project, i.e. to use C++/CLI to target the .NET platform.

The main difference between projects is what Visual Studio comes up with in terms of pre-created files. A windowed Win32 application for example (what you get when you choose Win32 project, but not a DLL) is created with a file for resources (menus, acceleators, icons etc.) and some default code to create and register a window class and to instantiate this window.

Quoted from http://en.wikipedia.org/wiki/Common_Language_Runtime

The Common Language Runtime (CLR) is the virtual machine component of Microsoft's .NET framework and is responsible for managing the execution of .NET programs. In a process known as Just-in-time compilation, the compiled code is converted into machine instructions that, in turn, are executed by the computer's CPU. The CLR provides additional services including memory management, type safety and exception handling. All programs written for the .NET framework, regardless of programming language, are executed by the CLR. It provides exception handling, garbage collection and thread management. CLR is common to all versions of the .NET framework.

The CLR is Microsoft's implementation of the Common Language Infrastructure (CLI) standard.

Difference between Win32, MFC and .NET

http://stackoverflow.com/questions/821676/how-do-i-decide-whether-to-use-atl-mfc-win32-or-clr-for-a-new-c-project

Using the CLR will provide you with the most expressive set of libraries (the entire .NET framework), at the cost of restricting your executable to requiring the .NET framework to be installed at runtime, as well as limiting you to the Windows platform (however, all 4 listed technologies are windows only, so the platform limitation is probably the least troublesome).

However, CLR requires you to use the C++/CLI extensions to the C++ language, so you'll, in essense, need to learn some extra language features in order to use this. Doing so gives you many "extras," such as access to the .net libraries, full garbage collection, etc.

Using Win32 directly provides the smallest executables, with the fewest dependencies, but is more work to write. You have the least amount of helper libraries, so you're writing more of the code.

Win32 is the raw, bare-metal way of doing it. It's tedious, difficult to use, and has alot of small details you need to remember otherwise things will fail in relatively mysterious ways.

MFC builds upon Win32 to provide you an object oriented way of building your application. It's not a replacement for Win32, but rather an enhancement - it does alot of the hard work for you.

System.Windows.Forms (which is what I assume you meant by CLR) is completely different, but has large similarities to MFC from its basic structure. It's by far the easiest to use, but requires the .NET framework, which may or may not be a hindrance in your case.

Why not MFC

http://win32-framework.sourceforge.net/explanation.htm The website also provides an alternative software called Win32++ to replace MFC. It also provides useful links for C++ compilers, tools, tutorial and references.

Qt

wxwidgets

Some projects:

Simple OpenGL GUI

As mentioned in http://www.oppi.uef.fi/bioinformatics/forg3d/downloads.php

OpenGL Programming on Windows

We need to include

#include <gl/gl.h>
#include <gl/glu.h>

And go to project's link properties and enter <opengl32.lib> & <glu32.lib>. Check the directory C:\Program Files (x86)\Microsoft SDKs\Windows\v7.0A\Include and C:\Program Files (x86)\Microsoft SDKs\Windows\v7.0A\lib

header files are: gl\gl.h and glu.h libraries are: openGL32.lib and GLU32.lib

x64 libs can be found in C:\Program Files (x86)\Microsoft SDKs\Windows\v7.0A\lib\x64 you can put freeglut lib and header files to those locations to use freeglut with visual studio 2010 when you copy freeglut DLLs to C:\Windows\System32 don’t copy 64 bit DLL to syswow64 this gives a freaky error 0xc000007b when running code. Don’t know what it mean, but if you have freeglut only in system32 you going to be fine.

Resource

Example 1

http://openglbook.com/setting-up-opengl-glew-and-freeglut-in-visual-c/

  1. Download freeglut (freeglut-MSVC-2.8.0-1.mp.zip) & glew (glew-1.9.0-win32.zip)
  2. Copy files include and lib to appropriate location
  3. Copy freeglut.dll to the Project's Release or Debug folder

I don't need Step 5 (Compiler) and Step 6 (Linker).

I keep a copy of the instruction in Evernote.

Example 2

Teapot and Glut shapes (WireTeapot, SolidTeapot, SolidCube & SolidSphere) from http://openglsamples.sourceforge.net/

Example 3 (no Glut, Windows OS only)

http://www.nullterminator.net/opengl32.html

Examples from opengl.org

http://www.opengl.org/sdk/docs/tutorials/

Example of American Flag

http://www.youtube.com/watch?v=9xjBlde4Cew

Computer Systems

MIPS Instruction Set