<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html lang="en" xml:lang="en" xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="content-type" content="text/html;charset=UTF-8"/> <link rel="icon" type="image/gif" href="favicon.gif"/> <link rel="apple-touch-icon" sizes="120x120" href="touch-icon-iphone-retina.png" /> <link rel="apple-touch-icon" sizes="152x152" href="touch-icon-ipad-retina.png" /> <title>Object-Oriented C Style Languages: C++, Objective-C, Java, C# - Hyperpolyglot</title> <style type="text/css" id="internal-style"> @import url(hyperpolyglot.css); </style> <meta http-equiv="content-type" content="text/html;charset=UTF-8"/> <meta http-equiv="content-language" content="en"/> </head> <body> <div id="container-wrap-wrap"> <div id="container-wrap"> <div id="container"> <div id="header"> <h1><a href="index.html"><span>Hyperpolyglot</span></a></h1> </div> <div id="content-wrap"> <div id="main-content"> <div id="page-title"> Object-Oriented C Style Languages: C++, Objective-C, Java, C# </div> <div id="page-content"> <p><a name="top" id="top"></a><em>a side-by-side reference sheet</em></p> <p><a href="cpp#grammar-execution">grammar and execution</a> | <a href="cpp#variables-expressions">variables and expressions</a> | <a href="cpp#arithmetic-logic">arithmetic and logic</a> | <a href="cpp#strings">strings</a> | <a href="cpp#regexes">regexes</a> | <a href="cpp#dates-time">dates and time</a> | <a href="cpp#fixed-length-arrays">fixed-length arrays</a> | <a href="cpp#resizable-arrays">resizable arrays</a> | <a href="cpp#tuples">tuples</a> | <a href="cpp#dictionaries">dictionaries</a> | <a href="cpp#functions">functions</a> | <a href="cpp#execution-control">execution control</a> | <a href="cpp#exceptions">exceptions</a> | <a href="cpp#concurrency">concurrency</a> | <a href="cpp#file-handles">file handles</a> | <a href="cpp#files">files</a> | <a href="cpp#file-fmt">file formats</a> | <a href="cpp#directories">directories</a> | <a href="cpp#processes-environment">processes and environment</a> | <a href="cpp#libraries-namespaces">libraries and namespaces</a> | <a href="cpp#user-defined-types">user-defined types</a> | <a href="cpp#generic-types">generic types</a> | <a href="cpp#objects">objects</a> | <a href="cpp#inheritance-polymorphism">inheritance and polymorphism</a> | <a href="cpp#reflection">reflection</a> | <a href="cpp#net-web">net and web</a> | <a href="cpp#unit-tests">unit tests</a> | <a href="cpp#debugging-profiling">debugging and profiling</a></p> <table class="wiki-content-table"> <tr> <th colspan="5"><a name="version" id="version"></a><a href="cpp#version-note">version</a></th> </tr> <tr> <th></th> <th><a href="cpp#cpp">c++</a></th> <th><a href="cpp#objective-c">objective c</a></th> <th><a href="cpp#java">java</a></th> <th><a href="cpp#c-sharp">c#</a></th> </tr> <tr> <td><a name="version-used" id="version-used"></a><a href="cpp#version-used-note">version used</a><br /> <span style="white-space: pre-wrap;"> </span></td> <td><span style="color: gray"><em>C++11</em><br /> <em>gcc 4.8</em><br /> <em>clang 3.5</em></span></td> <td><span style="color: gray"><em>gcc 4.2</em></span></td> <td><span style="color: gray"><em>java 1.7</em></span></td> <td><span style="color: gray"><em>mono 2.10 (C# 4.0)</em></span></td> </tr> <tr> <td><a name="show-version" id="show-version"></a><a href="cpp#show-version-note">show version</a><br /> <span style="white-space: pre-wrap;"> </span></td> <td>$ g++ <span style="white-space: pre-wrap;">--</span>version</td> <td>$ gcc <span style="white-space: pre-wrap;">--</span>version</td> <td>$ javac -version</td> <td>$ mcs <span style="white-space: pre-wrap;">--</span>version</td> </tr> <tr> <td><a name="implicit-prologue" id="implicit-prologue"></a><a href="cpp#implicit-prologue-note">implicit prologue</a></td> <td>#include <iostream><br /> #include <string><br /> <br /> using namespace std;</td> <td></td> <td></td> <td></td> </tr> <tr> <th colspan="5"><a name="grammar-execution" id="grammar-execution"></a><a href="cpp#grammar-execution-note">grammar and execution</a></th> </tr> <tr> <th></th> <th>c++</th> <th>objective c</th> <th>java</th> <th>c#</th> </tr> <tr> <td><a name="hello-world" id="hello-world"></a><a href="cpp#hello-world-note">hello world</a></td> <td>$ cat hello.cpp<br /> #include <iostream><br /> <br /> using namespace std;<br /> <br /> int main(int argc, char<span style="white-space: pre-wrap;">**</span> arg) {<br /> <span style="white-space: pre-wrap;"> </span>cout <span style="white-space: pre-wrap;"><<</span> "Hello, World!" <span style="white-space: pre-wrap;"><<</span> endl;<br /> }<br /> <br /> $ g++ -std=c++0x hello.cpp<br /> <br /> $ ./a.out</td> <td>$ cat hello.m<br /> #include <stdio.h><br /> <br /> int main(int argc, char <span style="white-space: pre-wrap;">**</span>argv) {<br /> <span style="white-space: pre-wrap;"> </span>printf("Hello, World!\n");<br /> }<br /> <br /> $ gcc hello.m<br /> <br /> $ ./a.out</td> <td>$ cat Hello.java<br /> public class Hello {<br /> <span style="white-space: pre-wrap;"> </span>public static void main(String[] args) {<br /> <span style="white-space: pre-wrap;"> </span><span style="white-space: pre-wrap;"> </span>System.out.println("Hello, World!");<br /> <span style="white-space: pre-wrap;"> </span>}<br /> }<br /> <br /> $ javac Hello.java<br /> <br /> $ java Hello</td> <td>$ cat hello.cs<br /> using System;<br /> <br /> public class Hello {<br /> <span style="white-space: pre-wrap;"> </span>public static void Main() {<br /> <span style="white-space: pre-wrap;"> </span><span style="white-space: pre-wrap;"> </span>Console.WriteLine("Hello, World!");<br /> <span style="white-space: pre-wrap;"> </span>}<br /> }<br /> <br /> $ mcs hello.cs<br /> <br /> $ mono hello.exe</td> </tr> <tr> <td><a name="file-suffixes" id="file-suffixes"></a><a href="cpp#file-suffixes-note">file suffixes</a><br /> <span style="color: gray"><em>source, header, object file</em></span></td> <td>foo.cpp<br /> foo.h<br /> foo.o</td> <td>Foo.m<br /> Foo.h<br /> Foo.o</td> <td>Foo.java<br /> <span style="color: gray"><em>none</em></span><br /> Foo.class<br /> <br /> <span style="color: gray">Foo.java <em>must define a single top level class</em> Foo</span></td> <td>Foo.cs<br /> <span style="color: gray"><em>none</em></span><br /> Foo.exe <span style="color: gray"><em>or</em></span> Foo.dll<br /> <br /> <span style="color: gray"><em>although files are often named after a class they contain, this is not required</em></span></td> </tr> <tr> <td><a name="block-delimiters" id="block-delimiters"></a><a href="cpp#block-delimiters-note">block delimiters</a><br /> <span style="white-space: pre-wrap;"> </span></td> <td>{ }</td> <td>{ }</td> <td>{ }</td> <td>{ }</td> </tr> <tr> <td><a name="stmt-terminator" id="stmt-terminator"></a><a href="cpp#stmt-terminator-note">statement terminator</a><br /> <span style="white-space: pre-wrap;"> </span></td> <td>;</td> <td>;</td> <td>;</td> <td>;</td> </tr> <tr> <td><a name="top-level-stmt" id="top-level-stmt"></a><a href="cpp#top-level-stmt-note">top level statements</a></td> <td><span style="color: gray"><em>A source file will normally have</em> #include <em>directives at the top, followed by declarations, definitions, and namespaces containing declarations and definitions.<br /> <br /> After the preprocessor has finished processing a source file, the compilation unit will only contain declarations, definitions, and namespaces at the top level.</em></span></td> <td></td> <td><span style="color: gray"><em>each file contains the following elements in order:<br /> <br /> (1) optional package directive<br /> (2) zero or more import directives<br /> (3) one public class definition and zero or more private class definitions</em></span></td> <td></td> </tr> <tr> <td><a name="eol-comment" id="eol-comment"></a><a href="cpp#eol-comment-note">end-of-line comment</a><br /> <span style="white-space: pre-wrap;"> </span></td> <td><span style="color: gray"><span style="white-space: pre-wrap;">//</span> comment</span></td> <td><span style="color: gray"><span style="white-space: pre-wrap;">//</span> comment</span></td> <td><span style="color: gray"><span style="white-space: pre-wrap;">//</span> comment</span></td> <td><span style="color: gray"><span style="white-space: pre-wrap;">//</span> comment</span></td> </tr> <tr> <td><a name="multiple-line-comment" id="multiple-line-comment"></a><a href="cpp#multiple-line-comment-note">multiple line comment</a></td> <td><span style="color: gray">/* comment<br /> another comment */</span></td> <td><span style="color: gray">/* comment<br /> another comment */</span></td> <td><span style="color: gray">/* comment<br /> another comment */</span></td> <td><span style="color: gray">/* comment<br /> another comment */</span></td> </tr> <tr> <th colspan="5"><a name="variables-expressions" id="variables-expressions"></a><a href="cpp#variables-expressions-note">variables and expressions</a></th> </tr> <tr> <th></th> <th>c++</th> <th>objective c</th> <th>java</th> <th>c#</th> </tr> <tr> <td><a name="local-var" id="local-var"></a><a href="cpp#local-var-note">local variable</a></td> <td>int i;<br /> int j = 3;<br /> int k(7);</td> <td>int i;<br /> int j = 3;</td> <td>int i;<br /> int j = 3;</td> <td>int i;<br /> int j = 3;</td> </tr> <tr> <td><a name="uninitialized-local-var" id="uninitialized-local-var"></a><a href="cpp#uninitialized-local-var-note">uninitialized local variable</a></td> <td><span style="color: gray"><em>The behavior is undefined.<br /> <br /> Most implementations do not zero-initialize stack variables, so the value will be whatever happened to be in memory.</em></span></td> <td><span style="color: gray"><em>behavior is undefined.<br /> <br /> Most implementations do not zero-initialize stack variables, so the value will be whatever happened to be in memory.</em></span></td> <td><span style="color: gray"><em>compiler error</em></span></td> <td><span style="color: gray"><em>compiler prevents use of uninitialized local variable</em></span></td> </tr> <tr> <td><a name="global-var" id="global-var"></a><a href="cpp#global-var-note">global variable</a></td> <td><span style="color: gray"><span style="white-space: pre-wrap;">//</span> in foo.cpp and outside of any function<br /> <span style="white-space: pre-wrap;">//</span> or class definition:</span><br /> int foo = 7;<br /> <br /> <span style="color: gray"><span style="white-space: pre-wrap;">//</span> in bar.cpp and outside of any function<br /> <span style="white-space: pre-wrap;">//</span> or class definition:</span><br /> extern int foo;</td> <td><span style="color: gray"><em>in</em> foo.cpp <em>outside of any function or class definition:</em></span><br /> int foo = 7;<br /> <br /> <span style="color: gray"><em>in</em> bar.cpp <em>outside of any function or class definition:</em></span><br /> extern int foo;</td> <td><span style="color: gray"><em>foo/Foo.java:</em></span><br /> package foo;<br /> <br /> <span style="color: gray"><span style="white-space: pre-wrap;">//</span> globals must be declared inside a<br /> <span style="white-space: pre-wrap;">//</span> class:</span><br /> public class Foo {<br /> <span style="white-space: pre-wrap;"> </span>public static int bar;<br /> }<br /> <br /> <span style="color: gray"><em>UseFoo.java:</em></span><br /> import foo.Foo;<br /> <br /> public class UseFoo {<br /> <span style="white-space: pre-wrap;"> </span>public static void main(String[] args) {<br /> <span style="white-space: pre-wrap;"> </span><span style="white-space: pre-wrap;"> </span>System.out.println(Foo.bar);<br /> <span style="white-space: pre-wrap;"> </span>}<br /> }</td> <td></td> </tr> <tr> <td><a name="uninitialized-global-var" id="uninitialized-global-var"></a><a href="cpp#uninitialized-global-var-note">uninitialized global variable</a></td> <td><span style="color: gray"><em>Zero initialized: numeric types and pointers are set to zero. Classes, structs, and arrays have all of their members or elements zero-initialized recursively.</em></span></td> <td></td> <td><span style="color: gray"><em>Zero initialized.</em></span></td> <td></td> </tr> <tr> <td><a name="write-once-var" id="write-once-var"></a><a href="cpp#write-once-var-note">write-once variable</a></td> <td>const int i = 7;</td> <td>const int i = 7;</td> <td>final int i = 7;</td> <td>const int i = 7;</td> </tr> <tr> <td><a name="assignment" id="assignment"></a><a href="cpp#assignment-note">assignment</a></td> <td>int n;<br /> n = 3;</td> <td></td> <td>int n;<br /> n = 3;</td> <td></td> </tr> <tr> <td><a name="compound-assignment" id="compound-assignment"></a><a href="cpp#compound-assignment-note">compound assignment</a><br /> <span style="color: gray"><em>arithmetic, bit</em></span></td> <td>+= -= *= /= %=<br /> <span style="white-space: pre-wrap;"><<= >>= </span>&= ^= |=</td> <td></td> <td>+= -= *= /= %=<br /> <span style="white-space: pre-wrap;"><<= >>= </span>&= ^= |=<br /> <br /> <span style="color: gray"><span style="white-space: pre-wrap;">>>=</span> <em>is arithmetic right shift,</em> <span style="white-space: pre-wrap;">>>>=</span> <em>is logical right shift</em></span></td> <td></td> </tr> <tr> <td><a name="incr-decr" id="incr-decr"></a><a href="cpp#incr-decr-note">increment and decrement</a></td> <td>int n = 1;<br /> int one = n++;<br /> int three = ++n;<br /> int two = <span style="white-space: pre-wrap;">--</span>n;</td> <td></td> <td>int n = 1;<br /> int one = n++;<br /> int three = ++n;<br /> int two = <span style="white-space: pre-wrap;">--</span>n;</td> <td></td> </tr> <tr> <td><a name="addr" id="addr"></a><a href="cpp#addr-note">address</a></td> <td>int i(3);<br /> int* ip = &i;</td> <td></td> <td><span style="color: gray"><em>none</em></span></td> <td></td> </tr> <tr> <td><a name="dereference" id="dereference"></a><a href="cpp#dereference-note">dereference</a></td> <td>int i(3);<br /> int* ip = &i;<br /> int i2 = *ip + 1;</td> <td></td> <td><span style="color: gray"><em>none</em></span></td> <td></td> </tr> <tr> <td><a name="type-size" id="type-size"></a><a href="cpp#type-size-note">type size</a></td> <td>cout <span style="white-space: pre-wrap;"><<</span> sizeof(int) <span style="white-space: pre-wrap;"><<</span> endl;<br /> cout <span style="white-space: pre-wrap;"><<</span> sizeof(int*) <span style="white-space: pre-wrap;"><<</span> endl;</td> <td></td> <td><span style="color: gray"><em>none</em></span></td> <td></td> </tr> <tr> <td><a name="addr-arith" id="addr-arith"></a><a href="cpp#addr-arith-note">address arithmetic</a></td> <td></td> <td></td> <td><span style="color: gray"><em>none</em></span></td> <td></td> </tr> <tr> <td><a name="unique-ptr" id="unique-ptr"></a><a href="cpp#unique-ptr-note">unique pointer</a></td> <td></td> <td></td> <td><span style="color: gray"><em>none</em></span></td> <td></td> </tr> <tr> <td><a name="ref-cnt-ptr" id="ref-cnt-ptr"></a><a href="cpp#ref-cnt-ptr-note">reference count pointer</a></td> <td></td> <td></td> <td><span style="color: gray"><em>none</em></span></td> <td></td> </tr> <tr> <td><a name="weak-ptr" id="weak-ptr"></a><a href="cpp#weak-ptr-note">weak pointer</a></td> <td></td> <td></td> <td><span style="color: gray"><em>none</em></span></td> <td></td> </tr> <tr> <td><a name="allocate-heap" id="allocate-heap"></a><a href="cpp#allocate-heap-note">allocate heap</a></td> <td>int* ip = new int;</td> <td>#include <stdlib.h><br /> <br /> int *ip = malloc(sizeof(int));</td> <td><span style="color: gray"><span style="white-space: pre-wrap;">//</span> Primitive types are stack allocated.<br /> <span style="white-space: pre-wrap;">//</span> Use a wrapper class to store on the<br /> <span style="white-space: pre-wrap;">//</span> heap:</span><br /> Integer i = new Integer(0);</td> <td>object i = 0;</td> </tr> <tr> <td><a name="uninitialized-heap" id="uninitialized-heap"></a><a href="cpp#uninitialized-heap-note">uninitialized heap</a></td> <td><span style="color: gray"><em>Memory allocated by the</em> new <em>operator is zero-initialized.</em></span></td> <td></td> <td><span style="color: gray"><em>zero-initialized</em></span></td> <td></td> </tr> <tr> <td><a name="free-heap" id="free-heap"></a><a href="cpp#free-heap-note">free heap</a></td> <td>delete ip;</td> <td>#include <stdlib.h><br /> <br /> free(ip);</td> <td><span style="color: gray"><em>garbage collected</em></span></td> <td><span style="color: gray"><em>garbage collected</em></span></td> </tr> <tr> <td><a name="null" id="null"></a><a href="cpp#null-note">null</a><br /> <span style="white-space: pre-wrap;"> </span></td> <td>NULL</td> <td>NULL</td> <td>null</td> <td>null</td> </tr> <tr> <td><a name="coalesce" id="coalesce"></a><a href="cpp#coalesce-note">coalesce</a><br /> <span style="white-space: pre-wrap;"> </span></td> <td>string s1 = s2 <span style="white-space: pre-wrap;">||</span> "was null";</td> <td>NSString *s1 = s2 <span style="white-space: pre-wrap;">||</span> @"was null";</td> <td>String s1 = s2 == null ? "was null" : s2;</td> <td>string s1 = s2 ?? "was null";</td> </tr> <tr> <th colspan="5"><a name="arithmetic-logic" id="arithmetic-logic"></a><a href="cpp#arithmetic-logic-note">arithmetic and logic</a></th> </tr> <tr> <th></th> <th>c++</th> <th>objective c</th> <th>java</th> <th>c#</th> </tr> <tr> <td><a name="boolean-type" id="boolean-type"></a><a href="cpp#boolean-type-note">boolean type</a><br /> <span style="white-space: pre-wrap;"> </span></td> <td>bool</td> <td>BOOL</td> <td>boolean</td> <td>bool</td> </tr> <tr> <td><a name="true-false" id="true-false"></a><a href="cpp#true-false-note">true and false</a><br /> <span style="white-space: pre-wrap;"> </span></td> <td>true false</td> <td>YES NO</td> <td>true false</td> <td>true false</td> </tr> <tr> <td><a name="falsehoods" id="falsehoods"></a><a href="cpp#falsehoods-note">falsehoods</a><br /> <span style="white-space: pre-wrap;"> </span></td> <td>false 0 0.0 NULL</td> <td>0 0.0 NULL</td> <td>false</td> <td>false</td> </tr> <tr> <td><a name="logical-op" id="logical-op"></a><a href="cpp#logical-op-note">logical operators</a></td> <td>&& <span style="white-space: pre-wrap;">||</span> !<br /> and or not</td> <td>&& <span style="white-space: pre-wrap;">||</span> !</td> <td>&& <span style="white-space: pre-wrap;">||</span> !</td> <td>&& <span style="white-space: pre-wrap;">||</span> !</td> </tr> <tr> <td><a name="relational-op" id="relational-op"></a><a href="cpp#relational-op-note">relational operators</a></td> <td>== != < > <= >=</td> <td>== != < > <= >=</td> <td>== != < > <= >=</td> <td>== != < > <= >=</td> </tr> <tr> <td><a name="int-type" id="int-type"></a><a href="cpp#int-type-note">integer type</a></td> <td>signed char n1;<span style="white-space: pre-wrap;"> </span><span style="white-space: pre-wrap;"> </span><span style="color: gray"><span style="white-space: pre-wrap;">//</span> 1+ bytes</span><br /> short int n2;<span style="white-space: pre-wrap;"> </span><span style="white-space: pre-wrap;"> </span><span style="white-space: pre-wrap;"> </span><span style="color: gray"><span style="white-space: pre-wrap;">//</span> 2+ bytes</span><br /> int n3;<span style="white-space: pre-wrap;"> </span><span style="white-space: pre-wrap;"> </span><span style="white-space: pre-wrap;"> </span><span style="white-space: pre-wrap;"> </span><span style="white-space: pre-wrap;"> </span><span style="white-space: pre-wrap;"> </span><span style="color: gray"><span style="white-space: pre-wrap;">//</span> 2+ bytes</span><br /> long int n4;<span style="white-space: pre-wrap;"> </span><span style="white-space: pre-wrap;"> </span><span style="white-space: pre-wrap;"> </span><span style="color: gray"><span style="white-space: pre-wrap;">//</span> 4+ bytes</span><br /> long long int n5;<span style="white-space: pre-wrap;"> </span><span style="color: gray"><span style="white-space: pre-wrap;">//</span> 4+ bytes</span></td> <td>signed char <span style="color: gray"><em>1+ byte</em></span>#<br /> short int <span style="color: gray"><em>2+ bytes</em></span><br /> int <span style="color: gray"><em>2+ bytes</em></span><br /> long int <span style="color: gray"><em>4+ bytes</em></span><br /> long long int <span style="color: gray"><em>4+ bytes</em></span></td> <td>byte n1;<span style="white-space: pre-wrap;"> </span><span style="color: gray"><span style="white-space: pre-wrap;">//</span> 1 byte</span><br /> short n2; <span style="color: gray"><span style="white-space: pre-wrap;">//</span> 2 bytes</span><br /> int n3;<span style="white-space: pre-wrap;"> </span><span style="color: gray"><span style="white-space: pre-wrap;">//</span> 4 bytes</span><br /> long n4;<span style="white-space: pre-wrap;"> </span><span style="color: gray"><span style="white-space: pre-wrap;">//</span> 8 bytes</span></td> <td>sbyte <span style="color: gray"><em>1 byte</em></span><br /> short <span style="color: gray"><em>2 bytes</em></span><br /> int <span style="color: gray"><em>4 bytes</em></span><br /> long <span style="color: gray"><em>8 bytes</em></span></td> </tr> <tr> <td><a name="unsigned-type" id="unsigned-type"></a><a href="cpp#unsigned-type-note">unsigned type</a></td> <td>unsigned char n1;<span style="white-space: pre-wrap;"> </span><span style="white-space: pre-wrap;"> </span><span style="white-space: pre-wrap;"> </span><span style="white-space: pre-wrap;"> </span><span style="white-space: pre-wrap;"> </span><span style="color: gray"><span style="white-space: pre-wrap;">//</span> 1+ bytes</span><br /> unsigned short int n2;<span style="white-space: pre-wrap;"> </span><span style="white-space: pre-wrap;"> </span><span style="white-space: pre-wrap;"> </span><span style="color: gray"><span style="white-space: pre-wrap;">//</span> 2+ bytes</span><br /> unsigned int n3;<span style="white-space: pre-wrap;"> </span><span style="white-space: pre-wrap;"> </span><span style="white-space: pre-wrap;"> </span><span style="white-space: pre-wrap;"> </span><span style="white-space: pre-wrap;"> </span><span style="white-space: pre-wrap;"> </span><span style="color: gray"><span style="white-space: pre-wrap;">//</span> 2+ bytes</span><br /> unsigned long int n4;<span style="white-space: pre-wrap;"> </span><span style="white-space: pre-wrap;"> </span><span style="white-space: pre-wrap;"> </span><span style="color: gray"><span style="white-space: pre-wrap;">//</span> 4+ bytes</span><br /> unsigned long long int n5; <span style="color: gray"><span style="white-space: pre-wrap;">//</span> 4+ bytes</span></td> <td>unsigned char: 8+<br /> unsigned short int <span style="color: gray"><em>2 bytes+</em></span><br /> unsigned int <span style="color: gray"><em>2 bytes+</em></span><br /> unsigned long int <span style="color: gray"><em>4+ bytes</em></span><br /> unsigned long long int <span style="color: gray"><em>4+ bytes</em></span></td> <td>char n1;<span style="white-space: pre-wrap;"> </span><span style="color: gray"><span style="white-space: pre-wrap;">//</span>2 bytes</span></td> <td>byte <span style="color: gray"><em>1 byte</em></span><br /> ushort <span style="color: gray"><em>2 bytes</em></span><br /> uint <span style="color: gray"><em>4 bytes</em></span><br /> ulong <span style="color: gray"><em>8 bytes</em></span></td> </tr> <tr> <td><a name="float-type" id="float-type"></a><a href="cpp#float-type-note">float type</a></td> <td>float x1;<span style="white-space: pre-wrap;"> </span><span style="white-space: pre-wrap;"> </span><span style="white-space: pre-wrap;"> </span><span style="white-space: pre-wrap;"> </span><span style="color: gray"><span style="white-space: pre-wrap;">//</span> 4 bytes</span><br /> double x2;<span style="white-space: pre-wrap;"> </span><span style="white-space: pre-wrap;"> </span><span style="white-space: pre-wrap;"> </span><span style="color: gray"><span style="white-space: pre-wrap;">//</span> 8 bytes</span><br /> long double x3; <span style="color: gray"><span style="white-space: pre-wrap;">//</span> 16 bytes</span></td> <td>float<br /> double<br /> long double</td> <td>float x1;<span style="white-space: pre-wrap;"> </span><span style="color: gray"><span style="white-space: pre-wrap;">//</span> 4 bytes</span><br /> double x2; <span style="color: gray"><span style="white-space: pre-wrap;">//</span> 8 bytes</span></td> <td>float <span style="color: gray"><em>4 bytes</em></span><br /> double <span style="color: gray"><em>8 bytes</em></span></td> </tr> <tr> <td><a name="fixed-type" id="fixed-type"></a><a href="cpp#fixed-type-note">fixed type</a><br /> <span style="white-space: pre-wrap;"> </span></td> <td><span style="color: gray"><em>none</em></span></td> <td><span style="color: gray"><em>none</em></span></td> <td><span style="color: gray"><em>none</em></span></td> <td>decimal <span style="color: gray"><em>12 bytes</em></span></td> </tr> <tr> <td><a name="arithmetic-op" id="arithmetic-op"></a><a href="cpp#arithmetic-op-note">arithmetic operators</a></td> <td>+ - * / %</td> <td>+ - * / %</td> <td>+ - * / %</td> <td>+ - * / %</td> </tr> <tr> <td><a name="int-div" id="int-div"></a><a href="cpp#int-div-note">integer division</a></td> <td><span style="color: gray"><span style="white-space: pre-wrap;">//</span> evaluates to 2:</span><br /> 7 / 3</td> <td><span style="color: gray"><span style="white-space: pre-wrap;">//</span> evaluates to 2:</span><br /> 7 / 3</td> <td><span style="color: gray"><span style="white-space: pre-wrap;">//</span> evaluates to 2:</span><br /> 7 / 3</td> <td><span style="color: gray"><span style="white-space: pre-wrap;">//</span> evaluates to 2:</span><br /> 7 / 3</td> </tr> <tr> <td><a name="int-div-zero" id="int-div-zero"></a><a href="cpp#int-div-zero-note">integer division by zero</a></td> <td><span style="color: gray"><em>process sent a</em></span> SIGFPE <span style="color: gray"><em>signal</em></span></td> <td><span style="color: gray"><em>process sent a</em></span> SIGFPE <span style="color: gray"><em>signal</em></span></td> <td><span style="color: gray"><em>throws</em></span> java.lang.ArithmeticException</td> <td><span style="color: gray"><em>Syntax error if divisor is a constant. Otherwise throws</em></span> System.DivideByZeroException</td> </tr> <tr> <td><a name="float-div" id="float-div"></a><a href="cpp#float-div-note">float division</a><br /> <span style="white-space: pre-wrap;"> </span></td> <td>7 / static_cast<float>(3)</td> <td>7 / (float)3</td> <td>7 / (float)3</td> <td>7 / (float)3</td> </tr> <tr> <td><a name="float-div-zero" id="float-div-zero"></a><a href="cpp#float-div-zero-note">float division by zero</a><br /> <span style="color: gray"><em>dividend is positive, zero, negative</em></span></td> <td>inf<br /> nan<br /> -inf<br /> <br /> <span style="color: gray"><em>There are no portably defined literals or constants for the above values.</em></span></td> <td>inf<br /> nan<br /> -inf<br /> <br /> <span style="color: gray"><em>there are no portably defined literals or constants for the above values.</em></span></td> <td>Float.POSITIVE_INFINITY<br /> Float.NaN<br /> Float.NEGATIVE_INFINITY<br /> <br /> <span style="color: gray"><em>constants with same names defined in</em></span> Double</td> <td>float.PositiveInfinity<br /> float.NaN<br /> float.NegativeInfinity<br /> <br /> <span style="color: gray"><em>constants with same names defined in</em></span> double</td> </tr> <tr> <td><a name="power" id="power"></a><a href="cpp#power-note">power</a></td> <td>#include <cmath><br /> <br /> double x = pow(2.0, 32.0);</td> <td>#include <math.h><br /> <br /> pow(2.0, 32.0);</td> <td>Math.pow(2.0, 32.0);</td> <td>System.Math.Pow(2.0, 32.0);</td> </tr> <tr> <td><a name="sqrt" id="sqrt"></a><a href="cpp#sqrt-note">sqrt</a></td> <td>#include <cmath><br /> <br /> double x = sqrt(2);</td> <td>#include <math.h><br /> <br /> sqrt(2)</td> <td>Math.sqrt(2)</td> <td>Math.Sqrt(2)</td> </tr> <tr> <td><a name="sqrt-negative-one" id="sqrt-negative-one"></a><a href="cpp#sqrt-negative-one-note">sqrt -1</a></td> <td>nan</td> <td>nan</td> <td>Double.NaN</td> <td>double.NaN</td> </tr> <tr> <td><a name="transcendental-func" id="transcendental-func"></a><a href="cpp#transcendental-func-note">transcendental functions</a></td> <td>#include <cmath><br /> <br /> exp log log2 log10<br /> sin cos tan<br /> asin acos atan<br /> atan2</td> <td>#include <math.h><br /> <br /> exp log log2 log10<br /> sin cos tan<br /> asin acos atan<br /> atan2</td> <td>Math.exp Math.log <span style="color: gray"><em>none</em></span> Math.log10<br /> Math.sin Math.cos Math.tan<br /> Math.asin Math.acos Math.atan<br /> Math.atan2</td> <td>using System;<br /> <span style="white-space: pre-wrap;"> </span><br /> Math.Exp Math.Log <span style="color: gray"><em>none</em></span> Math.Log10<br /> Math.Sin Math.Cos Math.Tan<br /> Math.Asin Math.Acos Math.Atan<br /> Math.Atan2</td> </tr> <tr> <td><a name="transcendental-const" id="transcendental-const"></a><a href="cpp#transcendental-const-note">transcendental constants</a></td> <td>#include <cmath><br /> <br /> double e = M_E;<br /> double pi = M_PI;</td> <td>#include <math.h><br /> <br /> M_E<br /> M_PI</td> <td>Math.E<br /> Math.PI</td> <td>System.Math.E<br /> System.Math.PI</td> </tr> <tr> <td><a name="float-truncation" id="float-truncation"></a><a href="cpp#float-truncation-note">float truncation</a><br /> <span style="color: gray"><em>towards zero, to nearest integer, towards -∞, towards ∞</em></span></td> <td>#include <cmath><br /> <span style="white-space: pre-wrap;"> </span><br /> double x = 3.7;<br /> <span style="white-space: pre-wrap;"> </span><br /> long trnc = static_cast<long>(x);<br /> long rnd = round(x);<br /> long flr = floorl(x);<br /> long cl = ceill(x);</td> <td>#include <math.h><br /> <span style="white-space: pre-wrap;"> </span><br /> double d = 3.77;<br /> <span style="white-space: pre-wrap;"> </span><br /> long trnc = (long)d;<br /> long rnd = round(d);<br /> long flr = floorl(d);<br /> long cl = ceill(d);</td> <td>(long)3.77<br /> Math.round(3.77)<br /> (long)Math.floor(3.77)<br /> (long)Math.ceil(3.77)</td> <td>using System;<br /> <span style="white-space: pre-wrap;"> </span><br /> (long)3.77<br /> Math.Round(3.77)<br /> Math.Floor(3.77)<br /> Math.Ceiling(3.77)</td> </tr> <tr> <td><a name="absolute-val" id="absolute-val"></a><a href="cpp#absolute-val-note">absolute value</a></td> <td>#include <cmath><span style="white-space: pre-wrap;"> </span><span style="white-space: pre-wrap;"> </span><span style="color: gray"><span style="white-space: pre-wrap;">//</span> fabs()</span><br /> #include <cstdlib><span style="white-space: pre-wrap;"> </span><span style="color: gray"><span style="white-space: pre-wrap;">//</span> abs()</span><br /> <br /> int n = -7;<br /> int absn = abs(n);<br /> <br /> double x = -7.77;<br /> double absx = fabs(x);</td> <td>#include <stdlib.h><span style="white-space: pre-wrap;"> </span><span style="color: gray"><span style="white-space: pre-wrap;">//</span> abs()</span><br /> #include <math.h><span style="white-space: pre-wrap;"> </span><span style="color: gray"><span style="white-space: pre-wrap;">//</span> fabs()</span><br /> <br /> int i = -7;<br /> int ai = abs(i);<br /> <br /> float x = -7.77;<br /> float ax = fabs(x);</td> <td>Math.abs(-7)<br /> Math.abs(-7.77)</td> <td>System.Math.Abs(-7)<br /> System.Math.Abs(-7.77)</td> </tr> <tr> <td><a name="int-overflow" id="int-overflow"></a><a href="cpp#int-overflow-note">integer overflow</a></td> <td><span style="color: gray"><em>modular arithmetic<br /> <br /> The C standard does not define behavior for signed integers, however.</em></span></td> <td><span style="color: gray"><em>modular arithmetic<br /> <br /> The C standard does not define behavior for signed integers, however.</em></span></td> <td><span style="color: gray"><em>modular arithmetic</em></span></td> <td><span style="color: gray"><em>modular arithmetic</em></span></td> </tr> <tr> <td><a name="float-overflow" id="float-overflow"></a><a href="cpp#float-overflow-note">float overflow</a></td> <td><span style="color: gray"><em>no behavior defined by standard; many implementations return</em> inf</span></td> <td><span style="color: gray"><em>no behavior defined by standard; many implementations return</em> inf</span></td> <td>Float.POSITIVE_INFINITY</td> <td>float.PositiveInfinity</td> </tr> <tr> <td><a name="float-limits" id="float-limits"></a><a href="cpp#float-limits-note">float limits</a><br /> <span style="color: gray"><em>largest finite float, smallest positive float</em></span></td> <td>#include <cfloat><br /> <br /> FLT_MAX<br /> FLT_MIN<br /> DBL_MAX<br /> DBL_MIN<br /> LDBL_MAX<br /> LDBL_MIN</td> <td></td> <td>Float.MAX_VALUE<br /> Float.MIN_VALUE<br /> Double.MAX_VALUE<br /> Double.MIN_VALUE</td> <td>float.MaxValue<br /> float.Epsilon<br /> double.MaxValue<br /> double.Epsilon</td> </tr> <tr> <td><a name="complex-construction" id="complex-construction"></a><a href="cpp#complex-construction-note">complex construction</a></td> <td>#include <complex><br /> <br /> complex<double> z(1.0, 2.0);</td> <td></td> <td></td> <td></td> </tr> <tr> <td><a name="complex-decomposition" id="complex-decomposition"></a><a href="cpp#complex-decomposition-note">complex decomposition</a><br /> <span style="color: gray"><em>real and imaginary component, argument, absolute value, conjugate</em></span></td> <td>z.real()<br /> z.imag()<br /> arg(z)<br /> abs(z)<br /> conj(z)</td> <td></td> <td></td> <td></td> </tr> <tr> <td><a name="random-num" id="random-num"></a><a href="cpp#random-num-note">random number</a><br /> <span style="color: gray"><em>uniform integer, uniform float, normal float</em></span></td> <td>#include <random><br /> <br /> default_random_engine dre;<br /> <br /> uniform_int_distribution<int> uid(0, 99);<br /> uniform_real_distribution<double><br /> <span style="white-space: pre-wrap;"> </span>urd(0.0, 1.0);<br /> normal_distribution<double> nd(0.0, 1.0);<br /> <br /> int i = uid(dre);<br /> double x = urd(dre);<br /> double y = nd(dre);</td> <td>#include <stdlib.h><br /> <br /> <span style="color: gray"><span style="white-space: pre-wrap;">//</span> assuming 100 much smaller than RAND_MAX:</span><br /> int i = rand() % 100;<br /> double x = drand48();<br /> <span style="color: gray"><em>none</em></span></td> <td>import java.util.Random;<br /> <br /> Random rnd = new Random();<br /> <br /> int i = rnd.nextInt(100);<br /> double x = rnd.nextDouble();<br /> double y = rnd.nextGaussian();</td> <td>using System;<br /> <br /> Random rnd = new Random();<br /> <br /> int i = rnd.Next();<br /> double x = rnd.NextDouble();<br /> <span style="color: gray"><em>none</em></span></td> </tr> <tr> <td><a name="random-seed" id="random-seed"></a><a href="cpp#random-seed-note">random seed</a></td> <td>#include <random><br /> <br /> <span style="color: gray"><span style="white-space: pre-wrap;">//</span> set seed in constructor:</span><br /> default_random_engine dre(17);<br /> <br /> <span style="color: gray"><span style="white-space: pre-wrap;">//</span> set seed of existing engine:</span><br /> dre.seed(17);</td> <td></td> <td>import java.util.Random;<br /> <br /> Random rnd = new Random();<br /> <br /> rnd.setSeed(17);<br /> <br /> <span style="color: gray"><span style="white-space: pre-wrap;">//</span> seed can also be passed to constructor</span></td> <td>using System;<br /> <br /> Random rnd = new Random(17);</td> </tr> <tr> <td><a name="bit-op" id="bit-op"></a><a href="cpp#bit-op-note">bit operators</a></td> <td><span style="white-space: pre-wrap;"> << >> & | ^ ~ </span><br /> <span style="white-space: pre-wrap;"> </span>bitand bitor compl<br /> <br /> <span style="color: gray"><span style="white-space: pre-wrap;">>></span> <em>is arithmetic right shift on signed integers and logical right shift on unsigned integers</em></span></td> <td><span style="white-space: pre-wrap;"> << >> & | ^ ~ </span></td> <td><span style="white-space: pre-wrap;"> << >> & | ^ ~ </span><br /> <br /> <span style="color: gray"><span style="white-space: pre-wrap;">>></span> <em>is arithmetic right shift, <span style="white-space: pre-wrap;">>>></span> is logical right shift</em></span></td> <td><span style="white-space: pre-wrap;"> << >> & | ^ ~ </span></td> </tr> <tr> <td><a name="binary-octal-hex-literals" id="binary-octal-hex-literals"></a><a href="cpp#binary-octal-hex-literals-note">binary, octal, and hex literals</a></td> <td>0b0101010<br /> 052<br /> 0x2a</td> <td></td> <td><span style="color: gray"><em>none in Java 1.6</em></span><br /> 052<br /> 0x2a</td> <td><span style="color: gray"><em>none</em></span><br /> 052<br /> 0x2a</td> </tr> <tr> <td><a name="radix" id="radix"></a><a href="cpp#radix-note">radix</a><br /> <span style="color: gray"><em>convert integer to and from string with radix</em></span></td> <td></td> <td></td> <td>Integer.toString(42, 7)<br /> Integer.parseInt("60", 7)</td> <td></td> </tr> <tr> <th colspan="5"><a name="strings" id="strings"></a><a href="cpp#strings-note">strings</a></th> </tr> <tr> <th></th> <th>c++</th> <th>objective c</th> <th>java</th> <th>c#</th> </tr> <tr> <td><a name="str-type" id="str-type"></a><a href="cpp#str-type-note">string type</a><br /> <span style="white-space: pre-wrap;"> </span></td> <td>string s("lorem ipsum");<br /> <br /> <span style="color: gray"><span style="white-space: pre-wrap;">//</span> convert to C string:</span><br /> const char* s2 = s.c_str();</td> <td>NSString* s = @"lorem ipsum";<br /> <br /> <span style="color: gray"><span style="white-space: pre-wrap;">//</span> convert to C string:</span><br /> const char* s2 = [s UTF8String];</td> <td>java.lang.String</td> <td>string</td> </tr> <tr> <td><a name="str-literal" id="str-literal"></a><a href="cpp#str-literal-note">string literal</a><br /> <span style="white-space: pre-wrap;"> </span></td> <td><span style="color: gray"><span style="white-space: pre-wrap;">//</span> const char*:</span><br /> "don't say \"no\""</td> <td>@"don't say \"no""</td> <td>"don't say\"no\""</td> <td>"don't say \"no\""</td> </tr> <tr> <td><a name="newline-literal" id="newline-literal"></a><a href="cpp#newline-literal-note">newline in literal</a></td> <td><span style="color: gray"><em>Newlines in string literals are ignored.</em></span></td> <td><span style="color: gray"><em>string literals can extend over multiple lines, but the newlines do not appear in the resulting string</em></span></td> <td><span style="color: gray"><em>no</em></span></td> <td><span style="color: gray"><em>string literals can extend over multiple lines, but the newlines do not appear in the resulting string</em></span></td> </tr> <tr> <td><a name="str-literal-escape" id="str-literal-escape"></a><a href="cpp#str-literal-escape-note">literal escapes</a><br /> <span style="white-space: pre-wrap;"> </span></td> <td>\a \b \f \n \r \t \v<br /> \\ \" \'<br /> \x<span style="color: gray"><em>hh</em></span> \<span style="color: gray"><em>o</em></span> \<span style="color: gray"><em>oo</em></span> \<span style="color: gray"><em>ooo</em></span></td> <td>\a \b \f \n \r \t \v<br /> \\ \" \'<br /> \x<span style="color: gray"><em>hh</em></span> \<span style="color: gray"><em>o</em></span> \<span style="color: gray"><em>oo</em></span> \<span style="color: gray"><em>ooo</em></span></td> <td>\b \f \n \r \t<br /> \\ \" \'<br /> \u<span style="color: gray"><em>hhhh</em></span> \<span style="color: gray"><em>o</em></span> \<span style="color: gray"><em>oo</em></span> \<span style="color: gray"><em>ooo</em></span></td> <td>\a \b \f \n \r \t \v<br /> \\ \" \'<br /> \x<span style="color: gray"><em>hh</em></span> \x<span style="color: gray"><em>hhhh</em></span> \<span style="color: gray"><em>o</em></span> \<span style="color: gray"><em>oo</em></span> \<span style="color: gray"><em>ooo</em></span></td> </tr> <tr> <td><a name="allocate-str" id="allocate-str"></a><a href="cpp#allocate-str-note">allocate string</a></td> <td>string* s = new string("hello");</td> <td>NSString *s = @"hello";</td> <td>String s = "hello";<br /> String t = new String(s);</td> <td>string s = "hello";<br /> string t = string.Copy(s);</td> </tr> <tr> <td><a name="mutable-str" id="mutable-str"></a><a href="cpp#mutable-str-note">are strings mutable?</a></td> <td>string s("bar");<br /> s[2] = 'z';</td> <td></td> <td><span style="color: gray">String <em>objects are immutable.</em></span><br /> <br /> <span style="color: gray">StringBuffer <em>has</em> append(), delete(), deleteCharAt(), insert(), replace(), setCharAt().</span></td> <td></td> </tr> <tr> <td><a name="copy-str" id="copy-str"></a><a href="cpp#copy-str-note">copy string</a></td> <td>string s("bar");<br /> <br /> <span style="color: gray"><span style="white-space: pre-wrap;">//</span> use assignment or copy constructor:</span><br /> string s2 = s;<br /> string s3(s);<br /> <br /> <span style="color: gray"><span style="white-space: pre-wrap;">//</span> s contains "baz";</span><br /> <span style="color: gray"><span style="white-space: pre-wrap;">//</span> s2 and s3 contain "bar":</span><br /> s[2] = 'z';</td> <td></td> <td>String s = "bar";<br /> StringBuffer sb = new StringBuffer(s);<br /> sb.setCharAt(2, 'z');<br /> <span style="color: gray"><span style="white-space: pre-wrap;">//</span> s contains "bar"; s2 contains "baz":</span><br /> String s2 = sb.toString();</td> <td></td> </tr> <tr> <td><a name="fmt-str" id="fmt-str"></a><a href="cpp#fmt-str-note">format string</a></td> <td>#include <sstream><br /> <br /> ostringstream oss;<br /> oss <span style="white-space: pre-wrap;"><<</span> "Spain: " <span style="white-space: pre-wrap;"><<</span> 7;<br /> string s(oss.str());</td> <td>[NSString stringWithFormat:@"%@: %d", @"Spain", 7]</td> <td>String.format("%s: %d", "Spain", 7)</td> <td>string.Format("{0}: {1}", "Spain", 7)</td> </tr> <tr> <td><a name="compare-str" id="compare-str"></a><a href="cpp#compare-str-note">compare strings</a></td> <td>string s1("hello");<br /> string s2("world");<br /> <br /> <span style="color: gray"><span style="white-space: pre-wrap;">//</span> negative if s1 lexically before s2;<br /> <span style="white-space: pre-wrap;">//</span> zero if s1 and s2 are equal:</span><br /> int result1 = s1.compare(s2);<br /> <br /> bool result2 = s1 == s2;</td> <td>[@"hello" compare:@"hello"]</td> <td>"hello".compareTo("world")</td> <td>"hello".CompareTo("world")</td> </tr> <tr> <td><a name="str-concat" id="str-concat"></a><a href="cpp#str-concat-note">concatenate</a><br /> <span style="color: gray"><em>and append</em></span></td> <td>string s("hello");<br /> string s2 = s + " world";<br /> s += " world";</td> <td>NSString *s1 = @"hello";<br /> NSString *s2 = @" world";<br /> NSString *s3 = [s1 stringByAppendingString:s2];</td> <td>"hello" + " world"</td> <td>"hello" + " world"</td> </tr> <tr> <td><a name="str-replicate" id="str-replicate"></a><a href="cpp#str-replicate-note">replicate</a><br /> <span style="white-space: pre-wrap;"> </span></td> <td>string hbar(80, '-');</td> <td></td> <td>import java.util.Arrays;<br /> <br /> char[] a = new char[80];<br /> Arrays.fill(a, '-');<br /> String s = new String(a);</td> <td></td> </tr> <tr> <td><a name="translate-case" id="translate-case"></a><a href="cpp#translate-case-note">translate case</a></td> <td>#include <algorithm><br /> <br /> string s("foo");<br /> <br /> <span style="color: gray"><span style="white-space: pre-wrap;">//</span> in place:</span><br /> transform(s.begin(), s.end(),<br /> <span style="white-space: pre-wrap;"> </span><span style="white-space: pre-wrap;"> </span><span style="white-space: pre-wrap;"> </span><span style="white-space: pre-wrap;"> </span><span style="white-space: pre-wrap;"> </span>s.begin(), ::toupper);<br /> transform(s.begin(), s.end(),<br /> <span style="white-space: pre-wrap;"> </span><span style="white-space: pre-wrap;"> </span><span style="white-space: pre-wrap;"> </span><span style="white-space: pre-wrap;"> </span><span style="white-space: pre-wrap;"> </span>s.begin(), ::tolower);<br /> <br /> <span style="color: gray"><span style="white-space: pre-wrap;">//</span> non-destructive:</span><br /> string s2;<br /> s2.resize(s.size();<br /> transform(s.begin(), s.end(),<br /> <span style="white-space: pre-wrap;"> </span><span style="white-space: pre-wrap;"> </span><span style="white-space: pre-wrap;"> </span><span style="white-space: pre-wrap;"> </span><span style="white-space: pre-wrap;"> </span>s2.begin(), ::toupper);</td> <td>[@"HELLO" lowercaseString]</td> <td>"hello".toUpperCase()<br /> "HELLO".toLowerCase()</td> <td>"hello".ToUpper()<br /> HELLO".ToLower()</td> </tr> <tr> <td><a name="trim" id="trim"></a><a href="cpp#trim-note">trim</a></td> <td>#include <algorithm><br /> <br /> string s(" hello ");<br /> <span style="color: gray"><span style="white-space: pre-wrap;">//</span> trim in place on left:</span><br /> s.erase(<br /> <span style="white-space: pre-wrap;"> </span>s.begin(),<br /> <span style="white-space: pre-wrap;"> </span>find_if(<br /> <span style="white-space: pre-wrap;"> </span><span style="white-space: pre-wrap;"> </span>s.begin(),<br /> <span style="white-space: pre-wrap;"> </span><span style="white-space: pre-wrap;"> </span>s.end(),<br /> <span style="white-space: pre-wrap;"> </span><span style="white-space: pre-wrap;"> </span>not1(ptr_fun<int, int>(isspace))<br /> <span style="white-space: pre-wrap;"> </span>)<br /> );<br /> <br /> <span style="color: gray"><span style="white-space: pre-wrap;">//</span> trim in place on right:</span><br /> s.erase(<br /> <span style="white-space: pre-wrap;"> </span>find_if(<br /> <span style="white-space: pre-wrap;"> </span><span style="white-space: pre-wrap;"> </span>s.rbegin(),<br /> <span style="white-space: pre-wrap;"> </span><span style="white-space: pre-wrap;"> </span>s.rend(),<br /> <span style="white-space: pre-wrap;"> </span><span style="white-space: pre-wrap;"> </span>not1(ptr_fun<int, int>(isspace))<br /> <span style="white-space: pre-wrap;"> </span>).base(),<br /> <span style="white-space: pre-wrap;"> </span>s.end()<br /> );</td> <td>[@" hello " stringByTrimmingCharactersInSet: [NSCharacterSet whitespaceCharacterSet]]</td> <td>" hello ".trim()</td> <td>" hello ".Trim()</td> </tr> <tr> <td><a name="pad" id="pad"></a><a href="cpp#pad-note">pad</a><br /> <span style="color: gray"><em>on right, on left</em></span></td> <td>#include <iomanip><br /> #include <sstream><br /> <br /> string s("hello");<br /> string rpad(s);<br /> rpad += string(10 - s.length(), ' ');<br /> <br /> ostringstream oss;<br /> oss <span style="white-space: pre-wrap;"><<</span> setw(10) <span style="white-space: pre-wrap;"><<</span> s;<br /> string lpad(oss.str());</td> <td>[@"hello" stringByPaddingToLength:10 withString:@" " startingAtIndex:0]</td> <td></td> <td>"hello".PadLeft(10)</td> </tr> <tr> <td><a name="num-to-str" id="num-to-str"></a><a href="cpp#num-to-str-note">number to string</a></td> <td>char buf[100];<br /> long n = 123;<br /> <br /> sprintf(buf, "%ld", n);<br /> <span style="color: gray">/* prevent buffer overflow: */</span><br /> snprintf(buf, 100, "%ld", n);</td> <td></td> <td>Integer.toString(14)<br /> Long.toString(14)<br /> Double.toString(14.7)</td> <td>14.ToString()<br /> 14.7.ToString()</td> </tr> <tr> <td><a name="str-to-num" id="str-to-num"></a><a href="cpp#str-to-num-note">string to number</a></td> <td>#include <sstream><br /> <br /> stringstream ss("7 14.3 12");<br /> int n1;<br /> double x;<br /> long n2;<br /> <br /> ss <span style="white-space: pre-wrap;">>></span> n1 <span style="white-space: pre-wrap;">>></span> x <span style="white-space: pre-wrap;">>></span> n2;</td> <td>[@"14" integerValue]<br /> [@"14" longLongvalue]<br /> [@"14.7" floatValue]<br /> [@"14.7" doubleValue]</td> <td>Byte.parseByte("14")<br /> Short.parseShort("14")<br /> Integer.parseInt("14")<br /> Long.parseLong("14")<br /> Float.parseFloat("14.7")<br /> Double.parseDouble("14.7")</td> <td>byte.Parse("14")<br /> short.Parse("14")<br /> int.Parse("14")<br /> long.Parse("14")<br /> float.Parse("14")<br /> double.Parse("14")<br /> decimal.Parse("14")</td> </tr> <tr> <td><a name="join" id="join"></a><a href="cpp#join-note">join</a><br /> <span style="white-space: pre-wrap;"> </span></td> <td></td> <td></td> <td></td> <td>System.String.Join(", ", names)</td> </tr> <tr> <td><a name="split" id="split"></a><a href="cpp#split-note">split</a></td> <td></td> <td>[@"Bob Ned Amy" componentsSeparatedByString:@" "]</td> <td>"Bob Ned Amy".split(" ")</td> <td>string[] names = "Bob Ned Amy".Split(' ');</td> </tr> <tr> <td><a name="serialize" id="serialize"></a><a href="cpp#serialize-note">serialize</a></td> <td></td> <td></td> <td></td> <td></td> </tr> <tr> <td><a name="str-length" id="str-length"></a><a href="cpp#str-length-note">string length</a><br /> <span style="white-space: pre-wrap;"> </span></td> <td>string s("hello");<br /> size_t len = s.length();</td> <td>[s length]</td> <td>s.length()</td> <td>s.Length</td> </tr> <tr> <td><a name="index-substr" id="index-substr"></a><a href="cpp#index-substr-note">index of substring</a><br /> <span style="white-space: pre-wrap;"> </span></td> <td>string("hello").find("ll")</td> <td>[@"hello" rangeOfString:@"ll"].location</td> <td>"hello".indexOf("ll")</td> <td>"hello".IndexOf("ll")</td> </tr> <tr> <td><a name="extract-substr" id="extract-substr"></a><a href="cpp#extract-substr-note">extract substring</a></td> <td>string("hello").substr(2, 2)</td> <td>[@"hello" substringWithRange:NSMakeRange(2, 2)]</td> <td>"hello".substring(2,4)</td> <td>"hello".Substring(2, 2)</td> </tr> <tr> <td><a name="char-type" id="char-type"></a><a href="cpp#char-type-note">character type</a></td> <td>char<br /> wchar_t</td> <td></td> <td>char<br /> Character</td> <td>char<br /> Char</td> </tr> <tr> <td><a name="char-literal" id="char-literal"></a><a href="cpp#char-literal-note">character literal</a></td> <td>char n = 'X';</td> <td></td> <td>char n = 'X';</td> <td>char n = 'X';</td> </tr> <tr> <td><a name="test-char" id="test-char"></a><a href="cpp#test-char-note">test character</a><br /> <span style="color: gray"><em>letter, digit, whitespace, uppercase letter, lowercase letter</em></span></td> <td><span style="color: gray"><span style="white-space: pre-wrap;">//</span> functions have this signature:<br /> <span style="white-space: pre-wrap;">//</span><br /> <span style="white-space: pre-wrap;">//</span> <span style="white-space: pre-wrap;"> </span>int (*)(int):<br /> <span style="white-space: pre-wrap;">//</span></span><br /> isalpha<br /> isdigit<br /> isspace<br /> isupper<br /> islower</td> <td></td> <td></td> <td>System.Char.IsLetter(Char)<br /> System.Char.IsNumber(Char)<br /> System.Char.IsWhiteSpace(Char)<br /> System.Char.IsUpper(Char)<br /> System.Char.IsLower(Char)</td> </tr> <tr> <th colspan="5"><a name="regexes" id="regexes"></a><a href="cpp#regexes-note">regular expressions</a></th> </tr> <tr> <th></th> <th>c++</th> <th>objective c</th> <th>java</th> <th>c#</th> </tr> <tr> <td><a name="regex-type" id="regex-type"></a><a href="cpp#regex-type-note">regex type</a></td> <td>regex<br /> wregex</td> <td></td> <td></td> <td></td> </tr> <tr> <td><a name="char-class-abbrev" id="char-class-abbrev"></a><a href="cpp#char-class-abbrev-note">character class abbreviations</a></td> <td>. \d \D \s \S \w \W</td> <td></td> <td></td> <td></td> </tr> <tr> <td><a name="regex-anchors" id="regex-anchors"></a><a href="cpp#regex-anchors-note">anchors</a></td> <td>^ $ \b \B</td> <td></td> <td></td> <td></td> </tr> <tr> <td><a name="regex-lookahead" id="regex-lookahead"></a><a href="cpp#regex-lookahead-note">lookahead</a><br /> <span style="color: gray"><em>positive, negative</em></span></td> <td>(?=<span style="color: gray"><em>subpattern</em></span>)<br /> (?!<span style="color: gray"><em>subpattern</em></span>)</td> <td></td> <td></td> <td></td> </tr> <tr> <td><a name="regex-test" id="regex-test"></a><a href="cpp#regex-test-note">match test</a></td> <td>#include <regex><br /> <br /> regex rx(".*ll.*");<br /> bool match = regex_match("hello", rx);</td> <td>NSPredicate *pred = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", @".*ll.*"];<br /> BOOL is_match = [pred evaluateWithObject:@"hello"];</td> <td>boolean isMatch = "hello".matches(".*ll.*");</td> <td>using System.Text.RegularExpressions;<br /> Regex regex = new Regex("ll");<br /> bool isMatch = regex.IsMatch("hello");</td> </tr> <tr> <td><a name="case-insensitive-regex" id="case-insensitive-regex"></a><a href="cpp#case-insensitive-regex-note">case insensitive match test</a></td> <td>#include <regex><br /> <br /> regex rx("lorem", icase);<br /> bool match = regex_match("Lorem", rx);</td> <td></td> <td></td> <td></td> </tr> <tr> <td><a name="regex-modifiers" id="regex-modifiers"></a><a href="cpp#regex-modifiers-note">modifiers</a></td> <td></td> <td></td> <td></td> <td></td> </tr> <tr> <td><a name="subst" id="subst"></a><a href="cpp#subst-note">substitution</a></td> <td></td> <td></td> <td>String s1 = "hello".replace("ll","LL");<br /> String s2 = "hello".replaceAll("l","L");</td> <td>using System.Text.RegularExpressions;<br /> Regex r1 = new Regex("ll");<br /> String s1 = r1.Replace("hello", "LL", 1);<br /> Regex r2 = new Regex("l");<br /> String s2 = r2.Replace("hello", "L");</td> </tr> <tr> <td><a name="match-prematch-postmatch" id="match-prematch-postmatch"></a><a href="cpp#match-prematch-postmatch-note">match, prematch, postmatch</a></td> <td></td> <td></td> <td></td> <td></td> </tr> <tr> <td><a name="group-capture" id="group-capture"></a><a href="cpp#group-capture-note">group capture</a></td> <td></td> <td></td> <td></td> <td></td> </tr> <tr> <th colspan="5"><a name="dates-time" id="dates-time"></a><a href="cpp#dates-time-note">dates and time</a></th> </tr> <tr> <th></th> <th>c++</th> <th>objective c</th> <th>java</th> <th>c#</th> </tr> <tr> <td><a name="date-time-type" id="date-time-type"></a><a href="cpp#date-time-type-note">date and time type</a><br /> <span style="white-space: pre-wrap;"> </span></td> <td></td> <td></td> <td>java.util.Date</td> <td>System.DateTime</td> </tr> <tr> <td><a name="current-date-time" id="current-date-time"></a><a href="cpp#current-date-time-note">current date and time</a></td> <td></td> <td></td> <td>import java.util.Date;<br /> <br /> long millis = System.currentTimeMillis();<br /> Date dt = new Date(millis);</td> <td>using System;<br /> <br /> DateTime dt = DateTime.Now;</td> </tr> <tr> <td><a name="unix-epoch" id="unix-epoch"></a><a href="cpp#unix-epoch-note">to unix epoch, from unix epoch</a></td> <td></td> <td></td> <td>long epoch = dt.getTime() / 1000;<br /> <br /> Date dt2 = new Date(epoch * 1000);</td> <td>using System;<br /> <br /> long tenM = 10 * 1000 * 1000;<br /> long sec = dt.ToFileTimeUtc() / tenM;<br /> long epoch = sec - 11644473600;<br /> <br /> long ft = (epoch + 11644473600) * tenM;<br /> DateTime dt2 = DateTime.FromFileTimeUtc(ft);</td> </tr> <tr> <td><a name="date-time-to-str" id="date-time-to-str"></a><a href="cpp#date-time-to-str-note">date and time to string</a></td> <td></td> <td></td> <td>dt.toString()</td> <td>dt.ToString()</td> </tr> <tr> <td><a name="format-date" id="format-date"></a><a href="cpp#format-date-note">format date</a></td> <td></td> <td></td> <td>String s = "yyyy-MM-dd HH:mm:ss";<br /> DateFormat fmt = new SimpleDateFormat(s);<br /> String s2 = fmt.format(dt);</td> <td>String s = "yyyy-MM-dd HH:mm:ss");<br /> String s2 = dt.ToString(s);</td> </tr> <tr> <td><a name="parse-date" id="parse-date"></a><a href="cpp#parse-date-note">parse date</a></td> <td></td> <td></td> <td>String s = "2011-05-03 17:00:00";<br /> Date dt2 = fmt.parse(s);</td> <td>using System;<br /> using System.Globalization;<br /> <br /> CultureInfo enUS =<br /> <span style="white-space: pre-wrap;"> </span>new CultureInfo("en-US");<br /> <br /> DateTime dt2 = DateTime.ParseExact(<br /> <span style="white-space: pre-wrap;"> </span>"2011-05-03 17:00:00",<br /> <span style="white-space: pre-wrap;"> </span>"yyyy-MM-dd HH:mm:ss",<br /> <span style="white-space: pre-wrap;"> </span>enUS);</td> </tr> <tr> <td><a name="date-subtraction" id="date-subtraction"></a><a href="cpp#date-subtraction-note">date subtraction</a></td> <td></td> <td></td> <td><span style="color: gray"><em>difference in milliseconds as a long:</em></span><br /> dt2.getTime() - dt.getTime()</td> <td></td> </tr> <tr> <td><a name="add-duration" id="add-duration"></a><a href="cpp#add-duration-note">add duration</a></td> <td></td> <td></td> <td>long day_ms = 24 * 3600 * 1000;<br /> Date dt = new Date(dt.getTime() + day_ms));</td> <td></td> </tr> <tr> <td><a name="date-parts" id="date-parts"></a><a href="cpp#date-parts-note">date parts</a></td> <td></td> <td></td> <td>import java.util.Date;<br /> import java.util.Calendar;<br /> import java.util.GregorianCalendar;<br /> <br /> Date dt = new Date();<br /> GregorianCalendar cal = new GregorianCalendar();<br /> cal.setTime(dt);<br /> <br /> cal.get(Calendar.YEAR)<br /> cal.get(Calendar.MONTH) + 1<br /> cal.get(Calendar.DAY_OF_MONTH)</td> <td></td> </tr> <tr> <td><a name="time-parts" id="time-parts"></a><a href="cpp#time-parts-note">time parts</a></td> <td></td> <td></td> <td>import java.util.Date;<br /> import java.util.Calendar;<br /> import java.util.GregorianCalendar;<br /> <br /> Date dt = new Date();<br /> GregorianCalendar cal = new GregorianCalendar();<br /> cal.setTime(dt);<br /> <br /> cal.get(Calendar.HOUR_OF_DAY)<br /> cal.get(Calendar.MINUTE)<br /> cal.get(Calendar.SECOND)</td> <td></td> </tr> <tr> <td><a name="build-datetime" id="build-datetime"></a><a href="cpp#build-datetime-note">build broken-down datetime</a></td> <td></td> <td></td> <td>import java.util.GregorianCalendar;<br /> <br /> int yr = 2015, mo = 5, dy = 31;<br /> int hr = 9, mi = 0, ss = 0;<br /> GregorianCalendar cal =<br /> <span style="white-space: pre-wrap;"> </span>new GregorianCalendar(yr, mo - 1, dy, hr, mi, ss);<br /> Date dt = cal.getTime();</td> <td></td> </tr> <tr> <th colspan="5"><a name="fixed-length-arrays" id="fixed-length-arrays"></a><a href="cpp#fixed-length-arrays-note">fixed-length arrays</a></th> </tr> <tr> <th></th> <th>c++</th> <th>objective c</th> <th>java</th> <th>c#</th> </tr> <tr> <td><a name="fixed-len-array-stack" id="fixed-len-array-stack"></a><a href="cpp#fixed-len-array-stack-note">declare on stack</a></td> <td>int a[10];</td> <td>int a[10];</td> <td><span style="color: gray"><em>arrays must be allocated on heap</em></span></td> <td><span style="color: gray"><em>arrays must be allocated on heap</em></span></td> </tr> <tr> <td><a name="fixed-len-array-heap" id="fixed-len-array-heap"></a><a href="cpp#fixed-len-array-heap-note">declare on heap</a></td> <td>int* a = new int[10];</td> <td>#include <stdlib.h><br /> int *a = calloc(10, sizeof(int));</td> <td>int[] a = new int[10];</td> <td>int[] a = new int[10];</td> </tr> <tr> <td><a name="free-fixed-len-array-heap" id="free-fixed-len-array-heap"></a><a href="cpp#free-fixed-len-array-heap-note">free heap</a></td> <td>delete[] a;</td> <td>#include <stdlib.h><br /> free(a);</td> <td><span style="color: gray"><em>garbage collected</em></span></td> <td><span style="color: gray"><em>garbage collected</em></span></td> </tr> <tr> <td><a name="fixed-len-array-init-list" id="fixed-len-array-init-list"></a><a href="cpp#fixed-len-array-init-list-note">initialization list</a></td> <td>int a[] = {1, 2, 3};</td> <td>NSArray *a = [NSArray arrayWithObjects:@"hello", @"goodbye", nil];</td> <td>int[] a = {1,2,3};</td> <td>int[] a = {1,2,3};</td> </tr> <tr> <td><a name="fixed-len-array-size" id="fixed-len-array-size"></a><a href="cpp#fixed-len-array-size-note">size</a></td> <td>int a[10];<br /> <br /> <span style="color: gray"><span style="white-space: pre-wrap;">//</span> stack arrays only:</span><br /> size_t len = sizeof(a) / sizeof(a[0]);</td> <td>[a count]</td> <td>a.length</td> <td>a.Length</td> </tr> <tr> <td><a name="fixed-len-array-lookup" id="fixed-len-array-lookup"></a><a href="cpp#fixed-len-array-lookup-note">lookup</a><br /> <span style="white-space: pre-wrap;"> </span></td> <td>int first = a[0];</td> <td>[a objectAtIndex:0]</td> <td>a[0]</td> <td>a[0]</td> </tr> <tr> <td><a name="fixed-len-array-update" id="fixed-len-array-update"></a><a href="cpp#fixed-len-array-update-note">update</a><br /> <span style="white-space: pre-wrap;"> </span></td> <td>a[0] = 7;</td> <td></td> <td></td> <td></td> </tr> <tr> <td><a name="fixed-len-array-out-of-bounds" id="fixed-len-array-out-of-bounds"></a><a href="cpp#fixed-len-array-out-of-bounds-note">out-of-bounds</a></td> <td><span style="color: gray"><em>No defined behavior</em></span><br /> <br /> <span style="color: gray"><em>An out-of-bounds lookup may return the value the memory location contains; an out-of-bounds update may cause memory corruption. The system may detect an invalid address and send the process a</em> SIGSEGV.</span></td> <td><span style="color: gray"><em>raises</em></span> NSRangeException exception</td> <td>ArrayIndexOutOfBoundsException</td> <td>IndexOutOfRangeException</td> </tr> <tr> <td><a name="copy-fixed-len-array" id="copy-fixed-len-array"></a><a href="cpp#copy-fixed-len-array-note">copy</a></td> <td>const size_t LEN(4);<br /> int src[LEN] = {3, 2, 4, 1};<br /> int dest[LEN];<br /> <br /> <span style="color: gray"><span style="white-space: pre-wrap;">//</span> 3rd arg is number of bytes to copy:</span><br /> memcpy(dest, src, LEN * sizeof(src[0]));</td> <td></td> <td></td> <td></td> </tr> <tr> <td><a name="fixed-len-array-as-func-arg" id="fixed-len-array-as-func-arg"></a><a href="cpp#fixed-len-array-as-func-arg-note">as function argument</a></td> <td>void<br /> reverse(int* a, size_t len) {<br /> <span style="white-space: pre-wrap;"> </span>for (int i = 0; i < len / 2; ++i) {<br /> <span style="white-space: pre-wrap;"> </span><span style="white-space: pre-wrap;"> </span>int tmp = a[len - i - 1];<br /> <span style="white-space: pre-wrap;"> </span><span style="white-space: pre-wrap;"> </span>a[len - i - 1] = a[i];<br /> <span style="white-space: pre-wrap;"> </span><span style="white-space: pre-wrap;"> </span>a[i] = tmp;<br /> <span style="white-space: pre-wrap;"> </span>}<br /> }<br /> <br /> const size_t LEN(4);<br /> int a[LEN] = {3, 2, 4, 1};<br /> reverse(a, LEN);</td> <td></td> <td></td> <td></td> </tr> <tr> <td><a name="iterate-over-fixed-len-array" id="iterate-over-fixed-len-array"></a><a href="cpp#iterate-over-fixed-len-array-note">iterate</a></td> <td>const size_t LEN(4);<br /> int a[LEN] = {3, 2, 4, 1};<br /> <br /> for (int i = 0; i < LEN; ++i) {<br /> <span style="white-space: pre-wrap;"> </span>cout <span style="white-space: pre-wrap;"><<</span> "value at " <span style="white-space: pre-wrap;"><<</span> i <span style="white-space: pre-wrap;"><<</span> " is "<br /> <span style="white-space: pre-wrap;"> </span><span style="white-space: pre-wrap;"> </span><span style="white-space: pre-wrap;"> </span><span style="white-space: pre-wrap;"> </span><span style="white-space: pre-wrap;"><<</span> a[i] <span style="white-space: pre-wrap;"><<</span> endl;<br /> }</td> <td>NSEnumerator *i = [a objectEnumerator];<br /> id o;<br /> while (o = [i nextObject]) {<br /> <span style="white-space: pre-wrap;"> </span><span style="color: gray"><em>do something with o</em></span><br /> }</td> <td>for (String name : names) {</td> <td>foreach (string name in names) {</td> </tr> <tr> <td><a name="sort-fixed-len-array" id="sort-fixed-len-array"></a><a href="cpp#sort-fixed-len-array-note">sort</a></td> <td>#include <cstdlib><br /> <br /> int<br /> comp(const void* ap, const void* bp) {<br /> <span style="white-space: pre-wrap;"> </span>int a = *(int*)ap;<br /> <span style="white-space: pre-wrap;"> </span>int b = *(int*)bp;<br /> <span style="white-space: pre-wrap;"> </span>return a < b ? -1 : (a == b ? 0 : 1);<br /> }<br /> <br /> const size_t LEN(4);<br /> int a[LEN] = {3, 2, 1, 4};<br /> <br /> qsort(a, LEN, sizeof(a[0]), &comp);</td> <td></td> <td></td> <td></td> </tr> <tr> <th colspan="5"><a name="resizable-arrays" id="resizable-arrays"></a><a href="cpp#resizable-arrays-note">resizable arrays</a></th> </tr> <tr> <th></th> <th>c++</th> <th>objective c</th> <th>java</th> <th>c#</th> </tr> <tr> <td><a name="decl-resizable-array" id="decl-resizable-array"></a><a href="cpp#decl-resizable-array-note">declare</a></td> <td>#include <vector><br /> <br /> vector <int> a;</td> <td>NSMutableArray *a = [NSMutableArray arrayWithCapacity:10];</td> <td>java.util.Vector<String> vec = new java.util.Vector<String>();</td> <td>using System.Collections.Generic;<br /> List<string> l = new List<string>();</td> </tr> <tr> <td><a name="resizable-array-init-list" id="resizable-array-init-list"></a><a href="cpp#resizable-array-init-list-note">initialization list</a></td> <td>#include <vector><br /> <br /> vector<int> a = {1, 2, 3};<br /> vector<int> a2({7, 8, 9});</td> <td></td> <td></td> <td></td> </tr> <tr> <td><a name="resizable-array-size" id="resizable-array-size"></a><a href="cpp#resizable-array-size-note">size</a><br /> <span style="white-space: pre-wrap;"> </span></td> <td>size_t len = a.size();</td> <td>[a count]</td> <td>vec.size()</td> <td>l.Count</td> </tr> <tr> <td><a name="resizable-array-capacity" id="resizable-array-capacity"></a><a href="cpp#resizable-array-capacity-note">capacity</a><br /> <span style="color: gray"><em>get, increase</em></span></td> <td>size_t cap = a.capacity();<br /> <br /> <span style="color: gray"><span style="white-space: pre-wrap;">//</span> will not decrease capacity:</span><br /> a.reserve(10);</td> <td></td> <td></td> <td></td> </tr> <tr> <td><a name="resizable-array-empty-test" id="resizable-array-empty-test"></a><a href="cpp#resizable-array-empty-test-note">empty test</a><br /> <span style="color: gray"><em>and clear</em></span></td> <td>bool is_empty = a.empty();<br /> a.clear();</td> <td></td> <td></td> <td></td> </tr> <tr> <td><a name="resizable-array-lookup" id="resizable-array-lookup"></a><a href="cpp#resizable-array-lookup-note">lookup</a></td> <td>int n = a[0];<br /> <br /> <span style="color: gray"><span style="white-space: pre-wrap;">//</span> can raise out_of_range:</span><br /> int n2 = a.at(0);</td> <td>[a objectAtIndex:0]</td> <td>vec.elementAt(0)</td> <td>l[0]</td> </tr> <tr> <td><a name="resizable-array-update" id="resizable-array-update"></a><a href="cpp#resizable-array-update-note">update</a><br /> <span style="white-space: pre-wrap;"> </span></td> <td>a[2] = 4;</td> <td></td> <td></td> <td></td> </tr> <tr> <td><a name="resizable-array-out-of-bounds" id="resizable-array-out-of-bounds"></a><a href="cpp#resizable-array-out-of-bounds-note">out-of-bounds behavior</a></td> <td><span style="color: gray"><em>using [] with out-of-bounds index has undefined behavior</em></span></td> <td><span style="color: gray"><em>raises</em></span> NSRangeException</td> <td><span style="color: gray"><em>throws</em></span> ArrayIndexOutOfBoundsException</td> <td><span style="color: gray"><em>throws</em></span> System.ArgumentOutOfRangeException</td> </tr> <tr> <td><a name="resizable-array-elem-index" id="resizable-array-elem-index"></a><a href="cpp#resizable-array-elem-index-note">element index</a></td> <td>#include <vector><br /> <br /> vector<int> a({6, 7, 8, 9});<br /> <br /> auto iter = find(a.cbegin(), a.cend(), 8);<br /> if (iter != a.cend()) {<br /> <span style="white-space: pre-wrap;"> </span>size_t pos = *iter;<br /> }</td> <td></td> <td></td> <td></td> </tr> <tr> <td><a name="slice-resizable-array" id="slice-resizable-array"></a><a href="cpp#slice-resizable-array-note">slice</a></td> <td>#include <vector><br /> <br /> vector<int> a({6, 7, 8, 9});<br /> <br /> <span style="color: gray"><span style="white-space: pre-wrap;">//</span> a2 contains {7, 8}:</span><br /> vector<int> a2(a.cbegin() + 1,<br /> <span style="white-space: pre-wrap;"> </span><span style="white-space: pre-wrap;"> </span><span style="white-space: pre-wrap;"> </span><span style="white-space: pre-wrap;"> </span><span style="white-space: pre-wrap;"> </span><span style="white-space: pre-wrap;"> </span><span style="white-space: pre-wrap;"> </span><span style="white-space: pre-wrap;"> </span>a.cbegin() + 3);</td> <td></td> <td></td> <td></td> </tr> <tr> <td><a name="slice-resizable-array-to-end" id="slice-resizable-array-to-end"></a><a href="cpp#slice-resizable-array-to-end-note">slice to end</a></td> <td>#include <vector><br /> <br /> vector<int> a({6, 7, 8, 9});<br /> <br /> <span style="color: gray"><span style="white-space: pre-wrap;">//</span> a2 contains {7, 8, 9}:</span><br /> vector<int> a2(a.cbegin() + 1, a.cend());</td> <td></td> <td></td> <td></td> </tr> <tr> <td><a name="resizable-array-back" id="resizable-array-back"></a><a href="cpp#resizable-array-back-note">manipulate back</a></td> <td>#include <vector><br /> <br /> vector<int> a({6, 7, 8});<br /> <br /> a.push_back(9);<br /> int elem = a.pop_back();</td> <td>[a addObject:@"hello"];<br /> [a removeLastObject];</td> <td>vec.add("hello");<br /> <span style="color: gray"><em>or</em></span><br /> vec.add(vec.size(), "hello");<br /> vec.removeElementAt(vec.size()-1);</td> <td>l.Add("hello");<br /> l.RemoveAt(l.Count - 1);</td> </tr> <tr> <td><a name="resizable-array-front" id="resizable-array-front"></a><a href="cpp#resizable-array-front-note">manipulate front</a></td> <td>#include <vector><br /> <br /> vector<int> a({6, 7, 8});<br /> <br /> <span style="color: gray"><span style="white-space: pre-wrap;">//</span> slower than manipulating back:</span><br /> a.insert(a.cbegin(), 5);<br /> int elem = a[0];<br /> a.erase(a.cbegin());</td> <td></td> <td></td> <td></td> </tr> <tr> <td><a name="concat-resizable-array" id="concat-resizable-array"></a><a href="cpp#concat-resizable-array-note">concatenate</a></td> <td>#include <vector><br /> <br /> vector<int> a1({1, 2, 3});<br /> vector<int> a2({4, 5, 6});<br /> <br /> a1.insert(a1.cend(),<br /> <span style="white-space: pre-wrap;"> </span><span style="white-space: pre-wrap;"> </span><span style="white-space: pre-wrap;"> </span><span style="white-space: pre-wrap;"> </span><span style="white-space: pre-wrap;"> </span>a2.cbegin(),<br /> <span style="white-space: pre-wrap;"> </span><span style="white-space: pre-wrap;"> </span><span style="white-space: pre-wrap;"> </span><span style="white-space: pre-wrap;"> </span><span style="white-space: pre-wrap;"> </span>a2.cend());</td> <td></td> <td></td> <td></td> </tr> <tr> <td><a name="replicate-resizable-array-elem" id="replicate-resizable-array-elem"></a><a href="cpp#replicate-resizable-array-elem-note">replicate element</a></td> <td>#include <vector><br /> <br /> <span style="color: gray"><span style="white-space: pre-wrap;">//</span> array of 10 zeros:</span><br /> vector<int> a(10, 0);</td> <td></td> <td></td> <td></td> </tr> <tr> <td><a name="copy-resizable-array" id="copy-resizable-array"></a><a href="cpp#copy-resizable-array-note">copy</a></td> <td>#include <vector><br /> <br /> vector<int> a({1, 2, 3});<br /> <span style="color: gray"><span style="white-space: pre-wrap;">//</span> copy constructor:</span><br /> vector<int> a2(a);<br /> vector<int> a3;<br /> <br /> <span style="color: gray"><span style="white-space: pre-wrap;">//</span> assignment performs copy:</span><br /> a3 = a;</td> <td></td> <td></td> <td></td> </tr> <tr> <td><a name="resizable-array-as-func-arg" id="resizable-array-as-func-arg"></a><a href="cpp#resizable-array-as-func-arg-note">array as function argument</a></td> <td><span style="color: gray"><em>use reference or pointer to avoid copying array</em></span></td> <td></td> <td></td> <td></td> </tr> <tr> <td><a name="iterate-over-resizable-array" id="iterate-over-resizable-array"></a><a href="cpp#iterate-over-resizable-array-note">iterate</a></td> <td>#include <vector><br /> <br /> int sum(0);<br /> vector<int> a({1, 2, 3});<br /> <br /> for (const auto& n: a) {<br /> <span style="white-space: pre-wrap;"> </span>sum += n;<br /> }</td> <td>NSEnumerator *i = [a objectEnumerator];<br /> id o;<br /> while (o = [i nextObject]) {<br /> <span style="white-space: pre-wrap;"> </span><span style="color: gray"><em>do something with o</em></span><br /> }</td> <td>for ( String s : vec ) {<br /> <span style="white-space: pre-wrap;"> </span><span style="color: gray"><em>do something with s</em></span><br /> }</td> <td>foreach ( string s in l ) {<br /> <span style="white-space: pre-wrap;"> </span><span style="color: gray"><em>do something with s</em></span><br /> }</td> </tr> <tr> <td><a name="indexed-array-iteration" id="indexed-array-iteration"></a><a href="cpp#indexed-array-iteration-note">iterate over elements and indices</a></td> <td>#include <vector><br /> <br /> vector<int> a({6, 7, 8});<br /> <br /> for (auto iter = a.cbegin();<br /> <span style="white-space: pre-wrap;"> </span><span style="white-space: pre-wrap;"> </span><span style="white-space: pre-wrap;"> </span>iter != a.cend();<br /> <span style="white-space: pre-wrap;"> </span><span style="white-space: pre-wrap;"> </span><span style="white-space: pre-wrap;"> </span>++iter) {<br /> <br /> <span style="white-space: pre-wrap;"> </span>cout <span style="white-space: pre-wrap;"><<</span> "value at " <span style="white-space: pre-wrap;"><<</span> iter - a.cbegin()<br /> <span style="white-space: pre-wrap;"> </span><span style="white-space: pre-wrap;"> </span><span style="white-space: pre-wrap;"> </span><span style="white-space: pre-wrap;"> </span><span style="white-space: pre-wrap;"><<</span> " is " <span style="white-space: pre-wrap;"><<</span> *iter <span style="white-space: pre-wrap;"><<</span> endl;<br /> }</td> <td></td> <td></td> <td></td> </tr> <tr> <td><a name="reverse-array" id="reverse-array"></a><a href="cpp#reverse-array-note">reverse</a></td> <td>#include <vector><br /> <br /> vector<int> a({1, 2, 3});<br /> vector<int> a2(a.crbegin(), a.crend());</td> <td></td> <td></td> <td></td> </tr> <tr> <td><a name="sort-array" id="sort-array"></a><a href="cpp#sort-array-note">sort</a></td> <td>#include <vector><br /> <br /> vector<int> a({3, 2, 4, 1});<br /> sort(a.begin(), a.end());</td> <td></td> <td></td> <td></td> </tr> <tr> <td><a name="dedupe-array" id="dedupe-array"></a><a href="cpp#dedupe-array-note">dedupe</a></td> <td>#include <set><br /> #include <vector><br /> <br /> vector<int> a({1, 1, 2, 2, 3});<br /> set<int> tmp(a.cbegin(), a.cend());<br /> <span style="color: gray"><span style="white-space: pre-wrap;">//</span> often unnecessary since sets provide<br /> <span style="white-space: pre-wrap;">//</span> many of the same methods as vectors:</span><br /> vector<int> a2(tmp.cbegin(), tmp.cend());</td> <td></td> <td></td> <td></td> </tr> <tr> <td><a name="membership" id="membership"></a><a href="cpp#membership-note">membership</a></td> <td>#include <vector><br /> <br /> vector<int> a({1, 2, 3});<br /> if (find(a.cbegin(), a.cend(), 7) !=<br /> <span style="white-space: pre-wrap;"> </span><span style="white-space: pre-wrap;"> </span>a.cend()) {<br /> <br /> <span style="white-space: pre-wrap;"> </span>cout <span style="white-space: pre-wrap;"><<</span> "contains 7" <span style="white-space: pre-wrap;"><<</span> endl;<br /> }</td> <td></td> <td></td> <td></td> </tr> <tr> <th colspan="5"><a name="tuples" id="tuples"></a><a href="cpp#tuples-note">tuples</a></th> </tr> <tr> <th></th> <th>c++</th> <th>objective c</th> <th>java</th> <th>c#</th> </tr> <tr> <td><a name="tuple-ctor" id="tuple-ctor"></a><a href="cpp#tuple-ctor-note">constructor</a></td> <td>tuple<string, int, float> tup("foo", 1, 3.7);<br /> <br /> <span style="color: gray"><span style="white-space: pre-wrap;">//</span> invokes default constructors for elements:</span><br /> tuple<string, int, float> tup2;<br /> <br /> <span style="color: gray"><span style="white-space: pre-wrap;">//</span> element types are inferred:</span><br /> auto tup3 = make_tuple("foo", 1, 3.7);</td> <td></td> <td></td> <td></td> </tr> <tr> <td><a name="tuple-lookup" id="tuple-lookup"></a><a href="cpp#tuple-lookup-note">lookup</a></td> <td>tuple<string, int, float> tup("foo", 1, 3.7);<br /> <br /> string s = get<0>(tup);<br /> int i = get<1>(tup);<br /> float x = get<2>(tup);</td> <td></td> <td></td> <td></td> </tr> <tr> <td><a name="tuple-decompose" id="tuple-decompose"></a><a href="cpp#tuple-decompose-note">decompose</a></td> <td>tuple<string, int, float> tup("foo", 1, 3.7);<br /> string s;<br /> float x;<br /> <br /> tie(s, ignore, x) = tup;</td> <td></td> <td></td> <td></td> </tr> <tr> <td><a name="tuple-update" id="tuple-update"></a><a href="cpp#tuple-update-note">update</a><br /> <span style="white-space: pre-wrap;"> </span></td> <td>get<0>(tup) = "bar";</td> <td></td> <td></td> <td></td> </tr> <tr> <td><a name="tuple-len" id="tuple-len"></a><a href="cpp#tuple-len-note">length</a><br /> <span style="white-space: pre-wrap;"> </span></td> <td>tuple_size<decltype(tup)>::value</td> <td></td> <td></td> <td></td> </tr> <tr> <td><a name="pair-ctor" id="pair-ctor"></a><a href="cpp#pair-ctor-note">pair constructor</a></td> <td>pair <string, int> p2("foo", 7);<br /> <br /> <span style="color: gray"><span style="white-space: pre-wrap;">//</span> invokes default constructors for elements:</span><br /> pair <string, int> p1;<br /> <br /> <span style="color: gray"><span style="white-space: pre-wrap;">//</span> element types are inferred:</span><br /> auto p3 = make_pair("foo", 7);</td> <td></td> <td></td> <td>using System.Collections.Generic;<br /> KeyValuePair<string,int> pr = new KeyValuePair<string,int>("hello",5);<br /> System.Console.WriteLine("{0} {1}", pr.Key, pr.Value);</td> </tr> <tr> <td><a name="pair-lookup" id="pair-lookup"></a><a href="cpp#pair-lookup-note">pair lookup</a></td> <td>auto p = make_pair("foo", 7);<br /> <br /> string s = p.first;<br /> int i = p.second;</td> <td></td> <td></td> <td></td> </tr> <tr> <td><a name="pair-update" id="pair-update"></a><a href="cpp#pair-update-note">pair update</a></td> <td>p.first = "bar";<br /> p.second = 8;</td> <td></td> <td></td> <td></td> </tr> <tr> <th colspan="5"><a name="dictionaries" id="dictionaries"></a><a href="cpp#dictionaries-note">dictionaries</a></th> </tr> <tr> <th></th> <th>c++</th> <th>objective c</th> <th>java</th> <th>c#</th> </tr> <tr> <td><a name="dict-ctor" id="dict-ctor"></a><a href="cpp#dict-ctor-note">constructor</a></td> <td>#include <map><br /> <br /> map<string, int> m;</td> <td>NSMutableDictionary *dict = [NSMutableDictionary dictionaryWithCapacity:10];</td> <td>java.util.TreeMap<String, Integer> m = new java.util.TreeMap<String, Integer>();</td> <td>using System.Collections.Generic;<br /> Dictionary<string, int> dict = new Dictionary<string, int>();</td> </tr> <tr> <td><a name="dict-lookup" id="dict-lookup"></a><a href="cpp#dict-lookup-note">lookup</a></td> <td>m["hello"] = 5;<br /> cout <span style="white-space: pre-wrap;"><<</span> m["hello"] <span style="white-space: pre-wrap;"><<</span> endl;</td> <td>[dict setObject:@"5" forKey:@"hello"];<br /> [dict objectForKey:@"hello"]</td> <td>m.put("hello", 5);<br /> m.get("hello")</td> <td>dict.Add("hello", 5);<br /> dict["hello"]</td> </tr> <tr> <td><a name="dict-size" id="dict-size"></a><a href="cpp#dict-size-note">size</a><br /> <span style="white-space: pre-wrap;"> </span></td> <td>m.size()</td> <td>[dict count]</td> <td>m.size()</td> <td>dict.Count</td> </tr> <tr> <td><a name="dict-delete" id="dict-delete"></a><a href="cpp#dict-delete-note">delete</a><br /> <span style="white-space: pre-wrap;"> </span></td> <td>m.erase(m.find("hello"));</td> <td>[dict removeObjectForKey:@"hello"];</td> <td>m.remove("hello");</td> <td>dict.Remove("hello");</td> </tr> <tr> <td><a name="dict-missing-key" id="dict-missing-key"></a><a href="cpp#dict-missing-key-note">missing key behavior</a></td> <td><span style="color: gray"><em>returns element created by default constructor of value type</em></span></td> <td>NULL</td> <td>null</td> <td><span style="color: gray"><em>throws</em></span> KeyNotFoundException<br /> <span style="color: gray"><em>in</em> System.Collections.Generic</span></td> </tr> <tr> <td><a name="dict-iter" id="dict-iter"></a><a href="cpp#dict-iter-note">iterate</a></td> <td>map<string,int>::iterator mi;<br /> for (mi = m.begin(); mi != m.end(); ++mi) {<br /> <span style="white-space: pre-wrap;"> </span>printf("%s %d", mi->first, mi->second)<br /> }</td> <td>NSEnumerator *i = [dict keyEnumerator];<br /> id key;<br /> while ((key = [i nextObject])) {<br /> <span style="white-space: pre-wrap;"> </span><span style="color: gray"><em>do something with key</em></span><br /> }</td> <td>for ( java.util.Map.Entry<String, Integer> e : m.entrySet() ) {<br /> <span style="white-space: pre-wrap;"> </span><span style="color: gray"><em>use e.getKey() or e.getValue()</em></span><br /> }</td> <td>foreach ( KeyValuePair<string,int> e in dict) {<br /> <span style="white-space: pre-wrap;"> </span><span style="color: gray"><em>use e.Key and e.Value</em></span><br /> }</td> </tr> <tr> <th colspan="5"><a name="functions" id="functions"></a><a href="cpp#functions-note">functions</a></th> </tr> <tr> <th></th> <th>c++</th> <th>objective c</th> <th>java</th> <th>c#</th> </tr> <tr> <td><a name="decl-func" id="decl-func"></a><a href="cpp#decl-func-note">declare</a></td> <td><span style="color: gray"><span style="white-space: pre-wrap;">//</span> parameter names are optional:</span><br /> int<br /> add(int m, int n);</td> <td></td> <td></td> <td></td> </tr> <tr> <td><a name="def-func" id="def-func"></a><a href="cpp#def-func-note">define</a></td> <td>int<br /> add(int m, int n) {<br /> <span style="white-space: pre-wrap;"> </span>return m + n;<br /> }</td> <td></td> <td></td> <td></td> </tr> <tr> <td><a name="call-func" id="call-func"></a><a href="cpp#call-func-note">call</a><br /> <span style="white-space: pre-wrap;"> </span></td> <td>int sum = add(3, 7);</td> <td></td> <td></td> <td></td> </tr> <tr> <td><a name="def-static-class-method" id="def-static-class-method"></a><a href="cpp#def-static-class-method-note">define static class method</a></td> <td><span style="color: gray"><span style="white-space: pre-wrap;">//</span> Ops.h:</span><br /> class Ops {<br /> public:<br /> <span style="white-space: pre-wrap;"> </span>static int add(int m, int n);<br /> };<br /> <br /> <span style="color: gray"><span style="white-space: pre-wrap;">//</span> Ops.cpp:</span><br /> int Ops::add(int m, int n) {<br /> <span style="white-space: pre-wrap;"> </span>return m + n;<br /> }</td> <td></td> <td></td> <td></td> </tr> <tr> <td><a name="invoke-static-class-method" id="invoke-static-class-method"></a><a href="cpp#invoke-static-class-method-note">invoke static class method</a></td> <td>int sum = Ops::add(3, 7);<br /> <br /> <span style="color: gray"><span style="white-space: pre-wrap;">//</span> class name not needed<br /> <span style="white-space: pre-wrap;">//</span> inside class namespace:</span><br /> int sum = add(3, 7);</td> <td></td> <td></td> <td></td> </tr> <tr> <td><a name="overload-func" id="overload-func"></a><a href="cpp#overload-func-note">overload</a></td> <td>int add(int m, int n) {<br /> <span style="white-space: pre-wrap;"> </span>return m + n;<br /> }<br /> <br /> float add(float x, float y) {<br /> <span style="white-space: pre-wrap;"> </span>return x + y;<br /> }</td> <td><span style="color: gray"><em>method overloading only</em></span></td> <td><span style="color: gray"><em>yes</em></span></td> <td><span style="color: gray"><em>yes</em></span></td> </tr> <tr> <td><a name="default-arg" id="default-arg"></a><a href="cpp#default-arg-note">default argument</a></td> <td>#include <cmath><br /> <br /> float<br /> logarithm(float x, float base = 10.0) {<br /> <span style="white-space: pre-wrap;"> </span>return log(x) / log(base);<br /> }</td> <td><span style="color: gray"><em>none</em></span></td> <td><span style="color: gray"><em>use method overloading</em></span></td> <td><span style="color: gray"><em>use method overloading</em></span></td> </tr> <tr> <td><a name="variable-num-arg" id="variable-num-arg"></a><a href="cpp#variable-num-arg-note">variable number of arguments</a></td> <td></td> <td><span style="color: gray"><em>use C; use method overloading for finite arities</em></span></td> <td>public static String concat(String first, String… rest) {<br /> <span style="white-space: pre-wrap;"> </span>StringBuilder sb = new StringBuilder(first);<br /> <span style="white-space: pre-wrap;"> </span>for (String arg: rest) {<br /> <span style="white-space: pre-wrap;"> </span><span style="white-space: pre-wrap;"> </span>sb.append(arg);<br /> <span style="white-space: pre-wrap;"> </span>}<br /> <span style="white-space: pre-wrap;"> </span>return sb.toString();<br /> }<br /> String s = Concat.concat("Hello", ", ", "World", "!");</td> <td>public static string concat(params string[] args) {<br /> <span style="white-space: pre-wrap;"> </span>return System.String.Join("",args);<br /> }<br /> string s = Concat.concat("Hello", ", ", "World", "!")</td> </tr> <tr> <td><a name="named-param" id="named-param"></a><a href="cpp#named-param-note">named parameters</a></td> <td><span style="color: gray"><em>none</em></span></td> <td>+(float)weight: (float) w height: (float) h {<br /> <span style="white-space: pre-wrap;"> </span>return (w * 703) / (h * h);<br /> }<br /> +(float)height: (float) h weight: (float) w {<br /> <span style="white-space: pre-wrap;"> </span>return [BMI weight: w height: h];<br /> }<br /> [BMI weight:155 height:70];<br /> [BMI height:70 weight:155];</td> <td><span style="color: gray"><em>none</em></span></td> <td><span style="color: gray"><em>added in C# 4.0:</em></span><br /> static int BMI(int weight, int height) {<br /> <span style="white-space: pre-wrap;"> </span>return (weight * 703) / (height * height);<br /> }<br /> BMI(weight: 123, height: 64);<br /> BMI(height: 64, weight: 123);</td> </tr> <tr> <td><a name="pass-by-val" id="pass-by-val"></a><a href="cpp#pass-by-val-note">pass by value</a></td> <td>int add1(int n) {<br /> <span style="white-space: pre-wrap;"> </span>return ++n;<br /> }<br /> <br /> int i(7);<br /> <br /> <span style="color: gray"><span style="white-space: pre-wrap;">//</span> set i2 to 8 w/o modifying i:</span><br /> int i2 = add1(i);</td> <td>void use_integer(int i) {<br /> <span style="white-space: pre-wrap;"> </span><span style="color: gray"><em>function body</em></span><br /> }<br /> int i = 7;<br /> use_integer(i);</td> <td><span style="color: gray"><em>primitive types are always passed by value</em></span></td> <td><span style="color: gray"><em>primitive types are always passed by value</em></span></td> </tr> <tr> <td><a name="pass-by-ref" id="pass-by-ref"></a><a href="cpp#pass-by-ref-note">pass by reference</a></td> <td>int add1(int& n) {<br /> <span style="white-space: pre-wrap;"> </span>return ++n;<br /> }<br /> <br /> int i(7);<br /> <br /> <span style="color: gray"><span style="white-space: pre-wrap;">//</span> set i and i2 to 8:</span><br /> int i2 = add1(i);</td> <td><span style="color: gray"><em>none</em></span></td> <td><span style="color: gray"><em>objects and arrays are always passed by reference</em></span></td> <td><span style="color: gray"><em>objects and arrays are always passed by reference</em></span><br /> <br /> <span style="color: gray"><em>also out parameter</em></span></td> </tr> <tr> <td><a name="pass-by-addr" id="pass-by-addr"></a><a href="cpp#pass-by-addr-note">pass by address</a></td> <td>int add1(int* n) {<br /> <span style="white-space: pre-wrap;"> </span>return ++*n;<br /> }<br /> <br /> int i(7);<br /> <br /> <span style="color: gray"><span style="white-space: pre-wrap;">//</span> set i and i2 to 8:</span><br /> int i2 = add1(&i);</td> <td>void use_iptr(int *i) {<br /> <span style="white-space: pre-wrap;"> </span><span style="color: gray"><em>function body</em></span><br /> }<br /> int i = 7;<br /> use_iptr(&i);</td> <td><span style="color: gray"><em>none</em></span></td> <td><span style="color: gray"><em>none</em></span></td> </tr> <tr> <td><a name="retval" id="retval"></a><a href="cpp#retval-note">return value</a><br /> <span style="white-space: pre-wrap;"> </span></td> <td><span style="color: gray"><em>argument of</em> return; <em>type must be declared</em></span></td> <td></td> <td></td> <td></td> </tr> <tr> <td><a name="no-retval" id="no-retval"></a><a href="cpp#no-retval-note">no return value</a></td> <td>void<br /> message(const string& msg) {<br /> <span style="white-space: pre-wrap;"> </span>cout <span style="white-space: pre-wrap;"><<</span> msg <span style="white-space: pre-wrap;"><<</span> endl;<br /> }</td> <td></td> <td></td> <td></td> </tr> <tr> <td><a name="recursive-func" id="recursive-func"></a><a href="cpp#recursive-func-note">recursive function</a></td> <td>int<br /> factorial(int n) {<br /> <span style="white-space: pre-wrap;"> </span>if (n <= 1) {<br /> <span style="white-space: pre-wrap;"> </span><span style="white-space: pre-wrap;"> </span>return 1;<br /> <span style="white-space: pre-wrap;"> </span>}<br /> <span style="white-space: pre-wrap;"> </span>return n * factorial(n - 1);<br /> }</td> <td></td> <td></td> <td></td> </tr> <tr> <td><a name="anon-func-literal" id="anon-func-literal"></a><a href="cpp#anon-func-literal-note">anonymous function</a></td> <td>auto add = [](int n, int m) {<br /> <span style="white-space: pre-wrap;"> </span>return n + m;<br /> };</td> <td></td> <td></td> <td></td> </tr> <tr> <td><a name="invoke-anonymous-func" id="invoke-anonymous-func"></a><a href="cpp#invoke-anonymous-func-note">invoke anonymous function</a></td> <td><span style="color: gray"><span style="white-space: pre-wrap;">//</span>on variable holding anon. function:</span><br /> int sum = add(3, 7);<br /> <br /> <span style="color: gray"><span style="white-space: pre-wrap;">//</span> on lambda expression:</span><br /> int sum2 = [](int n, int m) {<br /> <span style="white-space: pre-wrap;"> </span>return n + m;<br /> }(3, 7);</td> <td></td> <td></td> <td></td> </tr> <tr> <td><a name="closure" id="closure"></a><a href="cpp#closure-note">closure</a></td> <td></td> <td></td> <td></td> <td></td> </tr> <tr> <td><a name="func-private-state" id="func-private-state"></a><a href="cpp#func-private-state-note">function with private state</a></td> <td>int<br /> counter() {<br /> <span style="white-space: pre-wrap;"> </span>static int i = 0;<br /> <span style="white-space: pre-wrap;"> </span>return ++i;<br /> }</td> <td></td> <td></td> <td></td> </tr> <tr> <td><a name="func-as-val" id="func-as-val"></a><a href="cpp#func-as-val-note">function as value</a></td> <td></td> <td></td> <td></td> <td></td> </tr> <tr> <td><a name="overload-op" id="overload-op"></a><a href="cpp#overload-op-note">overload operator</a></td> <td>Rational Rational::operator+(Rational& o) {<br /> <span style="white-space: pre-wrap;"> </span>return Rational(this->num * o.denom + o.num * this->denom, this->denom * o.denom);<br /> }</td> <td><span style="color: gray"><em>none</em></span></td> <td><span style="color: gray"><em>none</em></span></td> <td>public static Rational operator+(Rational a, Rational b) {<br /> <span style="white-space: pre-wrap;"> </span>return new Rational(a.num*b.denom + b.num *a.denom,a.denom*b.denom);<br /> }</td> </tr> <tr> <th colspan="5"><a name="execution-control" id="execution-control"></a><a href="cpp#execution-control-note">execution control</a></th> </tr> <tr> <th></th> <th>c++</th> <th>objective c</th> <th>java</th> <th>c#</th> </tr> <tr> <td><a name="if" id="if"></a><a href="cpp#if-note">if</a></td> <td>int signum;<br /> <br /> if (n > 0) {<br /> <span style="white-space: pre-wrap;"> </span>signum = 1;<br /> }<br /> else if (n == 0) {<br /> <span style="white-space: pre-wrap;"> </span>signum = 0;<br /> }<br /> else {<br /> <span style="white-space: pre-wrap;"> </span>signum = -1;<br /> }</td> <td>if (i>0) {<br /> <span style="white-space: pre-wrap;"> </span>signum = 1;<br /> } else if (i==0) {<br /> <span style="white-space: pre-wrap;"> </span>signum = 0;<br /> } else {<br /> <span style="white-space: pre-wrap;"> </span>signum = -1;<br /> }</td> <td>if (i>0) {<br /> <span style="white-space: pre-wrap;"> </span>signum = 1;<br /> } else if (i==0) {<br /> <span style="white-space: pre-wrap;"> </span>signum = 0;<br /> } else {<br /> <span style="white-space: pre-wrap;"> </span>signum = -1;<br /> }</td> <td>if (i>0) {<br /> <span style="white-space: pre-wrap;"> </span>signum = 1;<br /> } else if (i==0) {<br /> <span style="white-space: pre-wrap;"> </span>signum = 0;<br /> } else {<br /> <span style="white-space: pre-wrap;"> </span>signum = -1;<br /> }</td> </tr> <tr> <td><a name="dangling-else" id="dangling-else"></a><a href="cpp#dangling-else-note">dangling else</a></td> <td>if (n == 0)<br /> <span style="white-space: pre-wrap;"> </span>if (m == 0)<br /> <span style="white-space: pre-wrap;"> </span><span style="white-space: pre-wrap;"> </span>cout <span style="white-space: pre-wrap;"><<</span> "n and m are zero" <span style="white-space: pre-wrap;"><<</span> endl;<br /> <span style="white-space: pre-wrap;"> </span>else<br /> <span style="white-space: pre-wrap;"> </span><span style="white-space: pre-wrap;"> </span>cout <span style="white-space: pre-wrap;"><<</span> "n is zero; m isn't" <span style="white-space: pre-wrap;"><<</span> endl;</td> <td></td> <td></td> <td></td> </tr> <tr> <td><a name="switch" id="switch"></a><a href="cpp#switch-note">switch</a></td> <td>const int INVALID_BINARY_DIGIT(-1);<br /> int bin_digit;<br /> <br /> switch(n) {<br /> case 0:<br /> case 1:<br /> <span style="white-space: pre-wrap;"> </span>bin_digit = n;<br /> <span style="white-space: pre-wrap;"> </span>break;<br /> default:<br /> <span style="white-space: pre-wrap;"> </span>bin_digit = INVALID_BINARY_DIGIT;<br /> <span style="white-space: pre-wrap;"> </span>break;<br /> }</td> <td>switch(i) {<br /> case 0:<br /> <span style="white-space: pre-wrap;"> </span>0;<br /> <span style="white-space: pre-wrap;"> </span>break;<br /> case 1:<br /> <span style="white-space: pre-wrap;"> </span>1;<br /> <span style="white-space: pre-wrap;"> </span>break;<br /> default:<br /> <span style="white-space: pre-wrap;"> </span>-1;<br /> <span style="white-space: pre-wrap;"> </span>break;<br /> }</td> <td>switch(i) {<br /> case 0:<br /> <span style="white-space: pre-wrap;"> </span>0;<br /> <span style="white-space: pre-wrap;"> </span>break;<br /> case 1:<br /> <span style="white-space: pre-wrap;"> </span>1;<br /> <span style="white-space: pre-wrap;"> </span>break;<br /> default:<br /> <span style="white-space: pre-wrap;"> </span>-1;<br /> <span style="white-space: pre-wrap;"> </span>break;<br /> }</td> <td>switch(i) {<br /> case 0:<br /> <span style="white-space: pre-wrap;"> </span>0;<br /> <span style="white-space: pre-wrap;"> </span>break;<br /> case 1:<br /> <span style="white-space: pre-wrap;"> </span>1;<br /> <span style="white-space: pre-wrap;"> </span>break;<br /> default:<br /> <span style="white-space: pre-wrap;"> </span>-1;<br /> <span style="white-space: pre-wrap;"> </span>break;<br /> }</td> </tr> <tr> <td><a name="while" id="while"></a><a href="cpp#while-note">while</a></td> <td>int i(1), fact(1), n(10);<br /> <br /> while (i < n) {<br /> <span style="white-space: pre-wrap;"> </span>fact *= i;<br /> <span style="white-space: pre-wrap;"> </span>++i;<br /> }</td> <td>int i = 0;<br /> while (i<10) {<br /> <span style="color: gray">…</span><br /> <span style="white-space: pre-wrap;"> </span>i++;<br /> }</td> <td>int i = 0;<br /> while (i<10) {<br /> <span style="color: gray">…</span><br /> <span style="white-space: pre-wrap;"> </span>i++;<br /> }</td> <td>int i = 0;<br /> while (i<10) {<br /> <span style="color: gray">…</span><br /> <span style="white-space: pre-wrap;"> </span>i++;<br /> }</td> </tr> <tr> <td><a name="for" id="for"></a><a href="cpp#for-note">for</a></td> <td>int fact, n(10);<br /> <br /> for (int i = 1, fact = 1; i <= n; ++i) {<br /> <span style="white-space: pre-wrap;"> </span>fact *= i;<br /> }</td> <td>int i, n;<br /> for (i=1,n=1; i<=10; i++) {<br /> <span style="white-space: pre-wrap;"> </span>n *= i;<br /> }</td> <td>int n = 1;<br /> for (int i=1; i<=10; i++) {<br /> <span style="white-space: pre-wrap;"> </span>n *= i;<br /> }</td> <td>int i, n;<br /> for (i=1,n=1; i<=10; i++) {<br /> <span style="white-space: pre-wrap;"> </span>n *= i;<br /> }</td> </tr> <tr> <td><a name="break" id="break"></a><a href="cpp#break-note">break</a></td> <td>int data[4] = {3, 2, 0, 1};<br /> int i;<br /> bool has_zero(false);<br /> <br /> for (i = 0; i < 4; ++i) {<br /> <span style="white-space: pre-wrap;"> </span>if (data[i] == 0) {<br /> <span style="white-space: pre-wrap;"> </span><span style="white-space: pre-wrap;"> </span>has_zero = true;<br /> <span style="white-space: pre-wrap;"> </span><span style="white-space: pre-wrap;"> </span>break;<br /> <span style="white-space: pre-wrap;"> </span>}<br /> }</td> <td></td> <td></td> <td></td> </tr> <tr> <td><a name="break-nested-loops" id="break-nested-loops"></a><a href="cpp#break-nested-loops-note">break out of nested loops</a></td> <td>int data[2][2] = <span style="white-space: pre-wrap;">{{</span>3, 2}, {0, 1<span style="white-space: pre-wrap;">}}</span>;<br /> int i, j;<br /> bool has_zero(false);<br /> <br /> for (i = 0; i < 2; ++i) {<br /> <span style="white-space: pre-wrap;"> </span>for (j = 0; j < 2; ++j) {<br /> <span style="white-space: pre-wrap;"> </span><span style="white-space: pre-wrap;"> </span>if (data[i][j] == 0) {<br /> <span style="white-space: pre-wrap;"> </span><span style="white-space: pre-wrap;"> </span><span style="white-space: pre-wrap;"> </span>has_zero = true;<br /> <span style="white-space: pre-wrap;"> </span><span style="white-space: pre-wrap;"> </span><span style="white-space: pre-wrap;"> </span>goto end_of_loops;<br /> <span style="white-space: pre-wrap;"> </span><span style="white-space: pre-wrap;"> </span>}<br /> <span style="white-space: pre-wrap;"> </span>}<br /> }<br /> :end_of_loops</td> <td></td> <td></td> <td></td> </tr> <tr> <td><a name="continue" id="continue"></a><a href="cpp#continue-note">continue</a></td> <td>int a[4] = {3, 2, 0, 1};<br /> <br /> for (int i = 0; i < 4; ++i) {<br /> <span style="white-space: pre-wrap;"> </span>if (a[i] == 0) {<br /> <span style="white-space: pre-wrap;"> </span><span style="white-space: pre-wrap;"> </span>continue;<br /> <span style="white-space: pre-wrap;"> </span>}<br /> <span style="white-space: pre-wrap;"> </span>cout <span style="white-space: pre-wrap;"><<</span> 1.0 / a[i] <span style="white-space: pre-wrap;"><<</span> endl;<br /> }</td> <td></td> <td></td> <td></td> </tr> <tr> <td><a name="goto" id="goto"></a><a href="cpp#goto-note">goto</a></td> <td></td> <td></td> <td></td> <td></td> </tr> <tr> <th colspan="5"><a name="exceptions" id="exceptions"></a><a href="cpp#exceptions-note">exceptions</a></th> </tr> <tr> <th></th> <th>c++</th> <th>objective c</th> <th>java</th> <th>c#</th> </tr> <tr> <td><a name="base-exc" id="base-exc"></a><a href="cpp#base-exc-note">base exception</a></td> <td><span style="color: gray"><em>Any type can be thrown.<br /> <br /> All exceptions thrown by the language or the standard library derive from</em> exception, <em>defined in</em> <exception>.</span></td> <td></td> <td><span style="color: gray"><em>Any type which implements the interface</em> java.lang.Throwable <em>can be thrown.<br /> <br /> Exceptions thrown by the language and the standard libraries derive from</em> java.lang.Errror <em>or</em> java.lang.Exception.</span></td> <td></td> </tr> <tr> <td><a name="predefined-exc" id="predefined-exc"></a><a href="cpp#predefined-exc-note">predefined exceptions</a></td> <td>#include <exception><br /> #include <stdexcept><br /> #include <system_error><br /> #include <typeinfo><br /> <br /> exception<br /> <span style="white-space: pre-wrap;"> </span>logic_error<br /> <span style="white-space: pre-wrap;"> </span><span style="white-space: pre-wrap;"> </span>domain_error<br /> <span style="white-space: pre-wrap;"> </span><span style="white-space: pre-wrap;"> </span>invalid_argument<br /> <span style="white-space: pre-wrap;"> </span><span style="white-space: pre-wrap;"> </span>length_error<br /> <span style="white-space: pre-wrap;"> </span><span style="white-space: pre-wrap;"> </span>out_of_range<br /> <span style="white-space: pre-wrap;"> </span>runtime_error<br /> <span style="white-space: pre-wrap;"> </span><span style="white-space: pre-wrap;"> </span>system_error<br /> <span style="white-space: pre-wrap;"> </span><span style="white-space: pre-wrap;"> </span><span style="white-space: pre-wrap;"> </span>ios_base::failure<br /> <span style="white-space: pre-wrap;"> </span>bad_cast<br /> <span style="white-space: pre-wrap;"> </span>bad_exception<br /> <span style="white-space: pre-wrap;"> </span>bad_alloc</td> <td></td> <td><span style="color: gray"><em>java.lang.Throwable</em></span><br /> <span style="white-space: pre-wrap;"> </span>java.lang.Error<br /> <span style="white-space: pre-wrap;"> </span>java.lang.Exception<br /> <span style="white-space: pre-wrap;"> </span><span style="white-space: pre-wrap;"> </span>java.lang.IOException<br /> <span style="white-space: pre-wrap;"> </span><span style="white-space: pre-wrap;"> </span>java.lang.RuntimeException<br /> <span style="white-space: pre-wrap;"> </span><span style="white-space: pre-wrap;"> </span><span style="white-space: pre-wrap;"> </span>java.lang.ArithmeticException<br /> <span style="white-space: pre-wrap;"> </span><span style="white-space: pre-wrap;"> </span><span style="white-space: pre-wrap;"> </span>java.lang.IllegalArgumentException<br /> <span style="white-space: pre-wrap;"> </span><span style="white-space: pre-wrap;"> </span><span style="white-space: pre-wrap;"> </span>java.lang.IndexOutOfBoundsException<br /> <span style="white-space: pre-wrap;"> </span><span style="white-space: pre-wrap;"> </span><span style="white-space: pre-wrap;"> </span>java.lang.NullPointerException</td> <td></td> </tr> <tr> <td><a name="raise-exc" id="raise-exc"></a><a href="cpp#raise-exc-note">raise exception</a></td> <td>#include <cstdlib><br /> #include <stdexcept><br /> <br /> void risky() {<br /> <span style="white-space: pre-wrap;"> </span>if (rand() < 10) {<br /> <span style="white-space: pre-wrap;"> </span><span style="white-space: pre-wrap;"> </span>throw runtime_error("bam!");<br /> <span style="white-space: pre-wrap;"> </span>}<br /> }</td> <td>NSException *exc = [NSException exceptionWithName:@"error" reason:@"failed" userInfo:nil];<br /> @throw exc;</td> <td>throw new Exception("failed");</td> <td>throw new System.Exception("failed");</td> </tr> <tr> <td><a name="handle-exc" id="handle-exc"></a><a href="cpp#handle-exc-note">handle exception</a></td> <td>#include <stdexcept><br /> <br /> try {<br /> <span style="white-space: pre-wrap;"> </span>risky();<br /> }<br /> catch (const exception &e) {<br /> <span style="white-space: pre-wrap;"> </span>cout <span style="white-space: pre-wrap;"><<</span> e.what() <span style="white-space: pre-wrap;"><<</span> endl;<br /> }</td> <td>@try {<br /> <span style="white-space: pre-wrap;"> </span>[NSException raise:@"error" format:@"failed"];<br /> } @catch (NSException *e) {<br /> <span style="white-space: pre-wrap;"> </span>printf([[e reason] UTF8String]);<br /> }</td> <td>try {<br /> <span style="white-space: pre-wrap;"> </span>throw new Exception("failed");<br /> }<br /> catch (Exception e) {<br /> <span style="white-space: pre-wrap;"> </span>System.out.println(e.getMessage());<br /> }</td> <td>try {<br /> <span style="white-space: pre-wrap;"> </span>throw new System.Exception("failed");<br /> } catch (System.Exception e) {<br /> <span style="white-space: pre-wrap;"> </span>System.Console.WriteLine(e.Message);<br /> }</td> </tr> <tr> <td><a name="def-exc" id="def-exc"></a><a href="cpp#def-exc-note">define exception</a></td> <td>#include <stdexcept><br /> <br /> class Bam : public runtime_error {<br /> public:<br /> <span style="white-space: pre-wrap;"> </span>Bam() : runtime_error("bam!") {}<br /> };<br /> <br /> throw Bam();</td> <td></td> <td></td> <td></td> </tr> <tr> <td><a name="re-raise-exc" id="re-raise-exc"></a><a href="cpp#re-raise-exc-note">re-raise exception</a></td> <td>#include <stdexcept><br /> <br /> try {<br /> <span style="white-space: pre-wrap;"> </span>risky();<br /> }<br /> catch (const exception& e) {<br /> <span style="white-space: pre-wrap;"> </span>cout <span style="white-space: pre-wrap;"><<</span> "an error occurred<span style="white-space: pre-wrap;">...</span>" <span style="white-space: pre-wrap;"><<</span> endl;<br /> <span style="white-space: pre-wrap;"> </span>throw;<br /> }</td> <td></td> <td></td> <td></td> </tr> <tr> <td><a name="catch-all-handler" id="catch-all-handler"></a> <a href="cpp#catch-all-handler-note">catch-all handler</a></td> <td>#include <stdexcept><br /> <br /> try {<br /> <span style="white-space: pre-wrap;"> </span>risky();<br /> }<br /> catch (<span style="white-space: pre-wrap;">...</span>) {<br /> <span style="white-space: pre-wrap;"> </span>cout <span style="white-space: pre-wrap;"><<</span> "an error was ignored"<br /> <span style="white-space: pre-wrap;"> </span><span style="white-space: pre-wrap;"> </span><span style="white-space: pre-wrap;"> </span><span style="white-space: pre-wrap;"> </span><span style="white-space: pre-wrap;"><<</span> endl;<br /> }</td> <td></td> <td></td> <td></td> </tr> <tr> <td><a name="multiple-handlers" id="multiple-handlers"></a><a href="cpp#multiple-handlers-note">multiple handlers</a></td> <td>#include <stdexcept><br /> <br /> try {<br /> <span style="white-space: pre-wrap;"> </span>risky();<br /> }<br /> catch (const system_error &e) {<br /> <span style="white-space: pre-wrap;"> </span>cout <span style="white-space: pre-wrap;"><<</span> "system error: " <span style="white-space: pre-wrap;"><<</span> e.name()<br /> <span style="white-space: pre-wrap;"> </span><span style="white-space: pre-wrap;"> </span><span style="white-space: pre-wrap;"> </span><span style="white-space: pre-wrap;"> </span><span style="white-space: pre-wrap;"><<</span> endl;<br /> }<br /> catch (const exception &e) {<br /> <span style="white-space: pre-wrap;"> </span>cout <span style="white-space: pre-wrap;"><</span>< "exception: " <span style="white-space: pre-wrap;"><<</span> e.what()<br /> <span style="white-space: pre-wrap;"> </span><span style="white-space: pre-wrap;"> </span><span style="white-space: pre-wrap;"> </span><span style="white-space: pre-wrap;"> </span><span style="white-space: pre-wrap;"><<</span> endl;<br /> }<br /> catch (<span style="white-space: pre-wrap;">...</span>) {<br /> <span style="white-space: pre-wrap;"> </span>cout <span style="white-space: pre-wrap;"><<</span> "unknown error" <span style="white-space: pre-wrap;"><<</span> endl;<br /> }</td> <td></td> <td></td> <td></td> </tr> <tr> <td><a name="uncaught-exc" id="uncaught-exc"></a><a href="cpp#uncaught-exc-note">uncaught exception behavior</a></td> <td><span style="color: gray"><em>calls</em> terminate() <em>which by default calls</em> abort()</span></td> <td></td> <td></td> <td></td> </tr> <tr> <td><a name="error-msg" id="error-msg"></a><a href="cpp#error-msg-note">error message</a></td> <td>#include <exception><br /> <br /> try {<br /> <span style="white-space: pre-wrap;"> </span>risky();<br /> }<br /> catch (const exception &e) {<br /> <span style="white-space: pre-wrap;"> </span>const chae *msg = e.what();<br /> }</td> <td></td> <td></td> <td></td> </tr> <tr> <td><a name="errno" id="errno"></a><a href="cpp#errno-note">system call errno</a></td> <td>#include <system_error><br /> <br /> try {<br /> <span style="white-space: pre-wrap;"> </span>risky();<br /> }<br /> catch (const system_error &e {<br /> <span style="white-space: pre-wrap;"> </span>int err_code_val = e.code().value();<br /> }</td> <td></td> <td></td> <td></td> </tr> <tr> <td><a name="finally-clause" id="finally-clause"></a><a href="cpp#finally-clause-note">finally clause</a></td> <td><span style="color: gray"><em>none</em></span></td> <td>@try {<br /> <span style="white-space: pre-wrap;"> </span><span style="color: gray"><em>risky code</em></span><br /> } @finally {<br /> <span style="white-space: pre-wrap;"> </span><span style="color: gray"><em>perform cleanup</em></span><br /> }</td> <td>try {<br /> <span style="white-space: pre-wrap;"> </span><span style="color: gray"><em>risky code</em></span><br /> } finally {<br /> <span style="white-space: pre-wrap;"> </span><span style="color: gray"><em>perform cleanup</em></span><br /> }</td> <td>try {<br /> <span style="white-space: pre-wrap;"> </span><span style="color: gray"><em>risky code</em></span><br /> } finally {<br /> <span style="white-space: pre-wrap;"> </span><span style="color: gray"><em>perform cleanup</em></span><br /> }</td> </tr> <tr> <td><a name="exc-specification" id="exc-specification"></a><a href="cpp#exc-specification-note">exception specification</a></td> <td><span style="color: gray"><span style="white-space: pre-wrap;">//</span> Use noexcept to declare that a function<br /> <span style="white-space: pre-wrap;">//</span> does not raise exceptions;<br /> <span style="white-space: pre-wrap;">//</span> declaring which exceptions a function<br /> <span style="white-space: pre-wrap;">//</span> raises is deprecated in C++11.</span><br /> int<br /> add(int a, int b) noexcept {<br /> <span style="white-space: pre-wrap;"> </span>return a + b;<br /> }</td> <td><span style="color: gray"><em>no</em></span></td> <td><span style="color: gray"><em>yes</em></span></td> <td><span style="color: gray"><em>no</em></span></td> </tr> <tr> <th colspan="5"><a name="concurrency" id="concurrency"></a><a href="cpp#concurrency-note">concurrency</a></th> </tr> <tr> <th></th> <th>c++</th> <th>objective c</th> <th>java</th> <th>c#</th> </tr> <tr> <td><a name="start-thread" id="start-thread"></a><a href="cpp#start-thread-note">start thread</a><br /> <span style="white-space: pre-wrap;"> </span></td> <td></td> <td></td> <td></td> <td></td> </tr> <tr> <td><a name="terminate-current-thread" id="terminate-current-thread"></a><a href="cpp#terminate-current-thread-note">terminate current thread</a></td> <td></td> <td></td> <td></td> <td></td> </tr> <tr> <td><a name="terminate-other-thread" id="terminate-other-thread"></a><a href="cpp#terminate-other-thread-note">terminate other thread</a></td> <td></td> <td></td> <td></td> <td></td> </tr> <tr> <td><a name="list-threads" id="list-threads"></a><a href="cpp#list-threads-note">list threads</a></td> <td></td> <td></td> <td></td> <td></td> </tr> <tr> <td><a name="wait-on-thread" id="wait-on-thread"></a><a href="cpp#wait-on-thread-note">wait on thread</a><br /> <span style="white-space: pre-wrap;"> </span></td> <td></td> <td></td> <td></td> <td></td> </tr> <tr> <td><a name="lock" id="lock"></a><a href="cpp#lock-note">lock</a></td> <td></td> <td></td> <td></td> <td></td> </tr> <tr> <td><a name="create-msg-queue" id="create-msg-queue"></a><a href="cpp#create-msg-queue-note">create message queue</a></td> <td></td> <td></td> <td></td> <td></td> </tr> <tr> <td><a name="send-msg" id="send-msg"></a><a href="cpp#send-msg-note">send message</a></td> <td></td> <td></td> <td></td> <td></td> </tr> <tr> <td><a name="receive-msg" id="receive-msg"></a><a href="cpp#receive-msg-note">receive message</a></td> <td></td> <td></td> <td></td> <td></td> </tr> <tr> <th colspan="5"><a name="file-handles" id="file-handles"></a><a href="cpp#file-handles-note">file handles</a></th> </tr> <tr> <th></th> <th>c++</th> <th>objective c</th> <th>java</th> <th>c#</th> </tr> <tr> <td><a name="std-file-handles" id="std-file-handles"></a><a href="cpp#std-file-handles-note">standard file handles</a></td> <td>cin<br /> cout<br /> cerr<br /> clog <span style="color: gray"><span style="white-space: pre-wrap;">//</span> buffered cerr by default</span></td> <td></td> <td>System.in<br /> System.out<br /> System.err</td> <td></td> </tr> <tr> <td><a name="read-line-stdin" id="read-line-stdin"></a><a href="cpp#read-line-stdin-note">read line from stdin</a></td> <td>string s;<br /> <br /> cin <span style="white-space: pre-wrap;">>></span> s;</td> <td></td> <td></td> <td></td> </tr> <tr> <td><a name="printf" id="printf"></a><a href="cpp#printf-note">write formatted string to stdout</a><br /> <span style="white-space: pre-wrap;"> </span></td> <td>cout <span style="white-space: pre-wrap;"><<</span> "count: " <span style="white-space: pre-wrap;"><<</span> 7 <span style="white-space: pre-wrap;"><<</span> endl;</td> <td>printf("count: %d\n", 7);</td> <td>System.out.printf("count: %d", 7);</td> <td>System.Console.WriteLine("count: {0}", 7);</td> </tr> <tr> <td><a href="cpp#read-file">read from file</a></td> <td>#include <fstream><br /> <br /> string line;<br /> ifstream f("/etc/passwd");<br /> <br /> if (f.is_open()) {<br /> <span style="white-space: pre-wrap;"> </span>while (!f.eof()) {<br /> <span style="white-space: pre-wrap;"> </span><span style="white-space: pre-wrap;"> </span>getline(f, line);<br /> <span style="white-space: pre-wrap;"> </span><span style="white-space: pre-wrap;"> </span><span style="color: gray"><span style="white-space: pre-wrap;">//</span> process line</span><br /> <span style="white-space: pre-wrap;"> </span>}<br /> <span style="white-space: pre-wrap;"> </span>f.close();<br /> <span style="white-space: pre-wrap;"> </span>if ( 0 != f.fail() ) {<br /> <span style="white-space: pre-wrap;"> </span><span style="white-space: pre-wrap;"> </span><span style="color: gray"><span style="white-space: pre-wrap;">//</span> handle error</span><br /> <span style="white-space: pre-wrap;"> </span>}<br /> }<br /> else {<br /> <span style="white-space: pre-wrap;"> </span><span style="color: gray"><span style="white-space: pre-wrap;">//</span> handle error</span><br /> }</td> <td>NSError *error = nil;<br /> NSString *s = [NSString stringWithContentsOfFile: @"/etc/passwd" encoding:NSUTF8StringEncoding error:&error];<br /> <br /> if ( error != nil ) {<br /> <span style="white-space: pre-wrap;"> </span><span style="color: gray"><span style="white-space: pre-wrap;">//</span> handle error</span><br /> }<br /> <br /> NSArray *a = [s componentsSeparatedByString:@"\n"];<br /> id line;<br /> <br /> while (line = [i nextObject]) {<br /> <span style="white-space: pre-wrap;"> </span><span style="color: gray"><span style="white-space: pre-wrap;">//</span> process line</span><br /> }</td> <td>import java.io.BufferedReader;<br /> import java.io.FileReader;<br /> <br /> BufferedReader in = new BufferedReader(new FileReader("/etc/passwd"));<br /> String line;<br /> <br /> while ((line = in.readLine()) != null) {<br /> <span style="white-space: pre-wrap;"> </span><span style="color: gray"><span style="white-space: pre-wrap;">//</span> process line</span><br /> }</td> <td>using System.IO;<br /> <br /> StreamReader sr = new StreamReader("/etc/passwd");<br /> string line;<br /> <br /> while ((line = sr.ReadLine()) != null) {<br /> <span style="white-space: pre-wrap;"> </span><span style="color: gray"><span style="white-space: pre-wrap;">//</span> process line</span><br /> }</td> </tr> <tr> <td><a href="cpp#write-file">write to file</a></td> <td>#include <fstream><br /> <br /> ofstream f("/tmp/test4");<br /> int i;<br /> <br /> for (i = 0; i < 10; ++i) {<br /> <span style="white-space: pre-wrap;"> </span>f <span style="white-space: pre-wrap;"><<</span> i <span style="white-space: pre-wrap;"><<</span> endl;<br /> }<br /> f.close();<br /> if (0 != f.fail()) {<br /> <span style="white-space: pre-wrap;"> </span><span style="color: gray"><span style="white-space: pre-wrap;">//</span> handle error</span><br /> }</td> <td></td> <td>import java.io.BufferedWriter;<br /> import java.io.FileWriter;<br /> <br /> BufferedWriter fout = new BufferedWriter(new FileWriter("/tmp/test2"));<br /> int i;<br /> <br /> for (i = 0; i < 10; i++) {<br /> <span style="white-space: pre-wrap;"> </span>fout.write(String.format("%d", i));<br /> <span style="white-space: pre-wrap;"> </span>fout.newLine();<br /> }<br /> fout.close();</td> <td>using System.IO;<br /> <br /> StreamWriter fout = new StreamWriter("/tmp/test3");<br /> int i;<br /> <br /> for (i = 0; i < 10; i++) {<br /> <span style="white-space: pre-wrap;"> </span>fout.WriteLine(i.ToString());<br /> }<br /> fout.Close();</td> </tr> <tr> <th colspan="5"><a name="files" id="files"></a><a href="cpp#files-note">files</a></th> </tr> <tr> <th></th> <th>c++</th> <th>objective c</th> <th>java</th> <th>c#</th> </tr> <tr> <td><a name="file-test" id="file-test"></a><a href="cpp#file-test-note">file exists test, file regular test</a></td> <td></td> <td></td> <td>import java.io.File;<br /> <br /> File f = new File("/etc/hosts");<br /> f.exists()<br /> f.isFile()</td> <td>System.IO.File.Exists("/etc/hosts")</td> </tr> <tr> <td><a name="file-size" id="file-size"></a><a href="cpp#file-size-note">file size</a></td> <td></td> <td></td> <td>import java.io.File;<br /> <br /> File f = new File("/etc/hosts");<br /> f.length()</td> <td></td> </tr> <tr> <td><a name="readable-writable-executable" id="readable-writable-executable"></a><a href="cpp#readable-writable-executable">is file readable, writable, executable</a></td> <td></td> <td></td> <td>import java.io.File;<br /> <br /> File f = new File("/etc/hosts");<br /> <br /> f.canRead()<br /> f.canWrite()<br /> f.canExecute()</td> <td></td> </tr> <tr> <td><a name="chmod" id="chmod"></a><a href="cpp#chmod-note">set file permissions</a></td> <td></td> <td></td> <td>import java.io.File;<br /> <br /> File f = new File("/tmp/foo");<br /> <br /> <span style="color: gray"><span style="white-space: pre-wrap;">//</span> sets owner perms; to turn perms off<br /> <span style="white-space: pre-wrap;">//</span> set arg to false:</span><br /> f.setReadable(true);<br /> f.setWritable(true);<br /> f.setExecutable(true);<br /> <br /> <span style="color: gray"><span style="white-space: pre-wrap;">//</span> if 2nd arg is false, perms are<br /> <span style="white-space: pre-wrap;">//</span> for owner, group, and other:</span><br /> f.setReadable(true, false);<br /> f.setWritable(true, false);<br /> f.setExecutable(true, false);</td> <td></td> </tr> <tr> <td><a name="file-cp-rm-mv" id="file-cp-rm-mv"></a><a href="cpp#file-cp-rm-mv-note">copy file, remove file, rename file</a></td> <td></td> <td></td> <td>import java.io.File;<br /> <br /> <span style="color: gray"><em>??</em></span><br /> <br /> File f2 = new File("/tmp/foo");<br /> f2.delete();<br /> <br /> File f3 = new File("/tmp/bar");<br /> f3.renameTo(new File("/tmp/bar"));</td> <td></td> </tr> <tr> <th colspan="5"><a name="file-fmt" id="file-fmt"></a><a href="cpp#file-fmt-note">file formats</a></th> </tr> <tr> <th></th> <th>c++</th> <th>objective c</th> <th>java</th> <th>c#</th> </tr> <tr> <td>csv</td> <td></td> <td></td> <td></td> <td></td> </tr> <tr> <td>json</td> <td></td> <td></td> <td></td> <td></td> </tr> <tr> <td>build xml</td> <td></td> <td></td> <td></td> <td></td> </tr> <tr> <td>parse xml</td> <td></td> <td></td> <td></td> <td></td> </tr> <tr> <td>parse html</td> <td></td> <td></td> <td></td> <td></td> </tr> <tr> <th colspan="5"><a name="directories" id="directories"></a><a href="cpp#directories-note">directories</a></th> </tr> <tr> <th></th> <th>c++</th> <th>objective c</th> <th>java</th> <th>c#</th> </tr> <tr> <td><a name="build-pathname" id="build-pathname"></a><a href="cpp#build-pathname-note">build pathname</a></td> <td></td> <td></td> <td>import java.io.File;<br /> <br /> File root = File.listRoots()[0];<br /> File etc = new File(root, "etc");<br /> File hosts = new File(etc, "hosts");<br /> String path = hosts.getPath();</td> <td></td> </tr> <tr> <td><a name="dirname-basename" id="dirname-basename"></a><a href="cpp#dirname-basename-note">dirname and basename</a></td> <td>#include <libgen.h><br /> <br /> string s1 = dirname("/etc/hosts");<br /> string s2 = basename("/etc/hosts");</td> <td></td> <td>import java.io.File;<br /> <br /> File f = new File("/etc/hosts");<br /> String dirname = f.getParent();<br /> String basename = f.getName();</td> <td></td> </tr> <tr> <td><a name="absolute-pathname" id="absolute-pathname"></a><a href="cpp#absolute-pathname-note">absolute pathname</a></td> <td>#include <climits><br /> #include <cstdlib><br /> <br /> char buf[PATH_MAX];<br /> if (realpath("..", buf) == NULL) {<br /> <span style="white-space: pre-wrap;"> </span>throw exception();<br /> }<br /> else {<br /> <span style="white-space: pre-wrap;"> </span>string path(buf);<br /> }</td> <td></td> <td>import java.io.File;<br /> <br /> File f = new File("foo");<br /> String abspath = f.getAbsolutePath();<br /> <br /> <span style="color: gray"><span style="white-space: pre-wrap;">//</span> getCanonicalPath() expands .. and .:</span><br /> File f2 = new File("../foo");<br /> String abspath2 = f2.getCanonicalPath();<br /> File f3 = new File("./foo");<br /> String abspath3 = f3.getCanonicalPath();</td> <td></td> </tr> <tr> <td><a name="iterate-dir" id="iterate-dir"></a><a href="cpp#dir-iterate-note">iterate over directory by file</a></td> <td></td> <td></td> <td>import java.io.File;<br /> <br /> File dir = new File("/etc");<br /> <br /> <span style="color: gray"><span style="white-space: pre-wrap;">//</span> iterate over names:</span><br /> for (String name: dir.list()) {<br /> <span style="white-space: pre-wrap;"> </span>System.out.println(name);<br /> }<br /> <br /> <span style="color: gray"><span style="white-space: pre-wrap;">//</span> iterate over file objects:</span><br /> for (File f: dir.listFiles()) {<br /> <span style="white-space: pre-wrap;"> </span>System.out.println(f.getName());<br /> }</td> <td></td> </tr> <tr> <td><a name="glob" id="glob"></a><a href="cpp#glob-note">glob paths</a></td> <td></td> <td></td> <td></td> <td></td> </tr> <tr> <td><a name="mkdir" id="mkdir"></a><a href="cpp#mkdir-note">make directory</a></td> <td></td> <td></td> <td>import java.io.File;<br /> <br /> File f = new File("/tmp/foo/bar");<br /> f.mkdirs();</td> <td></td> </tr> <tr> <td><a name="recursive-cp" id="recursive-cp"></a><a href="cpp#recursive-cp-note">recursive copy</a></td> <td></td> <td></td> <td></td> <td></td> </tr> <tr> <td><a name="rmdir" id="rmdir"></a><a href="cpp#rmdir-note">remove empty directory</a></td> <td></td> <td></td> <td></td> <td></td> </tr> <tr> <td><a name="rm-rf" id="rm-rf"></a><a href="cpp#rm-rf-note">remove directory and contents</a></td> <td></td> <td></td> <td></td> <td></td> </tr> <tr> <td><a name="dir-test" id="dir-test"></a><a href="cpp#dir-test-note">directory test</a><br /> <span style="white-space: pre-wrap;"> </span></td> <td></td> <td></td> <td>import java.io.File;<br /> <br /> File f = new File("/tmp");<br /> f.isDirectory()</td> <td></td> </tr> <tr> <td><a name="unused-dir" id="unused-dir"></a><a href="cpp#unused-dir-note">generate unused directory name</a></td> <td></td> <td></td> <td></td> <td></td> </tr> <tr> <td><a name="system-tmp-dir" id="system-tmp-dir"></a><a href="cpp#system-tmp-dir-note">system temporary file directory</a></td> <td></td> <td></td> <td></td> <td></td> </tr> <tr> <th colspan="5"><a name="processes-environment" id="processes-environment"></a><a href="cpp#processes-environment-note">processes and environment</a></th> </tr> <tr> <th></th> <th>c++</th> <th>objective c</th> <th>java</th> <th>c#</th> </tr> <tr> <td><a href="cpp#main">signature of main</a></td> <td>int main(int argc, char<span style="white-space: pre-wrap;">**</span> argv) {</td> <td>int main(int argc, char <span style="white-space: pre-wrap;">**</span>argv) {</td> <td>public class <em>Foo</em> {<br /> <span style="white-space: pre-wrap;"> </span>public static void main(String[] args) {</td> <td>public class <em>Foo</em> {<br /> <span style="white-space: pre-wrap;"> </span>public static void Main(string[] args) {</td> </tr> <tr> <td><a href="cpp#first-argument">first argument</a><br /> <span style="white-space: pre-wrap;"> </span></td> <td><span style="color: gray"><em>pathname of executable</em></span></td> <td><span style="color: gray"><em>pathname of executable</em></span></td> <td><span style="color: gray"><em>first command line argument</em></span></td> <td><span style="color: gray"><em>first command line argument</em></span></td> </tr> <tr> <td><a href="cpp#environment-variable">environment variable</a></td> <td>#include <stdlib.h><br /> <br /> char* home = getenv("HOME");<br /> setenv("EDITOR", "emacs", 1);<br /> unsetenv("EDITOR");</td> <td>NSString *home = [[[NSProcessInfo processInfo] environment] objectForKey:@"HOME"];</td> <td>String home = System.getenv("HOME");</td> <td>using System.Environment;<br /> string home = GetEnvironmentVariable("HOME");<br /> SetEnvironmentVariable("EDITOR", "emacs");<br /> SetEnvironmentVariable("EDITOR", null);</td> </tr> <tr> <td><a href="cpp#iterate-environment-variable">iterate through environment variables</a></td> <td></td> <td>NSEnumerator *i = [[[NSProcessInfo processInfo] environment] keyEnumerator];<br /> id key;<br /> while ((key = [i nextObject])) {<br /> <span style="white-space: pre-wrap;"> </span><span style="color: gray"><em>use NSString key</em></span><br /> }</td> <td>import java.util.Map;<br /> Map<String, String> env = System.getenv();<br /> for (String name : env.keySet()) {<br /> <span style="white-space: pre-wrap;"> </span>String value = env.get(name));<br /> }</td> <td>using System.Collections;<br /> using System.Environment;<br /> IDictionary env = GetEnvironmentVariables();<br /> foreach (DictionaryEntry de in env) {<br /> <span style="white-space: pre-wrap;"> </span><span style="color: gray"><em>use de.Key or de.Value</em></span><br /> }</td> </tr> <tr> <th colspan="5"><a name="libraries-namespaces" id="libraries-namespaces"></a><a href="cpp#libraries-namespaces-note">libraries and namespaces</a></th> </tr> <tr> <th></th> <th>c++</th> <th>objective c</th> <th>java</th> <th>c#</th> </tr> <tr> <td><a name="std-lib-name" id="std-lib-name"></a><a href="cpp#std-lib-name-note">standard library name</a></td> <td><span style="color: gray"><em>C++ Standard Library</em></span></td> <td><span style="color: gray"><em>Foundation Framework</em></span></td> <td><span style="color: gray"><em>Java API</em></span></td> <td><span style="color: gray"><em>Base Class Library</em></span></td> </tr> <tr> <td><a href="cpp#declare-namespace">declare namespace</a></td> <td>namespace foo {<br /> <span style="white-space: pre-wrap;"> </span>namespace bar {<br /> <span style="white-space: pre-wrap;"> </span><span style="white-space: pre-wrap;"> </span>class Baz {<br /> <span style="white-space: pre-wrap;"> </span><span style="white-space: pre-wrap;"> </span><span style="white-space: pre-wrap;"> </span>static const int ANSWER = 42;<br /> <span style="white-space: pre-wrap;"> </span><span style="white-space: pre-wrap;"> </span>};<br /> <span style="white-space: pre-wrap;"> </span>}<br /> }</td> <td></td> <td>package foo.bar;<br /> public class Baz {<br /> <span style="white-space: pre-wrap;"> </span>public static final int ANSWER = 42;<br /> }</td> <td>namespace foo {<br /> <span style="white-space: pre-wrap;"> </span>namespace bar {<br /> <span style="white-space: pre-wrap;"> </span><span style="white-space: pre-wrap;"> </span>public class Baz {<br /> <span style="white-space: pre-wrap;"> </span><span style="white-space: pre-wrap;"> </span><span style="white-space: pre-wrap;"> </span>public const int ANSWER = 42;<br /> <span style="white-space: pre-wrap;"> </span><span style="white-space: pre-wrap;"> </span>};<br /> <span style="white-space: pre-wrap;"> </span>}<br /> }</td> </tr> <tr> <td><a href="cpp#namespaces-per-file">multiple namespaces per file</a></td> <td><span style="color: gray"><em>yes</em></span></td> <td></td> <td><span style="color: gray"><em>no</em></span></td> <td><span style="color: gray"><em>yes</em></span></td> </tr> <tr> <td><a href="cpp#namespace-directory-mapping">namespaces map to directories</a></td> <td><span style="color: gray"><em>no</em></span></td> <td></td> <td><span style="color: gray"><em>yes</em></span></td> <td><span style="color: gray"><em>no</em></span></td> </tr> <tr> <td><a href="cpp#import-namespace">import namespace</a></td> <td>using namespace foo::bar;<br /> <br /> cout <span style="white-space: pre-wrap;"><<</span> Baz::ANSWER <span style="white-space: pre-wrap;"><<</span> endl;</td> <td></td> <td>import foo.bar.*;<br /> System.out.println(Baz.ANSWER);</td> <td>using foo.bar;<br /> System.Console.WriteLine(Baz.ANSWER);</td> </tr> <tr> <td><a href="cpp#import-part-namespace">import part of namespace</a></td> <td>using namespace foo;<br /> <br /> cout <span style="white-space: pre-wrap;"><<</span> bar::Baz::ANSWER <span style="white-space: pre-wrap;"><<</span> endl;</td> <td></td> <td><span style="color: gray"><em>none</em></span></td> <td><span style="color: gray"><em>none</em></span></td> </tr> <tr> <td><a href="cpp#import-symbol">import symbol</a></td> <td>using foo::bar::Baz;<br /> cout <span style="white-space: pre-wrap;"><<</span> Baz::ANSWER <span style="white-space: pre-wrap;"><<</span> endl;</td> <td></td> <td>import foo.bar.Baz;<br /> System.out.println(Baz.ANSWER);</td> <td><span style="color: gray"><em>none</em></span></td> </tr> <tr> <td><a href="cpp#import-static-symbol">import static symbol</a></td> <td><span style="color: gray"><em>none</em></span></td> <td></td> <td>import static foo.bar.Baz.ANSWER;<br /> System.out.println(ANSWER);</td> <td><span style="color: gray"><em>none</em></span></td> </tr> <tr> <td><a href="cpp#import-position">import position</a><br /> <span style="white-space: pre-wrap;"> </span></td> <td><span style="color: gray"><em>anywhere a statement is legal</em></span></td> <td></td> <td><span style="color: gray"><em>after package and before type definitions</em></span></td> <td><span style="color: gray"><em>outside of class definitions</em></span></td> </tr> <tr> <td><a href="cpp#not-imported-symbol">using a symbol that hasn't been imported</a></td> <td>cout <span style="white-space: pre-wrap;"><<</span> foo::bar::Baz::ANSWER <span style="white-space: pre-wrap;"><<</span> endl;</td> <td></td> <td>System.out.println(foo.bar.Baz.ANSWER);</td> <td>using System.Console;<br /> WriteLine(foo.bar.Baz.ANSWER);</td> </tr> <tr> <td><a name="app-env" id="app-env"></a><a href="cpp#app-env-note">application environment</a></td> <td></td> <td></td> <td></td> <td></td> </tr> <tr> <td><a name="multiple-installations" id="multiple-installations"></a><a href="cpp#multiple-installations-note">multiple installations</a></td> <td></td> <td></td> <td><span style="color: gray"><em>set</em> JAVA_HOME <em>environment variable to directory containing a</em> bin <em>subdirectory with</em> java, javac, <em>and other command line tools. Put</em> $JAVA_HOME/bin <em>at front of search path.</em></span></td> <td></td> </tr> <tr> <td><a name="pkg-manager" id="pkg-manager"></a><a href="cpp#pkg-manager-note">package manager</a></td> <td></td> <td></td> <td></td> <td></td> </tr> <tr> <th colspan="5"><a name="user-defined-types" id="user-defined-types"></a><a href="cpp#user-defined-types-note">user-defined types</a></th> </tr> <tr> <th></th> <th>c++</th> <th>objective c</th> <th>java</th> <th>c#</th> </tr> <tr> <td><a name="typedef" id="typedef"></a><a href="cpp#typedef-note">typedef</a></td> <td>typedef int customer_id;<br /> customer_id cid = 3;</td> <td>typedef int customer_id;<br /> customer_id cid = 3;</td> <td><span style="color: gray"><em>none</em></span></td> <td><span style="color: gray"><em>none</em></span></td> </tr> <tr> <td><a name="enum" id="enum"></a><a href="cpp#enum-note">enum</a></td> <td>enum day_of_week { mon, tue, wed, thu, fri, sat, sun };<br /> day_of_week d = tue;</td> <td>enum day_of_week { mon, tue, wed, thu, fri, sat, sun };<br /> enum day_of_week d = tue;</td> <td>public enum DayOfWeek { MON, TUE, WED, THU, FRI, SAT, SUN };<br /> DayOfWeek d = DayOfWeek.TUE;</td> <td>public enum DayOfWeek { MON, TUE, WED, THU, FRI, SAT, SUN };<br /> DayOfWeek d = DayOfWeek.TUE;</td> </tr> <tr> <td><a href="cpp#struct-definition">struct definition</a></td> <td>class MedalCount {<br /> public:<br /> <span style="white-space: pre-wrap;"> </span>const char *country;<br /> <span style="white-space: pre-wrap;"> </span>int gold;<br /> <span style="white-space: pre-wrap;"> </span>int silver;<br /> <span style="white-space: pre-wrap;"> </span>int bronze;<br /> };</td> <td>struct medal_count {<br /> <span style="white-space: pre-wrap;"> </span>const char* country;<br /> <span style="white-space: pre-wrap;"> </span>int gold;<br /> <span style="white-space: pre-wrap;"> </span>int silver;<br /> <span style="white-space: pre-wrap;"> </span>int bronze;<br /> };</td> <td>public class MedalCount {<br /> <span style="white-space: pre-wrap;"> </span>public String country;<br /> <span style="white-space: pre-wrap;"> </span>public int gold;<br /> <span style="white-space: pre-wrap;"> </span>public int silver;<br /> <span style="white-space: pre-wrap;"> </span>public int bronze;<br /> }</td> <td>public class MedalCount {<br /> <span style="white-space: pre-wrap;"> </span>public string country;<br /> <span style="white-space: pre-wrap;"> </span>public int gold;<br /> <span style="white-space: pre-wrap;"> </span>public int silver;<br /> <span style="white-space: pre-wrap;"> </span>public int bronze;<br /> }</td> </tr> <tr> <td><a href="cpp#struct-declaration">struct declaration</a></td> <td>MedalCount spain;</td> <td>struct medal_count spain;</td> <td>MedalCount spain = new MedalCount();</td> <td>MedalCount spain = new MedalCount();</td> </tr> <tr> <td><a href="cpp#struct-initialization">struct initialization</a></td> <td>MedalCount spain = { "Spain", 3, 7, 4 };</td> <td>struct medal_count spain = { "Spain", 3, 7, 4};<br /> struct medal_count france = { .gold = 8, .silver = 7, .bronze = 9, .country = "France" };</td> <td><span style="color: gray"><em>no object literal syntax; define a constructor</em></span></td> <td><span style="color: gray"><em>no object literal syntax; define a constructor</em></span></td> </tr> <tr> <td><a href="cpp#struct-member-assignment">struct member assignment</a></td> <td>spain.country = "Spain";<br /> spain.gold = 3;<br /> spain.silver = 7;<br /> spain.bronze = 4;</td> <td>spain.country = "Spain";<br /> spain.gold = 3;<br /> spain.silver = 7;<br /> spain.bronze = 4;</td> <td>spain.country = "Spain";<br /> spain.gold = 3;<br /> spain.silver = 7;<br /> spain.bronze = 4;</td> <td>spain.country = "Spain";<br /> spain.gold = 3;<br /> spain.silver = 7;<br /> spain.bronze = 4;</td> </tr> <tr> <td><a href="cpp#struct-member-access">struct member access</a></td> <td>int spain_total = spain.gold + spain.silver + spain.bronze;</td> <td>int spain_total = spain.gold + spain.silver + spain.bronze;</td> <td>int spain_total = spain.gold + spain.silver + spain.bronze;</td> <td>int spain_total = spain.gold + spain.silver + spain.bronze;</td> </tr> <tr> <th colspan="5"><a name="generic-types" id="generic-types"></a><a href="cpp#generic-types-note">generic types</a></th> </tr> <tr> <th></th> <th>c++</th> <th>objective c</th> <th>java</th> <th>c#</th> </tr> <tr> <td><a href="cpp#define-generic">define generic type</a></td> <td>template <class A><br /> class Foo {<br /> public:<br /> <span style="white-space: pre-wrap;"> </span>A a;<br /> <span style="white-space: pre-wrap;"> </span>Foo(A a);<br /> };<br /> <span style="white-space: pre-wrap;"> </span><br /> template <class A><br /> Foo<A>::Foo(A a) : a(a) {<br /> }</td> <td></td> <td>public class Foo<A> {<br /> <span style="white-space: pre-wrap;"> </span>public A a;<br /> <span style="white-space: pre-wrap;"> </span>public Foo(A a) {<br /> <span style="white-space: pre-wrap;"> </span><span style="white-space: pre-wrap;"> </span>this.a = a;<br /> <span style="white-space: pre-wrap;"> </span>}<br /> }</td> <td>public class Foo<A> {<br /> <span style="white-space: pre-wrap;"> </span>public A a;<br /> <span style="white-space: pre-wrap;"> </span>public Foo(A a) {<br /> <span style="white-space: pre-wrap;"> </span><span style="white-space: pre-wrap;"> </span>this.a = a;<br /> <span style="white-space: pre-wrap;"> </span>}<br /> }</td> </tr> <tr> <td><a href="cpp#instantiate-generic">instantiate generic type</a></td> <td>Foo<string> f = Foo<string>("foo");</td> <td></td> <td>Foo<String> f = new Foo<String>("foo");</td> <td>Foo<string> f = new Foo<string>("foo");</td> </tr> <tr> <td><a href="cpp#generic-function">generic function</a></td> <td>template <class C><br /> C add(C a, C b) {<br /> <span style="white-space: pre-wrap;"> </span>return a + b;<br /> }</td> <td></td> <td></td> <td></td> </tr> <tr> <td><a href="cpp#generic-array">generic array</a></td> <td>template <class C><br /> class Foo {<br /> public:<br /> <span style="white-space: pre-wrap;"> </span>C a[10];<br /> };</td> <td></td> <td><span style="color: gray"><em>not permitted. Use</em> Object <em>as the element type for the array or use an</em> ArrayList.</span></td> <td>public class Bar<C> {<br /> <span style="white-space: pre-wrap;"> </span>public C[] a;<br /> <span style="white-space: pre-wrap;"> </span>public Bar(C c) {<br /> <span style="white-space: pre-wrap;"> </span><span style="white-space: pre-wrap;"> </span>this.a = new C[10];<br /> <span style="white-space: pre-wrap;"> </span>}<br /> }</td> </tr> <tr> <td><a href="cpp#value-parameter">value parameter</a></td> <td>template <int N><br /> int add(int i) {<br /> <span style="white-space: pre-wrap;"> </span>return N+i;<br /> }<br /> <span style="white-space: pre-wrap;"> </span><br /> cout <span style="white-space: pre-wrap;"><<</span> add<7>(3) <span style="white-space: pre-wrap;"><<</span> endl;</td> <td></td> <td></td> <td></td> </tr> <tr> <td><a href="cpp#template-parameter">template parameter</a></td> <td></td> <td></td> <td></td> <td></td> </tr> <tr> <td><a href="cpp#template-specialization">template specialization</a></td> <td></td> <td></td> <td></td> <td></td> </tr> <tr> <td><a href="cpp#multiple-type-parameters">multiple type parameters</a></td> <td>template <class A, class B><br /> class Pair {<br /> public:<br /> <span style="white-space: pre-wrap;"> </span>A a;<br /> <span style="white-space: pre-wrap;"> </span>B b;<br /> <span style="white-space: pre-wrap;"> </span>Pair(A a, B b);<br /> };<br /> <span style="white-space: pre-wrap;"> </span><br /> template <class A, class B><br /> Pair<A, B>::Pair(A a, B b) :<br /> <span style="white-space: pre-wrap;"> </span>a(a), b(b) { }<br /> <span style="white-space: pre-wrap;"> </span><br /> Pair<int, string> p =<br /> <span style="white-space: pre-wrap;"> </span>Pair<int, string>(7, "foo");</td> <td></td> <td></td> <td></td> </tr> <tr> <td><a href="cpp#generic-type-parameters">generic type parameters</a></td> <td>Pair<int, Foo<string> > p =<br /> <span style="white-space: pre-wrap;"> </span>Pair<int, Foo<string> >(<br /> <span style="white-space: pre-wrap;"> </span><span style="white-space: pre-wrap;"> </span>7, Foo<string>("foo"));</td> <td></td> <td></td> <td></td> </tr> <tr> <td><a href="cpp#template-parameters">template parameters</a></td> <td></td> <td></td> <td></td> <td></td> </tr> <tr> <td>variadic template</td> <td></td> <td></td> <td></td> <td></td> </tr> <tr> <th colspan="5"><a name="objects" id="objects"></a><a href="cpp#objects-note">objects</a></th> </tr> <tr> <th></th> <th>c++</th> <th>objective c</th> <th>java</th> <th>c#</th> </tr> <tr> <td><a name="str-equal" id="str-equal"></a><a href="cpp#str-equal-note">semantics of ==</a></td> <td><span style="color: gray"><em>value comparison</em></span></td> <td><span style="color: gray"><em>object identity comparison</em></span></td> <td><span style="color: gray"><em>object identity comparison</em></span></td> <td><span style="color: gray"><em>value comparison</em></span></td> </tr> <tr> <td><a href="cpp#define-class">define class</a></td> <td><span style="color: gray"><em>Rational.hpp:</em></span><br /> class Rational {<br /> <span style="white-space: pre-wrap;"> </span>public:<br /> <span style="white-space: pre-wrap;"> </span>int num, denom;<br /> <span style="white-space: pre-wrap;"> </span>Rational(int num, int denom);<br /> <span style="white-space: pre-wrap;"> </span>virtual ~Rational();<br /> <span style="white-space: pre-wrap;"> </span>Rational operator+(Rational& addend);<br /> <span style="white-space: pre-wrap;"> </span>static Rational max(Rational& a, Rational& b);<br /> };</td> <td><span style="color: gray"><em>Rational.h:</em></span><br /> #import <Foundation/Foundation.h><br /> @interface Rational : NSObject {<br /> <span style="white-space: pre-wrap;"> </span>int num;<br /> <span style="white-space: pre-wrap;"> </span>int denom;<br /> }<br /> @property int num, denom;<br /> -(Rational*) initWith: (int) n: (int) d;<br /> -(Rational*) add: (Rational *) o;<br /> @end<br /> <span style="color: gray"><em>Rational.m:</em></span><br /> #include "Rational.h"<br /> @implementation Rational<br /> @synthesize num, denom;<br /> -(Rational*) add: (Rational*) o {<br /> <span style="white-space: pre-wrap;"> </span>int sum_n = self.num * o.denom + o.num * self.denom;<br /> <span style="white-space: pre-wrap;"> </span>int sum_d = self.denom * o.denom;<br /> <span style="white-space: pre-wrap;"> </span>Rational* sum = [[Rational alloc] initWith: sum_n: sum_d];<br /> <span style="white-space: pre-wrap;"> </span>return sum;<br /> }<br /> @end</td> <td>public class Rational {<br /> <span style="white-space: pre-wrap;"> </span>public int num;<br /> <span style="white-space: pre-wrap;"> </span>public int denom;<br /> <span style="white-space: pre-wrap;"> </span>public Rational add(Rational o) throws Exception {<br /> <span style="white-space: pre-wrap;"> </span><span style="white-space: pre-wrap;"> </span>return new Rational(this.num*o.denom + o.num*this.denom,this.denom*o.denom);<br /> <span style="white-space: pre-wrap;"> </span>}<br /> <span style="white-space: pre-wrap;"> </span>public static Rational max(Rational a, Rational b) {<br /> <span style="white-space: pre-wrap;"> </span><span style="white-space: pre-wrap;"> </span>return (a.num*b.denom > a.num*b.denom) ? a : b;<br /> <span style="white-space: pre-wrap;"> </span>}<br /> }</td> <td>public class Rational {<br /> <span style="white-space: pre-wrap;"> </span>public int num;<br /> <span style="white-space: pre-wrap;"> </span>public int denom;<br /> }</td> </tr> <tr> <td><a href="cpp#class-definition-location">class definition location</a></td> <td><span style="color: gray"><em>top level, class block, or function block</em></span></td> <td><span style="color: gray"><em>top level</em></span></td> <td><span style="color: gray"><em>top level, class block, or function block for anonymous classes</em></span></td> <td></td> </tr> <tr> <td><a href="cpp#constructor">constructor</a></td> <td>Rational::Rational(int n, int d) : num(n), denom(d) {<br /> <span style="white-space: pre-wrap;"> </span>if (denom == 0) {<br /> <span style="white-space: pre-wrap;"> </span><span style="white-space: pre-wrap;"> </span>throw "zero denominator";<br /> <span style="white-space: pre-wrap;"> </span>}<br /> <span style="white-space: pre-wrap;"> </span>int div = gcd(n,d);<br /> <span style="white-space: pre-wrap;"> </span>num = num / div;<br /> <span style="white-space: pre-wrap;"> </span>denom = denom / div;<br /> }</td> <td>-(Rational*) initWith: (int) n: (int) d {<br /> <span style="white-space: pre-wrap;"> </span>self = [super init];<br /> <span style="white-space: pre-wrap;"> </span>if (self) {<br /> <span style="white-space: pre-wrap;"> </span><span style="white-space: pre-wrap;"> </span>self.num = n;<br /> <span style="white-space: pre-wrap;"> </span><span style="white-space: pre-wrap;"> </span>self.denom = d;<br /> <span style="white-space: pre-wrap;"> </span>}<br /> <span style="white-space: pre-wrap;"> </span>return self;<br /> }</td> <td>public Rational(int n, int d) throws Exception {<br /> <span style="white-space: pre-wrap;"> </span>if (d == 0) {<br /> <span style="white-space: pre-wrap;"> </span><span style="white-space: pre-wrap;"> </span>throw new Exception("zero denominator");<br /> <span style="white-space: pre-wrap;"> </span>}<br /> <span style="white-space: pre-wrap;"> </span>if ( d < 0 ) {<br /> <span style="white-space: pre-wrap;"> </span><span style="white-space: pre-wrap;"> </span>this.num = -1 * n;<br /> <span style="white-space: pre-wrap;"> </span><span style="white-space: pre-wrap;"> </span>this.denom = -1 * d;<br /> <span style="white-space: pre-wrap;"> </span>}<br /> <span style="white-space: pre-wrap;"> </span>else {<br /> <span style="white-space: pre-wrap;"> </span><span style="white-space: pre-wrap;"> </span>this.num = n;<br /> <span style="white-space: pre-wrap;"> </span><span style="white-space: pre-wrap;"> </span>this.denom = d;<br /> <span style="white-space: pre-wrap;"> </span>}<br /> }</td> <td>public Rational(int n, int d) {<br /> <span style="white-space: pre-wrap;"> </span>if (0 == d) {<br /> <span style="white-space: pre-wrap;"> </span><span style="white-space: pre-wrap;"> </span>throw new System.Exception("zero denominator");<br /> <span style="white-space: pre-wrap;"> </span>}<br /> <span style="white-space: pre-wrap;"> </span>if (d < 0) {<br /> <span style="white-space: pre-wrap;"> </span><span style="white-space: pre-wrap;"> </span>this.num = -1 * n;<br /> <span style="white-space: pre-wrap;"> </span><span style="white-space: pre-wrap;"> </span>this.denom = -1 * d;<br /> <span style="white-space: pre-wrap;"> </span>}<br /> <span style="white-space: pre-wrap;"> </span>else {<br /> <span style="white-space: pre-wrap;"> </span><span style="white-space: pre-wrap;"> </span>this.num = n;<br /> <span style="white-space: pre-wrap;"> </span><span style="white-space: pre-wrap;"> </span>this.denom = d;<br /> <span style="white-space: pre-wrap;"> </span>}<br /> }</td> </tr> <tr> <td><a href="cpp#create-object">create object</a></td> <td>Rational r1(7, 3);<br /> Rational* r2 = new Rational(8, 5);</td> <td>Rational *r = [[Rational alloc] initWith: 7: 3];</td> <td>Rational r = new Rational(7,3);</td> <td>Rational r = new Rational(7,3);</td> </tr> <tr> <td><a href="cpp#destructor">destructor</a></td> <td>Rational::~Rational() {};</td> <td>-(void) dealloc {<br /> <span style="white-space: pre-wrap;"> </span>[super dealloc];<br /> <span style="white-space: pre-wrap;"> </span>printf("deallocated…");<br /> }</td> <td>protected void finalize() throws Throwable {<br /> <span style="white-space: pre-wrap;"> </span>super.finalize();<br /> }</td> <td>~Rational() {<br /> <span style="white-space: pre-wrap;"> </span><span style="color: gray"><em>perform cleanup</em></span><br /> }</td> </tr> <tr> <td><a href="cpp#destroy-object">destroy object</a><br /> <span style="white-space: pre-wrap;"> </span></td> <td>delete r2;</td> <td>[r release];</td> <td><span style="color: gray"><em>none</em></span></td> <td><span style="color: gray"><em>none</em></span></td> </tr> <tr> <td><a href="cpp#define-method">define method</a></td> <td>int Rational::height() {<br /> <span style="white-space: pre-wrap;"> </span>return (abs(num) > abs(denom)) ? abs(num) : abs(denom);<br /> }</td> <td>-(int) height {<br /> <span style="white-space: pre-wrap;"> </span>if ( abs(self.num) > abs(self.denom) ) {<br /> <span style="white-space: pre-wrap;"> </span><span style="white-space: pre-wrap;"> </span>return abs(self.num);<br /> <span style="white-space: pre-wrap;"> </span>}<br /> <span style="white-space: pre-wrap;"> </span>return abs(self.denom);<br /> }</td> <td>public int height() {<br /> <span style="white-space: pre-wrap;"> </span>return (Math.abs(this.num) > this.denom) ? Math.abs(this.num) : this.denom;<br /> }</td> <td>public int Height() {<br /> <span style="white-space: pre-wrap;"> </span>return (System.Math.Abs(this.num) > this.denom) ? System.Math.Abs(this.num) : this.denom;<br /> }</td> </tr> <tr> <td><a href="cpp#invoke-method">invoke method</a></td> <td>r1.height();<br /> r2->height();</td> <td>[r1 height];</td> <td>r.height();</td> <td>r.Height();</td> </tr> <tr> <td><a href="cpp#define-class-method">define class method</a></td> <td><span style="color: gray"><em>declare static in class definition</em></span></td> <td><span style="color: gray"><em>precede definition with +:</em></span><br /> +(Rational*) max: (Rational*) a: (Rational*) b {<br /> <span style="white-space: pre-wrap;"> </span>if ( a.num * b.denom > b.num * a.denom ) {<br /> <span style="white-space: pre-wrap;"> </span><span style="white-space: pre-wrap;"> </span>return a;<br /> <span style="white-space: pre-wrap;"> </span>}<br /> <span style="white-space: pre-wrap;"> </span>return b;<br /> }</td> <td><span style="color: gray"><em>declare static in class definition</em></span></td> <td><span style="color: gray"><em>declare static in class definition</em></span></td> </tr> <tr> <td><a href="cpp#invoke-class-method">invoke class method</a></td> <td></td> <td></td> <td></td> <td></td> </tr> <tr> <td><a href="cpp#receiver">name of receiver</a></td> <td>this</td> <td>self</td> <td>this</td> <td>this</td> </tr> <tr> <td><a href="cpp#access-control">access control</a></td> <td><span style="color: gray"><em>access keywords define regions:</em></span><br /> class Foo {<br /> <span style="white-space: pre-wrap;"> </span>int privateInt1;<br /> <span style="white-space: pre-wrap;"> </span>int privateInt2;<br /> public:<br /> <span style="white-space: pre-wrap;"> </span>int publicInt1;<br /> <span style="white-space: pre-wrap;"> </span>int publicInt2;<br /> protected:<br /> <span style="white-space: pre-wrap;"> </span>int protectedInt1;<br /> <span style="white-space: pre-wrap;"> </span>int protectedInt2;<br /> private:<br /> <span style="white-space: pre-wrap;"> </span>int privateInt3;<br /> <span style="white-space: pre-wrap;"> </span>int privateInt4;<br /> };</td> <td><span style="color: gray"><em>access keywords define regions:</em></span><br /> @interface Foo : NSObject {<br /> <span style="white-space: pre-wrap;"> </span>int protectedInt1;<br /> <span style="white-space: pre-wrap;"> </span>int protectedInt2;<br /> @public<br /> <span style="white-space: pre-wrap;"> </span>int publicInt1;<br /> <span style="white-space: pre-wrap;"> </span>int publicInt2;<br /> @protected<br /> <span style="white-space: pre-wrap;"> </span>int protectedInt3;<br /> <span style="white-space: pre-wrap;"> </span>int protectedInt4;<br /> @private<br /> <span style="white-space: pre-wrap;"> </span>int privateInt1;<br /> <span style="white-space: pre-wrap;"> </span>int privateInt2;<br /> }<br /> @end</td> <td><span style="color: gray"><em>access keywords required for methods and members:</em></span><br /> public class Foo {<br /> <span style="white-space: pre-wrap;"> </span>private int privateInt;<br /> <span style="white-space: pre-wrap;"> </span>protected int protectedInt;<br /> <span style="white-space: pre-wrap;"> </span>public int publicInt;<br /> }</td> <td><span style="color: gray"><em>access keywords available for methods and members:</em></span><br /> public class Foo {<br /> <span style="white-space: pre-wrap;"> </span>private int privateInt1;<br /> <span style="white-space: pre-wrap;"> </span>int privateInt2;<br /> <span style="white-space: pre-wrap;"> </span>protected int protectedInt;<br /> <span style="white-space: pre-wrap;"> </span>public int publicInt;<br /> }</td> </tr> <tr> <td><a href="cpp#anonymous-class">anonymous class</a></td> <td><span style="color: gray"><em>possible but not useful</em></span></td> <td><span style="color: gray"><em>none</em></span></td> <td>(new Object() { public void hello() { System.out.println("hello!"); } }).hello();</td> <td></td> </tr> <tr> <th colspan="5"><a name="inheritance-polymorphism" id="inheritance-polymorphism"></a><a href="cpp#inheritance-polymorphism-note">inheritance and polymorphism</a></th> </tr> <tr> <th></th> <th>c++</th> <th>objective c</th> <th>java</th> <th>c#</th> </tr> <tr> <td><a href="cpp#dynamic-dispatch">dynamic dispatch</a></td> <td><span style="color: gray"><em>declare as virtual in base class</em></span></td> <td><span style="color: gray"><em>dispatch always dynamic</em></span></td> <td><span style="color: gray"><em>dispatch dynamic by default</em></span></td> <td><span style="color: gray"><em>declare as virtual in base class and override in derived class</em></span></td> </tr> <tr> <td><a href="cpp#static-dispatch">static dispatch</a></td> <td><span style="color: gray"><em>dispatch static by default</em></span></td> <td><span style="color: gray"><em>dispatch always dynamic</em></span></td> <td><span style="color: gray"><em>declare as final, private, or static (i.e. make it a class method)</em></span></td> <td><span style="color: gray"><em>dispatch static by default; compiler error if same method defined in base and derived class and not marked virtual in base class</em></span></td> </tr> <tr> <td><a href="cpp#subclass">subclass</a></td> <td>class Integer : public Rational {<br /> <span style="white-space: pre-wrap;"> </span>public:<br /> <span style="white-space: pre-wrap;"> </span>Integer(int n);<br /> <span style="white-space: pre-wrap;"> </span>virtual ~Integer();<br /> };</td> <td></td> <td>public class RInteger extends Rational {<br /> <span style="white-space: pre-wrap;"> </span>public RInteger(int n) throws Throwable {<br /> <span style="white-space: pre-wrap;"> </span><span style="white-space: pre-wrap;"> </span>super(n, 1);<br /> <span style="white-space: pre-wrap;"> </span>}<br /> }</td> <td></td> </tr> <tr> <td><a href="cpp#superclass-constructor">invoking superclass constructor</a></td> <td>Integer::Integer(int n) : Rational(n, 1) {<br /> }</td> <td></td> <td>super(n, 1);</td> <td></td> </tr> <tr> <td><a href="cpp#underivable-class">mark class underivable or method unoverrideable</a></td> <td><span style="color: gray"><em>none</em></span></td> <td><span style="color: gray"><em>none</em></span></td> <td>final</td> <td>sealed</td> </tr> <tr> <td><a href="cpp#root-class">root class</a><br /> <span style="white-space: pre-wrap;"> </span></td> <td><span style="color: gray"><em>none</em></span></td> <td>NSObject</td> <td>java.lang.Object</td> <td>System.Object</td> </tr> <tr> <td><a href="cpp#root-class-methods">root class methods</a></td> <td><span style="color: gray"><em>none</em></span></td> <td>autorelease<br /> class<br /> conformsToProtocol:<br /> hash<br /> isEqual:<br /> isKindOfClass:<br /> isProxy<br /> performSelector:<br /> performSelector:withObject:<br /> performSelector:withObject:withObject:<br /> release<br /> respondsToSelector:<br /> retain<br /> retainCount<br /> self<br /> superclass</td> <td>clone()<br /> equals()<br /> finalize()<br /> getClass()<br /> hashCode()<br /> toString()</td> <td>Equals()<br /> Finalize()<br /> GetHashCode()<br /> GetType()<br /> MemberwiseClone()<br /> ReferenceEquals()<br /> ToString()</td> </tr> <tr> <th colspan="5"><a name="reflection" id="reflection"></a><a href="cpp#reflection-note">reflection</a></th> </tr> <tr> <th></th> <th>c++</th> <th>objective c</th> <th>java</th> <th>c#</th> </tr> <tr> <td><a href="cpp#type-class">get type class of object</a></td> <td></td> <td></td> <td>o = new Object();<br /> Class c = o.getClass();</td> <td>object o = new object();<br /> System.Type t = o.GetType();<br /> <span style="color: gray"><em>or</em></span><br /> System.type t = typeof(o);</td> </tr> <tr> <td><a href="cpp#get-type-class-string">get type class from string</a></td> <td></td> <td></td> <td>Class c = Class.forName("java.io.File");</td> <td>using System;<br /> Type t = Type.GetType("object");</td> </tr> <tr> <td><a href="cpp#get-type-class-identifier">get type class from type identifier</a></td> <td>typeid(Foo)</td> <td></td> <td></td> <td>System.Type t = typeof(object);</td> </tr> <tr> <td><a href="cpp#class-name">class name</a><br /> <span style="white-space: pre-wrap;"> </span></td> <td>typeid(Foo).name()</td> <td></td> <td>String name = c.getName();</td> <td>t.ToString();</td> </tr> <tr> <td><a href="cpp#get-methods">get methods</a></td> <td></td> <td></td> <td>import java.lang.reflect.*;<br /> Method[] m = c.getMethods();</td> <td>using System.Reflection;<br /> System.Type t = typeof(object);<br /> MethodInfo[] a = t.GetMethods();</td> </tr> <tr> <td><a href="cpp#has-method">has method</a></td> <td></td> <td></td> <td>import java.lang.reflect.*;<br /> Class c = Class.forName("java.io.File");<br /> Method[] a = c.getMethods();<br /> boolean hasMethod = false;<br /> for (int i=0; i < a.length; i++) {<br /> <span style="white-space: pre-wrap;"> </span>if (a[i].getName() == "toString") {<br /> <span style="white-space: pre-wrap;"> </span><span style="white-space: pre-wrap;"> </span>hasMethod = true;<br /> <span style="white-space: pre-wrap;"> </span>}<br /> }</td> <td><span style="color: gray"><em>null if method not found:</em></span><br /> MethodInfo m = t.GetMethod("ToString");</td> </tr> <tr> <td><a href="cpp#invoke-method-object">invoke method object</a></td> <td></td> <td></td> <td>import java.lang.reflect.*;<br /> Class c = Class.forName("java.io.File");<br /> Method m = c.getMethod("toString");<br /> Object o = new Object();<br /> m.invoke(o);</td> <td>m.Invoke(o);</td> </tr> <tr> <th colspan="5"><a name="net-web" id="net-web"></a><a href="cpp#net-web-note">net and web</a></th> </tr> <tr> <th></th> <th>c++</th> <th>objective c</th> <th>java</th> <th>c#</th> </tr> <tr> <td>get local hostname, dns lookup, reverse dns lookup</td> <td></td> <td></td> <td></td> <td></td> </tr> <tr> <td>http get</td> <td></td> <td></td> <td></td> <td></td> </tr> <tr> <td>http post</td> <td></td> <td></td> <td></td> <td></td> </tr> <tr> <td>absolute url</td> <td></td> <td></td> <td></td> <td></td> </tr> <tr> <td>parse url</td> <td></td> <td></td> <td></td> <td></td> </tr> <tr> <td><a name="url-encode" id="url-encode"></a><a href="cpp#url-encode-note">url encode/decode</a></td> <td></td> <td></td> <td>import java.net.URLEncoder;<br /> import java.net.URLDecoder;<br /> <br /> String url = "<span style="white-space: pre-wrap;">http://www.google.com</span>";<br /> String s = URLEncoder.encode(url, "utf8");<br /> String s2 = URLDecoder.decode(s, "utf8");</td> <td></td> </tr> <tr> <td>base64 encode/decode</td> <td></td> <td></td> <td></td> <td></td> </tr> <tr> <th colspan="5"><a name="unit-tests" id="unit-tests"></a><a href="cpp#unit-tests-note">unit tests</a></th> </tr> <tr> <th></th> <th>c++</th> <th>objective c</th> <th>java</th> <th>c#</th> </tr> <tr> <td><a name="test-class" id="test-class"></a><a href="cpp#test-class-note">test class</a></td> <td>$ cat > test_foo.cpp<br /> #include <cppunit/TestCaller.h><br /> #include <cppunit/TestCase.h><br /> #include <cppunit/TestSuite.h><br /> #include "test_foo.h"<br /> <br /> using namespace CppUnit;<br /> <br /> void TestFoo::test_01() {<br /> <span style="white-space: pre-wrap;"> </span>CPPUNIT_ASSERT_EQUAL(1, 1);<br /> }<br /> <br /> Test* TestFoo::suite() {<br /> <span style="white-space: pre-wrap;"> </span>TestSuite* suiteOfTests = new TestSuite("Foo");<br /> <span style="white-space: pre-wrap;"> </span>suiteOfTests->addTest(<br /> <span style="white-space: pre-wrap;"> </span>new TestCaller<TestFoo>(<br /> <span style="white-space: pre-wrap;"> </span><span style="white-space: pre-wrap;"> </span>"test_01",<br /> <span style="white-space: pre-wrap;"> </span><span style="white-space: pre-wrap;"> </span>&TestFoo::test_01));<br /> <br /> <span style="white-space: pre-wrap;"> </span>return suiteOfTests;<br /> }<br /> <br /> $ cat > test_foo.h<br /> #include <cppunit/TestCase.h><br /> <br /> class TestFoo: public CppUnit::TestCase {<br /> public:<br /> <span style="white-space: pre-wrap;"> </span>void test_01();<br /> <br /> <span style="white-space: pre-wrap;"> </span>static CppUnit::Test* suite();<br /> };</td> <td></td> <td></td> <td></td> </tr> <tr> <td><a name="run-all-tests" id="run-all-tests"></a><a href="cpp#run-all-tests-note">run all tests</a></td> <td>$ cat > test_runner.cpp<br /> #include <cppunit/ui/text/TestRunner.h><br /> #include "test_foo.h"<br /> <br /> int main( int argc, char** argv)<br /> {<br /> <span style="white-space: pre-wrap;"> </span>CppUnit::TextUi::TestRunner runner;<br /> <span style="white-space: pre-wrap;"> </span>runner.addTest(TestFoo::suite());<br /> <span style="white-space: pre-wrap;"> </span>runner.run();<br /> <span style="white-space: pre-wrap;"> </span>return 0;<br /> }<br /> <br /> $ sudo apt-get install libcppunit-dev<br /> <br /> $ cat > Makefile<br /> test_runner: test_runner.o test_foo.o<br /> <span style="white-space: pre-wrap;"> </span><span style="white-space: pre-wrap;"> </span><span style="white-space: pre-wrap;"> </span><span style="white-space: pre-wrap;"> </span>g++ -o $@ $^ -lcppunit<br /> <br /> check: test_runner<br /> <span style="white-space: pre-wrap;"> </span><span style="white-space: pre-wrap;"> </span><span style="white-space: pre-wrap;"> </span><span style="white-space: pre-wrap;"> </span>./test_runner<br /> <br /> $ make check</td> <td></td> <td></td> <td></td> </tr> <tr> <td><a name="assert-equal" id="assert-equal"></a><a href="cpp#assert-equal-note">equality assertion</a></td> <td>#include <cppunit/TestCase.h><br /> <br /> CPPUNIT_ASSERT_EQUAL(1, 1);<br /> CPPUNIT_ASSERT_EQUAL("foo", "bar");<br /> CPPUNIT_ASSERT_EQUAL_MESSAGE("1 != 1",<br /> <span style="white-space: pre-wrap;"> </span>1, 1);</td> <td></td> <td></td> <td></td> </tr> <tr> <td><a name="assert-approx" id="assert-approx"></a><a href="cpp#assert-approx-note">approximate assertion</a></td> <td></td> <td></td> <td></td> <td></td> </tr> <tr> <td><a name="assert-exc" id="assert-exc"></a><a href="cpp#assert-exc-note">exception assertion</a></td> <td></td> <td></td> <td></td> <td></td> </tr> <tr> <td><a name="setup" id="setup"></a><a href="cpp#setup-note">setup</a></td> <td></td> <td></td> <td></td> <td></td> </tr> <tr> <td><a name="teardown" id="teardown"></a><a href="cpp#teardown-note">teardown</a></td> <td></td> <td></td> <td></td> <td></td> </tr> <tr> <th colspan="5"><a name="debugging-profiling" id="debugging-profiling"></a><a href="cpp#debugging-profiling-note">debugging and profiling</a></th> </tr> <tr> <th></th> <th>c++</th> <th>objective c</th> <th>java</th> <th>c#</th> </tr> <tr> <td>flag for stronger warnings</td> <td>$ g++ -Wall foo.cpp</td> <td></td> <td></td> <td></td> </tr> <tr> <td>suppress warnings</td> <td>$ g++ -w foo.cpp</td> <td></td> <td></td> <td></td> </tr> <tr> <td>treat warnings as errors</td> <td>$ g++ -Werror foo.cpp</td> <td></td> <td></td> <td></td> </tr> <tr> <td>run debugger</td> <td>$ g++ -g -o foo foo.cpp<br /> <br /> $ gdb foo<br /> (gdb) b main<br /> (gdb) run</td> <td></td> <td></td> <td></td> </tr> <tr> <td><a name="debugger-cmds" id="debugger-cmds"></a><a href="cpp#debugger-cmds-note">debugger commands</a><br /> <span style="color: gray"><em>help, list source, (re)load executable, next, step, set breakpoint, show breakpoints, delete breakpoint, continue, backtrace, up stack, down stack, print, run, quit</em></span></td> <td><span style="white-space: pre-wrap;">></span> h<br /> <span style="white-space: pre-wrap;">></span> l <span style="color: gray">[FIRST_LINENO, LAST_LINENO]</span><br /> <span style="white-space: pre-wrap;">></span> file <span style="color: gray">PATH</span><br /> <span style="white-space: pre-wrap;">></span> n<br /> <span style="white-space: pre-wrap;">></span> s<br /> <span style="white-space: pre-wrap;">></span> b <span style="color: gray">[FILE:]LINENO</span><br /> <span style="white-space: pre-wrap;">></span> i<br /> <span style="white-space: pre-wrap;">></span> d <span style="color: gray">NUM</span><br /> <span style="white-space: pre-wrap;">></span> c<br /> <span style="white-space: pre-wrap;">></span> bt<br /> <span style="white-space: pre-wrap;">></span> up<br /> <span style="white-space: pre-wrap;">></span> do<br /> <span style="white-space: pre-wrap;">></span> p <span style="color: gray">EXPR</span><br /> <span style="white-space: pre-wrap;">></span> r <span style="color: gray">[ARG1[, [ARG2 <span style="white-space: pre-wrap;">...</span>]]</span><br /> <span style="white-space: pre-wrap;">></span> q</td> <td></td> <td></td> <td></td> </tr> <tr> <td>benchmark code</td> <td></td> <td></td> <td></td> <td></td> </tr> <tr> <td>profile code</td> <td><span style="color: gray"><em>gprof does not work on Mac OS X:</em></span><br /> <br /> $ g++ -pg -o foo foo.cpp<br /> <br /> $ ./foo<br /> <br /> $ gprof foo</td> <td></td> <td></td> <td></td> </tr> <tr> <td>memory tool</td> <td>$ sudo apt-get install valgrind<br /> <br /> $ g++ -o foo foo.cpp<br /> <br /> $ valgrind ./foo</td> <td></td> <td></td> <td></td> </tr> <tr> <th></th> <th><span style="color: #efefef"><span style="white-space: pre-wrap;">_______________________________________________</span></span></th> <th><span style="color: #efefef"><span style="white-space: pre-wrap;">_______________________________________________</span></span></th> <th><span style="color: #efefef"><span style="white-space: pre-wrap;">_______________________________________________</span></span></th> <th><span style="color: #efefef"><span style="white-space: pre-wrap;">_______________________________________________</span></span></th> </tr> </table> <p><a name="version-note" id="version-note"></a></p> <h1 id="toc0"><span><a href="cpp#version">Version</a></span></h1> <p><a name="version-used-note" id="version-used-note"></a></p> <h2 id="toc1"><span><a href="cpp#version-used">version used</a></span></h2> <p>The compiler version used for this sheet.</p> <p><a name="show-version-note" id="show-version-note"></a></p> <h2 id="toc2"><span><a href="cpp#show-version">show version</a></span></h2> <p>How to get the compiler version.</p> <p><a name="implicit-prologue-note" id="implicit-prologue-note"></a></p> <h2 id="toc3"><span><a href="cpp#implicit-prologue">implicit prologue</a></span></h2> <p>Code which examples in the sheet assume to have already been executed.</p> <p><a name="grammar-execution-note" id="grammar-execution-note"></a></p> <h1 id="toc4"><span><a href="cpp#grammar-execution">Grammar and Execution</a></span></h1> <p><a name="hello-world-note" id="hello-world-note"></a></p> <h2 id="toc5"><span><a href="cpp#hello-world">hello world</a></span></h2> <p>How to write, compile, and run a "Hello, World!" program.</p> <p><a name="file-suffixes-note" id="file-suffixes-note"></a></p> <h2 id="toc6"><span><a href="cpp#file-suffixes">file suffixes</a></span></h2> <p>For source files, header files, and compiled object files.</p> <p><strong>C++</strong></p> <p>The <tt>gcc</tt> compiler will treat a file with any of the following suffixes as C++ source:</p> <div class="code"> <pre> <code> .cc .cp .cxx .cpp .CPP .c++ .C</code> </pre></div> <p>GNU <tt>make</tt> has built-in rules which treat the following suffixes as C++ source:</p> <div class="code"> <pre> <code>.cc .C .cpp</code> </pre></div> <p>The Google C++ Style Guide recommends that <tt>.cc</tt> be used as the suffix for C++ source files. Visual Studio uses <tt>.cpp</tt> as the C++ source file suffix.</p> <p>One sometimes sees suffixes for headers which distinguish C++ code from C code, but the Google C++ Style Guide and Visual Studio both use <tt>.h</tt> as the C++ header suffix.</p> <p>The C++11 standard library uses no suffix at all in headers, at least in the <tt>#include</tt> statements. This change was made so that the new and the old standard library headers could be distributed together and new headers could have the same basename as the old headers.</p> <p><a name="block-delimiters-note" id="block-delimiters-note"></a></p> <h2 id="toc7"><span><a href="cpp#block-delimiters">block delimiters</a></span></h2> <p>How blocks are delimited.</p> <p>A block contains a sequence of statements. Blocks for function bodies in function definitions; to define the branches of <tt>if</tt> statements and the bodies of <tt>while</tt> loops.</p> <p>Class definition bodies are blocks, though the statements that appear in them are restricted to declarations and definitions.</p> <p>Bare blocks can be used to limit the scope of variables which are declared inside them.</p> <p><a name="stmt-terminator-note" id="stmt-terminator-note"></a></p> <h2 id="toc8"><span><a href="cpp#stmt-terminator">statement terminator</a></span></h2> <p>How statements are terminated.</p> <p><a name="top-level-stmt-note" id="top-level-stmt-note"></a></p> <h2 id="toc9"><span><a href="cpp#top-level-stmt">top level statements</a></span></h2> <p>Statements that can appear at the top level of a source file.</p> <p><a name="eol-comment-note" id="eol-comment-note"></a></p> <h2 id="toc10"><span><a href="cpp#eol-comment">end-of-line comment</a></span></h2> <p>The syntax for a comment which is terminated by the end of the line.</p> <p><a name="multiple-line-comment-note" id="multiple-line-comment-note"></a></p> <h2 id="toc11"><span><a href="cpp#multiple-line-comment">multiple line comment</a></span></h2> <p>The syntax for a comment which can span multiple lines.</p> <p>The <tt>/* */</tt> delimiters do not nest. Using them to comment out code which already contains a <tt>/* */</tt> comment usually results in a syntax error.</p> <p><a name="variables-expressions-note" id="variables-expressions-note"></a></p> <h1 id="toc12"><span><a href="cpp#variables-expressions">Variables and Expressions</a></span></h1> <p><a name="local-var-note" id="local-var-note"></a></p> <h2 id="toc13"><span><a href="cpp#local-var">local variable</a></span></h2> <p>How to declare a variable which is allocated on the stack.</p> <p><a name="uninitialized-local-var-note" id="uninitialized-local-var-note"></a></p> <h2 id="toc14"><span><a href="cpp#uninitialized-local-var">uninitialized local variable</a></span></h2> <p>The value contained by a local variable that wasn't initialized.</p> <p><a name="global-var-note" id="global-var-note"></a></p> <h2 id="toc15"><span><a href="cpp#global-var">global variable</a></span></h2> <p>How to declare a global variable.</p> <p><a name="uninitialized-global-var-note" id="uninitialized-global-var-note"></a></p> <h2 id="toc16"><span><a href="cpp#uninitialized-global-var">uninitialized global variable</a></span></h2> <p>The value assigned to an uninitialized global variable.</p> <p><a name="write-once-var-note" id="write-once-var-note"></a></p> <h2 id="toc17"><span><a href="cpp#write-once-var">write-once variable</a></span></h2> <p>How to declare a constant variable.</p> <p><a name="assignment-note" id="assignment-note"></a></p> <h2 id="toc18"><span><a href="cpp#assignment">assignment</a></span></h2> <p>How to assign a value to a variable.</p> <p><a name="compound-assignment-note" id="compound-assignment-note"></a></p> <h2 id="toc19"><span><a href="cpp#compound-assignment">compound assignment</a></span></h2> <p>The compound assignment operators.</p> <p>If <tt><OP></tt> is a binary operator and the language has the compound assignment operator <tt><OP>=</tt>, then the following are equivalent:</p> <div class="code"> <pre> <code>x <OP>= y x = x <OP> y</code> </pre></div> <p><a name="incr-decr-note" id="incr-decr-note"></a></p> <h2 id="toc20"><span><a href="cpp#incr-decr">increment and decrement</a></span></h2> <p>The C-style increment and decrement operators.</p> <p>There are prefix (preincrement and predecrement) and postfix (postincrement and postdecrement) versions. The prefix version returns the value after mutation, and the postfix version returns the value before mutation.</p> <p><strong>c++</strong></p> <p>Since the compound assignment operators also return the value after mutation, the following are equivalent for primitive types:</p> <div class="code"> <pre> <code>cout << ++i << endl; cout << (i += 1) << endl;</code> </pre></div> <p>The parens are necessary because compound assignment has lower precedence than the <tt><span style="white-space: pre-wrap;"><<</span></tt> operator.</p> <p>The caveat about primitive types is necessary because the <tt>++</tt> operator can be overloaded. In fact the prefix and postfix versions can be overloaded separately.</p> <p>In the case of the the postfix operator, the following are equivalent for primitive types:</p> <div class="code"> <pre> <code>cout << i-- << endl; cout << (i += 1, i - 1) << endl;</code> </pre></div> <p>The compiler may have to allocate a temporary variable to hold the value of <tt>i - 1</tt>, which means the postfix version might be slower.</p> <p><a name="addr-note" id="addr-note"></a></p> <h2 id="toc21"><span><a href="cpp#addr">address</a></span></h2> <p>How to get the memory address for a variable. Memory addresses are stored in a type which records the type of the variable whose address was taken.</p> <p><a name="dereference-note" id="dereference-note"></a></p> <h2 id="toc22"><span><a href="cpp#dereference">dereference</a></span></h2> <p>How to get the value stored at a memory address.</p> <p><a name="type-size-note" id="type-size-note"></a></p> <h2 id="toc23"><span><a href="cpp#type-size">type size</a></span></h2> <p>How to get the size of a type in bytes.</p> <p><a name="allocate-heap-note" id="allocate-heap-note"></a></p> <h2 id="toc24"><span><a href="cpp#allocate-heap">allocate heap</a></span></h2> <p>How to allocate memory for a primitive type on the heap.</p> <p><strong>C++</strong></p> <p><em>new</em> and <em>delete</em> can be used to manage the memory of both primitive types and objects.</p> <p><strong>objective c</strong></p> <p>Object C has a different memory management schemes for primitive types and objects. Objects are allocated with <em>alloc</em> and freed by means of <em>NSAutoreleasePool</em>. For primitive types the same techniques are used as for C. However, idiomatic Objective C will declare primitive types as local variables or as part of the state of an object and avoid explicit calls to <em>malloc</em>.</p> <p>Arrays of objects can be created with <em>NSArray</em> and <em>NSMutableArray</em>.</p> <p><strong>java</strong></p> <p>In Java, arrays are always stored on the heap and the JVM is responsible for garbage collection. The primitive types are stored (1) on the local frame, (2) as part of the state of an object, or (3) as part of the state of a class. The primitive types are never stored in the heap directly and when they are part of object state they are garbage collected with the object. Primitive types are passed by value unless they are encapsulated in an object.</p> <p>Each of the primitive types has a wrapper class, and instantiating this class is the best approximation in Java to allocating the primitive type on the heap:</p> <div class="code"> <pre> <code>Integer i = new Integer(0);</code> </pre></div> <p>The compiler may instantiate the wrapper class implicitly; this is called boxing. The compiler also permits use of a wrapper class in the place of the primitive type, or unboxing.</p> <p><strong>C#</strong></p> <p>C# behavior is like Java. Note that C# lacks specific wrapper classes for each primitive data type.</p> <p><a name="free-heap-note" id="free-heap-note"></a></p> <h2 id="toc25"><span><a href="cpp#free-heap">free heap</a></span></h2> <p>How to free the memory for a primitive type that was allocated on the heap.</p> <p><a name="null-note" id="null-note"></a></p> <h2 id="toc26"><span><a href="cpp#null">null</a></span></h2> <p><strong>C++</strong></p> <p>A typical definition:</p> <div class="code"> <pre> <code>const int NULL = 0;</code> </pre></div> <p><a name="coalesce-note" id="coalesce-note"></a></p> <h2 id="toc27"><span><a href="cpp#coalesce">coalesce</a></span></h2> <p>The equivalent of the COALESCE function from SQL.</p> <p><strong>C++, Objective C++:</strong></p> <p>The short circuit or operator <span style="white-space: pre-wrap;">||</span> can be used as a coalesce operator. However, in C++ and Objective C, NULL is identical to zero, whereas in databases they are two distinct values.</p> <p><strong>Java:</strong></p> <p>The ternary operator provides the closest approximation to COALESCE, but it does not have the same behavior if the tested value has a side effect.</p> <p><a name="arithmetic-logic-note" id="arithmetic-logic-note"></a></p> <h1 id="toc28"><span><a href="cpp#arithmetic-logic">Arithmetic and Logic</a></span></h1> <p><a name="boolean-type-note" id="boolean-type-note"></a></p> <h2 id="toc29"><span><a href="cpp#boolean-type">boolean type</a></span></h2> <p><strong>C</strong></p> <p>The following definitions are common:</p> <div class="code"> <pre> <code>typedef int BOOL; #define TRUE 1 #define FALSE 0</code> </pre></div> <p><strong>Objective C</strong></p> <p>From objc.h:</p> <div class="code"> <pre> <code>typedef signed char BOOL; #define YES (BOOL)1 #define NO (BOOL)0</code> </pre></div> <p><strong>C#</strong></p> <p>bool is an alias for System.Boolean</p> <p><a name="true-false-note" id="true-false-note"></a></p> <h2 id="toc30"><span><a href="cpp#true-false">true and false</a></span></h2> <p>Literals for the boolean values true and false.</p> <p><strong>C</strong></p> <p>The following definitions are common:</p> <div class="code"> <pre> <code>typedef int BOOL; #define TRUE 1 #define FALSE 0</code> </pre></div> <p><strong>Objective C</strong></p> <p>From objc.h:</p> <div class="code"> <pre> <code>typedef signed char BOOL; #define YES (BOOL)1 #define NO (BOOL)0</code> </pre></div> <p><a name="falsehoods-note" id="falsehoods-note"></a></p> <h2 id="toc31"><span><a href="cpp#falsehoods">falsehoods</a></span></h2> <p>Values which evaluate as false in the conditional expression of an <tt>if</tt> statement.</p> <p><a name="logical-op-note" id="logical-op-note"></a></p> <h2 id="toc32"><span><a href="cpp#logical-op">logical operators</a></span></h2> <p>The logical operators.</p> <p>In all languages on this sheet the && and <span style="white-space: pre-wrap;">||</span> operators short circuit: i.e. && will not evaluate the 2nd argument if the 1st argument is false, and <span style="white-space: pre-wrap;">||</span> will not evaluate the 2nd argument if the 1st argument is true. If the 2nd argument is not evaluated, side-effects that it contains are not executed.</p> <p><strong>C++</strong></p> <p>C++ defines <em>and</em>, <em>or</em>, and <em>not</em> to be synonyms of &&, <span style="white-space: pre-wrap;">||</span>, and !, with the same semantics and precedence.</p> <p><strong>Java</strong></p> <p>The arguments of the logical operators must be of type <em>boolean</em>.</p> <p><strong>C#</strong></p> <p>The arguments of the logical operators must be of type <em>bool</em>.</p> <p><a name="relational-op-note" id="relational-op-note"></a></p> <h2 id="toc33"><span><a href="cpp#relational-op">relational operators</a></span></h2> <p>Binary operators which return boolean values.</p> <p><a name="int-type-note" id="int-type-note"></a></p> <h2 id="toc34"><span><a href="cpp#int-type">integer type</a></span></h2> <p>Signed integer types.</p> <p><strong>C++</strong></p> <p>Whether <em>char</em> is a signed or unsigned type depends on the implementation.</p> <p><strong>C#</strong></p> <p>C# has the following aliases:</p> <p>sbyte: System.SByte<br /> short: System.Int16<br /> int: System.Int32<br /> long: System.Int64</p> <p><a name="unsigned-type-note" id="unsigned-type-note"></a></p> <h2 id="toc35"><span><a href="cpp#unsigned-type">unsigned type</a></span></h2> <p>Unsigned integer types.</p> <p><strong>C++</strong></p> <p>Whether <em>char</em> is a signed or unsigned type depends on the implementation.</p> <p><strong>C#</strong></p> <p>C# has the following aliases:</p> <p>byte: System.Byte<br /> ushort: System.UInt16<br /> uint: System.UInt32<br /> ulong: System.UInt64</p> <p><a name="float-type-note" id="float-type-note"></a></p> <h2 id="toc36"><span><a href="cpp#float-type">float type</a></span></h2> <p>Floating point types.</p> <p><strong>C#</strong></p> <p>C# has the following aliases:</p> <p>float: System.Single<br /> double: System.Double</p> <p><a name="fixed-type-note" id="fixed-type-note"></a></p> <h2 id="toc37"><span><a href="cpp#fixed-type">fixed type</a></span></h2> <p>Fixed-point decimal types.</p> <p><strong>C#:</strong></p> <p>C# has the following alias:</p> <p>decimal: System.Decimal</p> <p><a name="arithmetic-op-note" id="arithmetic-op-note"></a></p> <h2 id="toc38"><span><a href="cpp#arithmetic-op">arithmetic operators</a></span></h2> <p>The arithmetic binary operators: addition, subtraction, multiplication, division, modulus.</p> <p><a name="int-div-note" id="int-div-note"></a></p> <h2 id="toc39"><span><a href="cpp#int-div">integer division</a></span></h2> <p>How to get the quotient of two integers.</p> <p><a name="int-div-zero-note" id="int-div-zero-note"></a></p> <h2 id="toc40"><span><a href="cpp#int-div-zero">integer division by zero</a></span></h2> <p>The results of integer division by zero.</p> <p><strong>C++, Objective C</strong></p> <p>The behavior for division by zero is system dependent; the behavior described is nearly universal on Unix.</p> <p><strong>C#</strong></p> <p>It is a compilation error to divide by a zero constant. Division by a variable set to zero results in a runtime exception.</p> <p><a name="float-div-note" id="float-div-note"></a></p> <h2 id="toc41"><span><a href="cpp#float-div">float division</a></span></h2> <p>How to perform floating point division on two integers.</p> <p><a name="float-div-zero-note" id="float-div-zero-note"></a></p> <h2 id="toc42"><span><a href="cpp#float-div-zero">float division by zero</a></span></h2> <p>The result of floating point division by zero.</p> <p>Modern hardware, if it implements floating point instructions, will implement instructions which conform to the IEEE 754 standard. The standard requires values for positive infinity, negative infinity, and not-a-number (NaN).</p> <p>The C and C++ standards do not assume that they are running on hardware which provides these values; code which assumes they exist is not strictly speaking portable.</p> <p><a name="power-note" id="power-note"></a></p> <h2 id="toc43"><span><a href="cpp#power">power</a></span></h2> <p>How to perform exponentiation.</p> <p><strong>C++</strong></p> <p><em>powm1</em> is an abbreviation for "power minus one". Hence the need to add one to get the answer.</p> <p><a name="sqrt-note" id="sqrt-note"></a></p> <h2 id="toc44"><span><a href="cpp#sqrt">sqrt</a></span></h2> <p>The positive square root function.</p> <p><a name="sqrt-negative-one-note" id="sqrt-negative-one-note"></a></p> <h2 id="toc45"><span><a href="cpp#sqrt-negative-one">sqrt -1</a></span></h2> <p>The result of taking the square root of a negative number.</p> <p>Here is a list of the standard mathematical functions whose domains do not cover the entire real number line:</p> <table class="wiki-content-table"> <tr> <th>function</th> <th>returns inf on</th> <th>returns nan on</th> <th>returns -inf on</th> </tr> <tr> <td>sqrt</td> <td>inf</td> <td>[-inf, 0)</td> <td></td> </tr> <tr> <td>log</td> <td>inf</td> <td>[-inf, 0)</td> <td>0</td> </tr> <tr> <td>asin</td> <td></td> <td>[-inf, -1) U (1, inf]</td> <td></td> </tr> <tr> <td>acos</td> <td></td> <td>[-inf, -1) U (1, inf]</td> <td></td> </tr> </table> <p><a name="transcendental-func-note" id="transcendental-func-note"></a></p> <h2 id="toc46"><span><a href="cpp#transcendental-func">transcendental functions</a></span></h2> <p>The exponential and logarithm functions; the trigonometric functions; the inverse trigonometric functions.</p> <p>The arguments of the trigonometric functions are in radians as are the return values of the inverse trigonometric functions.</p> <p><a name="transcendental-const-note" id="transcendental-const-note"></a></p> <h2 id="toc47"><span><a href="cpp#transcendental-const">transcendental constants</a></span></h2> <p>The transcendental constants <em>e</em> and <em>pi</em>.</p> <p><a name="float-truncation-note" id="float-truncation-note"></a></p> <h2 id="toc48"><span><a href="cpp#float-truncation">float truncation</a></span></h2> <p>Functions for converting a float to a nearby integer value.</p> <p><strong>C:</strong></p> <p>The <tt>math.h</tt> library also provides <tt>floor</tt> and <tt>ceil</tt> which return <tt>double</tt> values.</p> <p><strong>Java:</strong></p> <p><tt>Math.floor</tt> and <tt>Math.ceil</tt> return <tt>double</tt> values.</p> <p><a name="absolute-val-note" id="absolute-val-note"></a></p> <h2 id="toc49"><span><a href="cpp#absolute-val">absolute value</a></span></h2> <p>The absolute value of a numeric quantity.</p> <p><a name="int-overflow-note" id="int-overflow-note"></a></p> <h2 id="toc50"><span><a href="cpp#int-overflow">integer overflow</a></span></h2> <p>What happens when an integer expression results in a value larger than what can be stored in the integer type.</p> <p><a name="float-overflow-note" id="float-overflow-note"></a></p> <h2 id="toc51"><span><a href="cpp#float-overflow">float overflow</a></span></h2> <p>What happens when a float expression results in a value larger than largest representable finite float value.</p> <p><a name="float-limits-note" id="float-limits-note"></a></p> <h2 id="toc52"><span><a href="cpp#float-limits">float limits</a></span></h2> <p>The largest finite floating point number and the smallest positive floating point number.</p> <p><a name="complex-construction-note" id="complex-construction-note"></a></p> <h2 id="toc53"><span><a href="cpp#complex-construction">complex construction</a></span></h2> <p>How to construct a complex number.</p> <p><a name="complex-decomposition-note" id="complex-decomposition-note"></a></p> <h2 id="toc54"><span><a href="cpp#complex-decomposition">complex decomposition</a></span></h2> <p>How to get the components of a complex number. Both Cartesian and polar decompositions are illustrated. Also how to get the complex conjugate.</p> <p><a name="random-num-note" id="random-num-note"></a></p> <h2 id="toc55"><span><a href="cpp#random-num">random number</a></span></h2> <p>Ways to generate random numbers. The distributions are a uniform integer from 0 to 99; a uniform float from 0.0 to 1.0; a standard normal float.</p> <p><strong>c++:</strong></p> <p>The standard library includes functions for generating random numbers from <a href="distributions">other distributions</a>.</p> <p><a name="random-seed-note" id="random-seed-note"></a></p> <h2 id="toc56"><span><a href="cpp#random-seed">random seed</a></span></h2> <p>How to set the seed for the random number generator.</p> <p><a name="bit-op-note" id="bit-op-note"></a></p> <h2 id="toc57"><span><a href="cpp#bit-op">bit operators</a></span></h2> <p>The bit operators: left shift, right shift, and, or, xor, and complement.</p> <p><strong>C++</strong></p> <p><em>bitand</em>, <em>bitor</em>, and <em>compl</em> are synonyms of &, |, and ~ with identical precedence and semantics.</p> <p><a name="binary-octal-hex-literals-note" id="binary-octal-hex-literals-note"></a></p> <h2 id="toc58"><span><a href="cpp#binary-octal-hex-literals">binary, octal, and hex literals</a></span></h2> <p>Binary, octal, and hex integer literals.</p> <p><a name="radix-note" id="radix-note"></a></p> <h2 id="toc59"><span><a href="cpp#radix">radix</a></span></h2> <p>How to convert integers to strings of digits of a given base. How to convert such strings into integers.</p> <p><a name="strings-note" id="strings-note"></a></p> <h1 id="toc60"><span><a href="cpp#strings">Strings</a></span></h1> <p><a name="str-type-note" id="str-type-note"></a></p> <h2 id="toc61"><span><a href="cpp#str-type">string type</a></span></h2> <p>The type for strings.</p> <p><a name="str-literal-note" id="str-literal-note"></a></p> <h2 id="toc62"><span><a href="cpp#str-literal">string literal</a></span></h2> <p>The syntax for string literals.</p> <p><a name="newline-literal-note" id="newline-literal-note"></a></p> <h2 id="toc63"><span><a href="cpp#newline-literal">newline in literal</a></span></h2> <p>Can a newline be used in a string literal? Does the newline appear in the resulting string object?</p> <p><a name="str-literal-escape-note" id="str-literal-escape-note"></a></p> <h2 id="toc64"><span><a href="cpp#str-literal-escape">literal escapes</a></span></h2> <p>Escape sequences that can be used in string literals.</p> <p><a name="allocate-str-note" id="allocate-str-note"></a></p> <h2 id="toc65"><span><a href="cpp#allocate-str">allocate string</a></span></h2> <p>How to allocate a string.</p> <p><strong>Java</strong></p> <p>The following code</p> <div class="code"> <pre> <code>String t = new String(s);</code> </pre></div> <p>creates a copy of the string <em>s</em>. However, because Java strings are immutable, it would be safe to store the same string object it <em>t</em> as follows:</p> <div class="code"> <pre> <code>String t = s;</code> </pre></div> <p><a name="mutable-str-note" id="mutable-str-note"></a></p> <h2 id="toc66"><span><a href="cpp#mutable-str">are strings mutable?</a></span></h2> <p><a name="copy-str-note" id="copy-str-note"></a></p> <h2 id="toc67"><span><a href="cpp#copy-str">copy string</a></span></h2> <p><a name="fmt-str-note" id="fmt-str-note"></a></p> <h2 id="toc68"><span><a href="cpp#fmt-str">format string</a></span></h2> <p><a name="compare-str-note" id="compare-str-note"></a></p> <h2 id="toc69"><span><a href="cpp#compare-str">compare strings</a></span></h2> <p><strong>C++</strong></p> <p><em>string::compare</em> returns a positive value, 0, or a negative value depending upon whether the receiver is lexicographically greater, equal, or less than the argument. C++ overload the comparison operators (<, >, ==, !=, <=, >=) so that they can be used for string comparison.</p> <p><strong>Objective C</strong><br /> <em>compare</em> will return -1, 0, or 1.</p> <p><strong>Java</strong></p> <p><em>compareTo</em> will return a negative value, 0, or a positive value.</p> <p><strong>C#</strong></p> <p><em>CompareTo</em> will return -1, 0, or 1.</p> <p><a name="str-concat-note" id="str-concat-note"></a></p> <h2 id="toc70"><span><a href="cpp#str-concat">concatenate</a></span></h2> <p><a name="str-replicate-note" id="str-replicate-note"></a></p> <h2 id="toc71"><span><a href="cpp#str-replicate">replicate</a></span></h2> <p><a name="translate-case-note" id="translate-case-note"></a></p> <h2 id="toc72"><span><a href="cpp#translate-case">translate case</a></span></h2> <p><a name="trim-note" id="trim-note"></a></p> <h2 id="toc73"><span><a href="cpp#trim">trim</a></span></h2> <p><a name="pad-note" id="pad-note"></a></p> <h2 id="toc74"><span><a href="cpp#pad">pad</a></span></h2> <p><a name="num-to-str-note" id="num-to-str-note"></a></p> <h2 id="toc75"><span><a href="cpp#num-to-str">number to string</a></span></h2> <p><a name="str-to-num-note" id="str-to-num-note"></a></p> <h2 id="toc76"><span><a href="cpp#str-to-num">string to number</a></span></h2> <p><strong>C++</strong></p> <p><em>strtoimax</em>, <em>strtol</em>, <em>strtoll</em>, <em>strtoumax</em>, <em>strtoul</em>, and <em>strtoull</em> take three arguments:</p> <div class="code"> <pre> <code>intmax_t strtoimax(const char *str, char **endp, int base);</code> </pre></div> <p>The 2nd argument, if not NULL, will be set to first character in the string that is not part of the number. The 3rd argument can specify a base between 2 and 36.</p> <p><em>strtof</em>, <em>strtod</em>, and <em>strtold</em> take three arguments:</p> <div class="code"> <pre> <code>double strtod(const char *str, char **endp);</code> </pre></div> <p><strong>Java</strong></p> <p><em>parseInt</em> has an optional second argument for the base.</p> <p><a name="join-note" id="join-note"></a></p> <h2 id="toc77"><span><a href="cpp#join">join</a></span></h2> <p><strong>java:</strong></p> <p>Use <tt>StringBuilder</tt> to implement join:</p> <div class="code"> <pre> <code>public static String join(String[] a, String sep) { StringBuilder sb = new StringBuilder(); for (int i=0; i<a.length; i++) { if (i > 0) { sb.append(sep); } sb.append(a[i]); } return sb.toString(); }</code> </pre></div> <p><a name="split-note" id="split-note"></a></p> <h2 id="toc78"><span><a href="cpp#split">split</a></span></h2> <p><a name="serialize-note" id="serialize-note"></a></p> <h2 id="toc79"><span><a href="cpp#serialize">serialize</a></span></h2> <p><a name="str-length-note" id="str-length-note"></a></p> <h2 id="toc80"><span><a href="cpp#str-length">string length</a></span></h2> <p><a name="index-substr-note" id="index-substr-note"></a></p> <h2 id="toc81"><span><a href="cpp#index-substr">index of substring</a></span></h2> <p><a name="extract-substr-note" id="extract-substr-note"></a></p> <h2 id="toc82"><span><a href="cpp#extract-substr">extract substring</a></span></h2> <p><a name="char-type-note" id="char-type-note"></a></p> <h2 id="toc83"><span><a href="cpp#char-type">character type</a></span></h2> <p><a name="char-literal-note" id="char-literal-note"></a></p> <h2 id="toc84"><span><a href="cpp#char-literal">character literal</a></span></h2> <p><a name="test-char-note" id="test-char-note"></a></p> <h2 id="toc85"><span><a href="cpp#test-char">test character</a></span></h2> <p><a name="regexes-note" id="regexes-note"></a></p> <h1 id="toc86"><span><a href="cpp#regexes">Regular Expressions</a></span></h1> <p><a name="regex-match" id="regex-match"></a></p> <h2 id="toc87"><span>regex match</span></h2> <p><a name="regex-substitute" id="regex-substitute"></a></p> <h2 id="toc88"><span>regex substitute</span></h2> <p><a name="dates-time-note" id="dates-time-note"></a></p> <h1 id="toc89"><span><a href="cpp#dates-time">Date and Time</a></span></h1> <p><a name="date-time-type-note" id="date-time-type-note"></a></p> <h2 id="toc90"><span><a href="cpp#date-time-type">date and time type</a></span></h2> <p>The data type used to store a combined date and time.</p> <p><a name="current-date-time-note" id="current-date-time-note"></a></p> <h2 id="toc91"><span><a href="cpp#current-date-time">current date and time</a></span></h2> <p>How to get the current date and time.</p> <p><a name="unix-epoch-note" id="unix-epoch-note"></a></p> <h2 id="toc92"><span><a href="cpp#unix-epoch">to unix epoch, from unix epoch</a></span></h2> <p>How to convert a date/time object to the Unix epoch. How to convert the Unix epoch to a date/time object.</p> <p>The Unix epoch is the number of seconds since 1 January 1970 UTC.</p> <p><strong>c#:</strong></p> <p>Windows file time is the number of nanoseconds since 1 January 1601 UTC divided by 100. The concept was introduced when journaling was added to NTFS with Windows 2000.</p> <p>The magic constant (11644473600) used for the conversion can be calculated with the following code:</p> <div class="code"> <pre> <code>using System; using System.Globalization; CultureInfo enUS = new CultureInfo("en-US"); DateTime startEpoch = DateTime.ParseExact( "1970-01-01 00:00:00 -00", "yyyy-MM-dd HH:mm:ss zz", enUS); Console.WriteLine(startEpoch.ToFileTimeUtc() / (10 * 1000 * 1000));</code> </pre></div> <p><a name="format-date-note" id="format-date-note"></a></p> <h2 id="toc93"><span><a href="cpp#format-date">format date</a></span></h2> <p>How to use a format string to display a date/time object.</p> <p>The canonical example of doing this is the <tt>strftime</tt> function from the C standard library which defines used letters prefix by percent signs as conversion specification characters, e.g. %Y-%m-%d.</p> <p><a name="parse-date-note" id="parse-date-note"></a></p> <h2 id="toc94"><span><a href="cpp#parse-date">parse date</a></span></h2> <p>How to use a format string to parse date data from a string.</p> <p><a name="date-subtraction-note" id="date-subtraction-note"></a></p> <h2 id="toc95"><span><a href="cpp#date-subtraction">date subtraction</a></span></h2> <p><a name="add-duration-note" id="add-duration-note"></a></p> <h2 id="toc96"><span><a href="cpp#add-duration">add duration</a></span></h2> <p><a name="date-parts-note" id="date-parts-note"></a></p> <h2 id="toc97"><span><a href="cpp#date-parts">date parts</a></span></h2> <p><a name="time-parts-note" id="time-parts-note"></a></p> <h2 id="toc98"><span><a href="cpp#time-parts">time parts</a></span></h2> <p><a name="fixed-length-arrays-note" id="fixed-length-arrays-note"></a></p> <h1 id="toc99"><span><a href="cpp#fixed-length-arrays">Fixed-Length Arrays</a></span></h1> <p><a name="fixed-len-array-stack-note" id="fixed-len-array-stack-note"></a></p> <h2 id="toc100"><span><a href="cpp#fixed-len-array-stack">declare on stack</a></span></h2> <p>How to allocate an array which is freed when the block in which it is defined goes out of scope.</p> <p><a name="fixed-len-array-heap-note" id="fixed-len-array-heap-note"></a></p> <h2 id="toc101"><span><a href="cpp#fixed-len-array-heap">declare on heap</a></span></h2> <p>How to allocate an array on the heap.</p> <p><a name="free-fixed-len-array-heap-note" id="free-fixed-len-array-heap-note"></a></p> <h2 id="toc102"><span><a href="cpp#free-fixed-len-array-heap">free heap</a></span></h2> <p>How to free an array that was allocated on the heap.</p> <p><a name="fixed-len-array-init-list-note" id="fixed-len-array-init-list-note"></a></p> <h2 id="toc103"><span><a href="cpp#fixed-len-array-init-list">initialization list</a></span></h2> <p><strong>Objective C</strong></p> <p>NSArray can only store instances of NSObject. For primitive types, use C arrays.</p> <p><strong>Java</strong></p> <p>Java permits arrays to be declared with C-style syntax:</p> <div class="code"> <pre> <code>int a[] = {1,2,3}</code> </pre></div> <p><a name="fixed-len-array-size-note" id="fixed-len-array-size-note"></a></p> <h2 id="toc104"><span><a href="cpp#fixed-len-array-size">size</a></span></h2> <p>How to get the size of a fixed-length array.</p> <p><a name="fixed-len-array-lookup-note" id="fixed-len-array-lookup-note"></a></p> <h2 id="toc105"><span><a href="cpp#fixed-len-array-lookup">lookup</a></span></h2> <p>How to get the value stored at an index in a fixed-length array.</p> <p><strong>C++</strong></p> <p>Arrays can be manipulated with pointer syntax. The following sets <em>x</em> and <em>y</em> to the same value:</p> <div class="code"> <pre> <code>int a[] = {3,7,4,8,5,9,6,10}; int x = a[4]; int y = *(a+4);</code> </pre></div> <p><a name="fixed-len-array-update-note" id="fixed-len-array-update-note"></a></p> <h2 id="toc106"><span><a href="cpp#fixed-len-array-update">update</a></span></h2> <p>How to update the value stored at an index in a fixed-length array.</p> <p><a name="fixed-len-array-out-of-bounds-note" id="fixed-len-array-out-of-bounds-note"></a></p> <h2 id="toc107"><span><a href="cpp#fixed-len-array-out-of-bounds">out-of-bounds</a></span></h2> <p>What happens when an out-of-bounds index is used to access a value in a fixed-length array.</p> <p><a name="copy-fixed-len-array-note" id="copy-fixed-len-array-note"></a></p> <h2 id="toc108"><span><a href="cpp#copy-fixed-len-array">copy</a></span></h2> <p>How to copy a fixed-length array.</p> <p><a name="fixed-len-array-as-func-arg-note" id="fixed-len-array-as-func-arg-note"></a></p> <h2 id="toc109"><span><a href="cpp#fixed-len-array-as-func-arg">as function argument</a></span></h2> <p>How to pass a fixed-length array as a function argument.</p> <p><a name="iterate-over-fixed-len-array-note" id="iterate-over-fixed-len-array-note"></a></p> <h2 id="toc110"><span><a href="cpp#iterate-over-fixed-len-array">iterate</a></span></h2> <p>How to iterate over the values of the fixed-length array.</p> <p><strong>C++</strong></p> <p>Range-based for loops can be used with fixed-length arrays (but not pointers):</p> <div class="code"> <pre> <code>int a[4] = {3, 2, 4, 1}; int sum(0); for (const auto& n: a) { sum += n; }</code> </pre></div> <p><a name="sort-fixed-len-array-note" id="sort-fixed-len-array-note"></a></p> <h2 id="toc111"><span><a href="cpp#sort-fixed-len-array">sort</a></span></h2> <p><a name="resizable-arrays-note" id="resizable-arrays-note"></a></p> <h1 id="toc112"><span><a href="cpp#resizable-arrays">Resizable Arrays</a></span></h1> <p><a name="decl-resizable-array-note" id="decl-resizable-array-note"></a></p> <h2 id="toc113"><span><a href="cpp#decl-resizable-array">declare</a></span></h2> <p><a name="resizable-array-init-list-note" id="resizable-array-init-list-note"></a></p> <h2 id="toc114"><span><a href="cpp#resizable-array-init-list">initialization list</a></span></h2> <p><a name="resizable-array-size-note" id="resizable-array-size-note"></a></p> <h2 id="toc115"><span><a href="cpp#resizable-array-size">size</a></span></h2> <p><a name="resizable-array-capacity-note" id="resizable-array-capacity-note"></a></p> <h2 id="toc116"><span><a href="cpp#resizable-array-capacity">capacity</a></span></h2> <p><a name="resizable-array-empty-test-note" id="resizable-array-empty-test-note"></a></p> <h2 id="toc117"><span><a href="cpp#resizable-array-empty-test">empty test</a></span></h2> <p><a name="resizable-array-lookup-note" id="resizable-array-lookup-note"></a></p> <h2 id="toc118"><span><a href="cpp#resizable-array-lookup">lookup</a></span></h2> <p><a name="resizable-array-update-note" id="resizable-array-update-note"></a></p> <h2 id="toc119"><span><a href="cpp#resizable-array-update">update</a></span></h2> <p><a name="resizable-array-out-of-bounds-note" id="resizable-array-out-of-bounds-note"></a></p> <h2 id="toc120"><span><a href="cpp#resizable-array-out-of-bounds">out-of-bounds behavior</a></span></h2> <p><a name="resizable-arary-elem-index-note" id="resizable-arary-elem-index-note"></a></p> <h2 id="toc121"><span><a href="cpp#resizable-array-elem-index">element index</a></span></h2> <p><a name="slice-resizable-array-note" id="slice-resizable-array-note"></a></p> <h2 id="toc122"><span><a href="cpp#slice-resizable-array">slice</a></span></h2> <p><a name="slice-resizable-array-to-end-note" id="slice-resizable-array-to-end-note"></a></p> <h2 id="toc123"><span><a href="cpp#slice-resizable-array-to-end">slice to end</a></span></h2> <p><a name="resizable-array-back-note" id="resizable-array-back-note"></a></p> <h2 id="toc124"><span><a href="cpp#resizable-array-back">manipulate back</a></span></h2> <p><a name="resizable-array-front-note" id="resizable-array-front-note"></a></p> <h2 id="toc125"><span><a href="cpp#resizable-array-front">manipulate front</a></span></h2> <p><a name="concat-resizable-array-note" id="concat-resizable-array-note"></a></p> <h2 id="toc126"><span><a href="cpp#concat-resizable-array">concatenate</a></span></h2> <p><a name="replicate-resizable-array-elem-note" id="replicate-resizable-array-elem-note"></a></p> <h2 id="toc127"><span><a href="cpp#replicate-resizable-array-elem">replicate element</a></span></h2> <p><a name="copy-resizable-array-note" id="copy-resizable-array-note"></a></p> <h2 id="toc128"><span><a href="cpp#copy-resizable-array">copy</a></span></h2> <p><a name="resizable-array-as-func-arg-note" id="resizable-array-as-func-arg-note"></a></p> <h2 id="toc129"><span><a href="cpp#resizable-array-as-func-arg">array as function argument</a></span></h2> <p><a name="iterate-over-resizable-array-note" id="iterate-over-resizable-array-note"></a></p> <h2 id="toc130"><span><a href="cpp#iterate-over-resizable-array">iterate</a></span></h2> <p>How to iterate over a resizable array.</p> <p><strong>C++</strong></p> <p>The range-based for loop was introduced in C++11. It it can be used to iterate over an initialization list:</p> <div class="code"> <pre> <code>for (const auto& n: {1, 2, 3}) { sum += n; }</code> </pre></div> <p><a name="tuples-note" id="tuples-note"></a></p> <h1 id="toc131"><span><a href="cpp#tuples">Tuples</a></span></h1> <p><a name="pair" id="pair"></a></p> <h2 id="toc132"><span>pair</span></h2> <p><a name="dictionaries-note" id="dictionaries-note"></a></p> <h1 id="toc133"><span><a href="cpp#dictionaries">Dictionaries</a></span></h1> <p><a name="map" id="map"></a></p> <h2 id="toc134"><span>map declaration</span></h2> <p><strong>C:</strong></p> <p>For those interested in an industrial strength hashtable implementation for C, here is the <a href="http://svn.ruby-lang.org/cgi-bin/viewvc.cgi/trunk/include/ruby/st.h?revision=26401&view=markup">header file</a> and the <a href="http://svn.ruby-lang.org/cgi-bin/viewvc.cgi/trunk/st.c?revision=26672&view=markup">source file</a> for the hashtable used by Ruby.<br /> For those interested in a "Computer Science 101" implementation of a hashtable, here is a simpler <a href="http://gist.github.com/400762">source file</a> and <a href="http://gist.github.com/400764">header file</a>.</p> <p><a name="map-access" id="map-access"></a></p> <h2 id="toc135"><span>map access</span></h2> <p><a name="map-size" id="map-size"></a></p> <h2 id="toc136"><span>map size</span></h2> <p><a name="map-remove" id="map-remove"></a></p> <h2 id="toc137"><span>map remove</span></h2> <p><a name="map-element-not-found" id="map-element-not-found"></a></p> <h2 id="toc138"><span>map element not found result</span></h2> <p><a name="map-iterator" id="map-iterator"></a></p> <h2 id="toc139"><span>map iterator</span></h2> <p><a name="functions-note" id="functions-note"></a></p> <h1 id="toc140"><span><a href="cpp#functions">Functions</a></span></h1> <p><a name="decl-func-note" id="decl-func-note"></a></p> <h2 id="toc141"><span><a href="cpp#decl-func">declare function</a></span></h2> <p>How to declare the type of a function.</p> <p><a name="def-func-note" id="def-func-note"></a></p> <h2 id="toc142"><span><a href="cpp#def-func">define function</a></span></h2> <p>How to define a function.</p> <p><a name="invoke-func-note" id="invoke-func-note"></a></p> <h2 id="toc143"><span><a href="cpp#invoke-func">invoke function</a></span></h2> <p>How to invoke a function.</p> <p><a name="def-static-method-note" id="def-static-method-note"></a></p> <h2 id="toc144"><span><a href="cpp#def-static-class-method">define static class method</a></span></h2> <p><a name="invoke-static-method-note" id="invoke-static-method-note"></a></p> <h2 id="toc145"><span><a href="cpp#invoke-static-class-method">invoke static class method</a></span></h2> <p><a name="overload-func-note" id="overload-func-note"></a></p> <h2 id="toc146"><span><a href="cpp#overload-func">overload function</a></span></h2> <p><a name="default-arg-note" id="default-arg-note"></a></p> <h2 id="toc147"><span><a href="cpp#default-arg">default argument</a></span></h2> <p><a name="variable-num-arg-note" id="variable-num-arg-note"></a></p> <h2 id="toc148"><span><a href="cpp#variable-num-arg">variable number of arguments</a></span></h2> <p><a name="named-param-note" id="named-param-note"></a></p> <h2 id="toc149"><span><a href="cpp#named-param">named parameters</a></span></h2> <p><strong>Objective C:</strong></p> <p>Named parameters must be invoked in the order in which they are defined in the method signature.</p> <p><strong>C#:</strong></p> <p>Named parameter do not need to be invoked in the order in which they are defined in the method signature. Additionally, their use in<br /> invocation is optional: the arguments can be provided without names in which case the definition order must be used.</p> <p><a name="pass-by-val-note" id="pass-by-val-note"></a></p> <h2 id="toc150"><span><a href="cpp#pass-by-val">pass by value</a></span></h2> <p><a name="pass-by-ref-note" id="pass-by-ref-note"></a></p> <h2 id="toc151"><span><a href="cpp#pass-by-ref">pass by reference</a></span></h2> <p><a name="pass-by-addr-note" id="pass-by-addr-note"></a></p> <h2 id="toc152"><span><a href="cpp#pass-by-addr">pass by address</a></span></h2> <p><a name="retval-note" id="retval-note"></a></p> <h2 id="toc153"><span><a href="cpp#retval">return value</a></span></h2> <p><a name="no-retval-note" id="no-retval-note"></a></p> <h2 id="toc154"><span><a href="cpp#no-retval">no return value</a></span></h2> <p><a name="recursive-func-note" id="recursive-func-note"></a></p> <h2 id="toc155"><span><a href="cpp#recursive-func">recursive function</a></span></h2> <p><a name="anonymous-func-note" id="anonymous-func-note"></a></p> <h2 id="toc156"><span><a href="cpp#anonymous-func">anonymous function</a></span></h2> <p><a name="invoke-anonymous-func-note" id="invoke-anonymous-func-note"></a></p> <h2 id="toc157"><span><a href="cpp#invoke-anonymous-func">invoke anonymous function</a></span></h2> <p><a name="closure-note" id="closure-note"></a></p> <h2 id="toc158"><span><a href="cpp#closure">closure</a></span></h2> <p><a name="func-private-state-note" id="func-private-state-note"></a></p> <h2 id="toc159"><span><a href="cpp#func-private-state">function with private state</a></span></h2> <p><a name="func-as-val-note" id="func-as-val-note"></a></p> <h2 id="toc160"><span><a href="cpp#func-as-val">function as value</a></span></h2> <p><a name="overload-op-note" id="overload-op-note"></a></p> <h2 id="toc161"><span><a href="cpp#overload-op">overload operator</a></span></h2> <p><strong>C++</strong></p> <p>A note on how to overload postfix and prefix <tt>++</tt> and <tt>—</tt>.</p> <p><a name="execution-control-note" id="execution-control-note"></a></p> <h1 id="toc162"><span><a href="cpp#execution-control">Execution Control</a></span></h1> <p><a name="if-note" id="if-note"></a></p> <h2 id="toc163"><span><a href="cpp#if">if</a></span></h2> <p>The syntax for an <tt>if</tt> statement.</p> <p><a name="dangling-else-note" id="dangling-else-note"></a></p> <h2 id="toc164"><span><a href="cpp#dangling-else">dangling else</a></span></h2> <p>The curly braces around the branches of an <tt>if</tt> statement are optional when the branch contains a single statement.</p> <p>From the perspective of the parser, the branches are statements. Curly branch delimited blocks are legal wherever a statement is legal.</p> <p>When <tt>if</tt> statements are nested and the outer <tt>if</tt> statement does not put its if-clause in curly braces, the parser will match a subsequent <tt>else</tt> to the inner <tt>if</tt>. Code which puts the <tt>else</tt> on the same level of indentation as the outer <tt>if</tt> is deceptive.</p> <p><a name="switch-note" id="switch-note"></a></p> <h2 id="toc165"><span><a href="cpp#switch">switch</a></span></h2> <p>The syntax for a <tt>switch</tt> statement.</p> <p>A <tt>switch</tt> checks the value of integer expression and jumps to the correct label. This can faster than an <tt>if</tt> statement with numerous <tt>else if</tt> branches which tests the expression until a match is found.</p> <p>Execution falls through to code after subsequent labels unless a <tt>break</tt> statement is encountered. This makes it possible to write code once which handles multiple values.</p> <p><a name="while-note" id="while-note"></a></p> <h2 id="toc166"><span><a href="cpp#while">while</a></span></h2> <p>The syntax for a <tt>while</tt> loop.</p> <p>If the body of a <tt>while</tt> loop is a single statement, the curly braces are optional.</p> <p>The languages in this sheet also have a <tt>do-while</tt> loop, which is a loop which always executes at least once.</p> <p>Here is an example of a <tt>do-while</tt> loop which converts an unsigned integer to a string. The corresponding <tt>while</tt> loop would require extra handling when the integer is zero.</p> <div class="code"> <pre> <code>/* unsigned int n; * char* s; */ do { s[i++] = n % 10 + '0'; } while ((n /= 10) > 0); s[i] = '\0'; reverse(s);</code> </pre></div> <p><a name="for-note" id="for-note"></a></p> <h2 id="toc167"><span><a href="cpp#for">for</a></span></h2> <p>The syntax for a C-style <tt>for</tt> loop.</p> <p>The parens of the <tt>for</tt> loop contain three expressions set off by semicolons: the <em>initialization</em>, the <em>condition</em>, and the <em>increment</em>.</p> <p>The initialization expression executes once before the loop starts.</p> <p>The condition expression executes once before the start of each iteration. The loop stops if the condition is false.</p> <p>The increment executes at the end of each iteration.</p> <p><a name="break-note" id="break-note"></a></p> <h2 id="toc168"><span><a href="cpp#break">break</a></span></h2> <p>The <tt>break</tt> statement terminates execution of the innermost containing loop or switch statement.</p> <p><a name="break-nested-loops-note" id="break-nested-loops-note"></a></p> <h2 id="toc169"><span><a href="cpp#break-nested-loops">break out of nested loops</a></span></h2> <p>A method for breaking out of nested loops.</p> <p><a name="continue-note" id="continue-note"></a></p> <h2 id="toc170"><span><a href="cpp#continue">continue</a></span></h2> <p>The <tt>continue</tt> statement, which terminates execution of the current loop iteration.</p> <p><a name="goto-note" id="goto-note"></a></p> <h2 id="toc171"><span><a href="cpp#goto">goto</a></span></h2> <p><a name="exceptions-note" id="exceptions-note"></a></p> <h1 id="toc172"><span><a href="cpp#exceptions">Exceptions</a></span></h1> <p><a name="base-exc-note" id="base-exc-note"></a></p> <h2 id="toc173"><span><a href="cpp#base-exc">base exception</a></span></h2> <p>The base class or interface for exceptions.</p> <p><a name="predefined-exc-note" id="predefined-exc-note"></a></p> <h2 id="toc174"><span><a href="cpp#predefined-exc">predefined exceptions</a></span></h2> <p>A partial list of exceptions raised by the language or the standard library.</p> <p><a name="raise-exc-note" id="raise-exc-note"></a></p> <h2 id="toc175"><span><a href="cpp#raise-exc">raise exception</a></span></h2> <p>How to raise an exception.</p> <p><strong>C++</strong></p> <p>C++ code can throw or catch any type of object or primitive data type. The C++ standard library throws subclasses of std::exception, which does not have a message member.</p> <p><strong>Objective C</strong></p> <p>Objective C can only throw an instance of NSException or one of its subclasses.</p> <p><strong>Java</strong></p> <p>Java can only throw an implementation of java.lang.Throwable.</p> <p><strong>C#</strong></p> <p>C# can only throw an instance of System.Exception of one of its subclasses.</p> <p><a name="handle-exc-note" id="handle-exc-note"></a></p> <h2 id="toc176"><span><a href="cpp#handle-exc">handle exception</a></span></h2> <p>How to handle an exception.</p> <p><strong>C++</strong></p> <p>Exceptions can be caught by value or by reference. If the exception is an object and it is caught by value, the copy constructor and the destructor will be invoked.</p> <p><strong>Objective C</strong></p> <p>Exceptions are thrown and caught by pointer value.</p> <p><a name="def-exc-note" id="def-exc-note"></a></p> <h2 id="toc177"><span><a href="cpp#def-exc">define exception</a></span></h2> <p>How to define a new exception type.</p> <p><a name="re-raise-exc-note" id="re-raise-exc-note"></a></p> <h2 id="toc178"><span><a href="cpp#re-raise-exc">re-raise exception</a></span></h2> <p>How to handle and re-raise an exception.</p> <p><a name="catch-all-handler-note" id="catch-all-handler-note"></a></p> <h2 id="toc179"><span><a href="cpp#catch-all-handler">catch-all handler</a></span></h2> <p>How to write a handler witch catches any exception.</p> <p><a name="multiple-handlers-note" id="multiple-handlers-note"></a></p> <h2 id="toc180"><span><a href="cpp#multiple-handlers">multiple handlers</a></span></h2> <p><a name="error-msg-note" id="error-msg-note"></a></p> <h2 id="toc181"><span><a href="cpp#error-msg">error message</a></span></h2> <p><a name="errno-note" id="errno-note"></a></p> <h2 id="toc182"><span><a href="cpp#errno">system call errno</a></span></h2> <p><a name="finally-clause-note" id="finally-clause-note"></a></p> <h2 id="toc183"><span><a href="cpp#finally-clause">finally clause</a></span></h2> <p><strong>C++</strong></p> <div class="code"> <pre> <code>Class Finally { void (*finally)(); Finally(void (*f)()) : finally(f) { } ~Finally() { do_cleanup(); } }; { Cleanup c(); risky(); }</code> </pre></div> <p><a name="exc-specification-note" id="exc-specification-note"></a></p> <h2 id="toc184"><span><a href="cpp#exc-specification">exception specification</a></span></h2> <p><strong>Java</strong></p> <p>If a method throws a subclass of java.lang.Exception, it must declare the exception in its throws clause. This includes exceptions originating in code called by the method. On the other hand, if the method throws a subclass of java.lang.Error, no declaration in the throws clause is necessary.</p> <p><a name="concurrency-note" id="concurrency-note"></a></p> <h1 id="toc185"><span><a href="cpp#concurrency">Concurrency</a></span></h1> <p><a name="file-handles-note" id="file-handles-note"></a></p> <h1 id="toc186"><span><a href="cpp#file-handles">File Handles</a></span></h1> <p><a name="printf-note" id="printf-note"></a></p> <h2 id="toc187"><span><a href="cpp#printf">printf</a></span></h2> <p>How to print a formatted string to standard out.</p> <p><a name="read-file" id="read-file"></a></p> <h2 id="toc188"><span>read from file</span></h2> <p><strong>C</strong></p> <p>If there is an error, the global variable <em>errno</em> will be set to a nonzero value, and <em>strerror(errno)</em> will return an error message for the error.</p> <p><a name="write-file" id="write-file"></a></p> <h2 id="toc189"><span>write to file</span></h2> <p><a name="files-note" id="files-note"></a></p> <h1 id="toc190"><span><a href="cpp#files">Files</a></span></h1> <p><a name="file-fmt-note" id="file-fmt-note"></a></p> <h1 id="toc191"><span><a href="cpp#file-fmt">File Formats</a></span></h1> <p><a name="directories-note" id="directories-note"></a></p> <h1 id="toc192"><span><a href="cpp#directories">Directories</a></span></h1> <p><a name="processes-environment-note" id="processes-environment-note"></a></p> <h1 id="toc193"><span><a href="cpp#processes-environment">Processes and Environment</a></span></h1> <p><a name="main" id="main"></a></p> <h2 id="toc194"><span>signature of main</span></h2> <p><a name="first-argument" id="first-argument"></a></p> <h2 id="toc195"><span>first argument</span></h2> <p><strong>C</strong></p> <p>The first argument is the pathname to the executable. Whether the pathname is absolute or relative depends on how the executable was invoked. If the executable was invoked via a symlink, then the first argument is the pathname of the symlink, not the executable the symlink points to.</p> <p><a name="environment-variable" id="environment-variable"></a></p> <h2 id="toc196"><span>environment variable</span></h2> <p><a name="iterate-through-environment-variables" id="iterate-through-environment-variables"></a></p> <h2 id="toc197"><span>iterate through environment variables</span></h2> <p><a name="libraries-namespaces-note" id="libraries-namespaces-note"></a></p> <h1 id="toc198"><span><a href="cpp#libraries-namespaces">Library and Namespaces</a></span></h1> <p><a name="std-lib-name-note" id="std-lib-name-note"></a></p> <h2 id="toc199"><span><a href="cpp#std-lib-name">standard library name</a></span></h2> <p>The name of the standard library.</p> <p><strong>C++</strong></p> <p><a href="http://www.sgi.com/tech/stl/">Standard Template Library (STL)</a></p> <p>The STL might not be installed by default.</p> <p><strong>Objective C</strong></p> <p><a href="http://developer.apple.com/mac/library/documentation/cocoa/reference/foundation/objc_classic/index.html">Foundation Framework</a></p> <p>The Foundation Framework is the core of Cocoa, a set of libraries for Objective C development on Mac OS X and the iPhone. The Foundation Framework descends from NextStep, hence the NS prefix in the class names. NextStep was made available to operating systems other than Next as OpenStep and the GNU implementation is called GNUStep.</p> <p><strong>Java</strong></p> <p><a href="http://java.sun.com/javase/6/docs/api/">Java 1.6 API</a></p> <p><strong>C#</strong></p> <p><a href="http://msdn.microsoft.com/en-us/library/ms229335(v=VS.100).aspx">.NET Framework 4 Class Library</a><br /> <a href="http://www.go-mono.com/docs/">Mono Documentation</a></p> <p>The core of the .NET framework is called the Base Class Library. Mono implements the BCL, but not all of the .NET framework.</p> <p><a name="user-defined-types-note" id="user-defined-types-note"></a></p> <h1 id="toc200"><span><a href="cpp#user-defined-types">User-Defined Types</a></span></h1> <p><a name="typedef-note" id="typedef-note"></a></p> <h2 id="toc201"><span><a href="cpp#typedef">typedef</a></span></h2> <p><strong>C</strong></p> <p>Because C integer types don't have well defined sizes, <em>typedef</em> is sometimes employed to as an aid to writing portable code. One might include the following in a header file:</p> <div class="code"> <pre> <code>typedef int int32_t;</code> </pre></div> <p>The rest of the code would declare integers that need to be 32 bits in size using <em>int32_t</em> and if the code needed to be ported to a platform with a 16 bit <em>int</em>, only a single place in the code requires change. In practice the <em>typedef</em> abstraction is leaky because functions in the standard library such as <em>atoi</em>, <em>strtol</em>, or the format strings used by <em>printf</em> depend on the underlying type used.</p> <p><strong>Java</strong></p> <p>Java has well defined integer sizes so <em>typedef</em> is not needed as a portability aid. In other situations where a C programmer would use a <em>typedef</em> for data abstraction, a Java programmer must either define a class or retain the raw primitive type throughout the code.</p> <p><a name="enum-note" id="enum-note"></a></p> <h2 id="toc202"><span><a href="cpp#enum">enum</a></span></h2> <p><strong>C</strong></p> <p>Enums were added to the C standard when the language was standardized by ANSI in 1989.</p> <p>An enum defines a family of integer constants. If an integer value is not explicitly provided for a constant, it is given a value one greater than the previous constant in the list. If the first constant in the list is not given an explicit value, it is assigned a value of zero. it is possible for constants in a list to share values. For example, in the following enum, <em>a</em> and <em>c</em> are both zero and <em>b</em> and <em>d</em> are both one.</p> <div class="code"> <pre> <code>enum { a=0, b, c=0, d };</code> </pre></div> <p>A <em>typedef</em> can be used to make the <em>enum</em> keyword unnecessary in variable declarations:</p> <div class="code"> <pre> <code>typedef enum { mon, tue, wed, thu, fri, sat, sun } day_of_week; day_of_week d = tue;</code> </pre></div> <p>From the point of view of the C compiler, an enum is an <em>int</em>. The C compiler does not prevent assigning values to an enum type that are not in the enumerated list. Thus, the following code compiles:</p> <div class="code"> <pre> <code>enum day_of_week { mon, tue, wed, thu, fri, sat, sun }; day_of_week d = 10; typedef enum { mon, tue, wed, thu, fri, sat, sun } day_of_week2; day_of_week2 d2 = 10;</code> </pre></div> <p><strong>C++</strong></p> <p>C++ enums are more strongly typed the C enums. The compiler rejects attempts to assign a value to an enum variable that is not in the enumerated list. The following code:</p> <div class="code"> <pre> <code>enum day_of_week { mon, tue, wed, thu, fri, sat, sun }; day_of_week d = 10;</code> </pre></div> <p>produces an error like the following:</p> <div class="code"> <pre> <code>main.cpp: In function ‘int main()’: main.cpp:21: error: invalid conversion from ‘int’ to ‘main()::day_of_week’</code> </pre></div> <p><strong>Java</strong></p> <p>Java added enums in 1.5.</p> <p>Java enums are strongly typed like C++ enums. Unlike C++ enums, it is an error to use an enum value in an integer context. The value has a method <em>ordinal()</em> which returns the integer value, however.</p> <p>When used in a string context, an enum will evaluate as the string corresponding to its identifier: i.e. "TUE" for DayOfWeek.TUE. This string can be accessed explicitly with DayOfWeek.TUE.toString(). Conversely, DayOfWeek.valueOf("TUE") returns DayofWeek.TUE.</p> <p>Java enums are subclasses of java.lang.Enum. In particular, an enum is a class, and if the last value if the enum definition is followed by a semicolon, what follows is a class body which can contain methods and constructors. An enum class is final and cannot be subclassed, but an enum can implement an interface.</p> <p><strong>C#</strong></p> <p>Like Java enums, C# enums will return the string corresponding to their identifier. Unlike Java enums, C# enums will evaluate as integers in a numeric context.</p> <p>When used as an argument in a C# style format string, an enum value returns the string corresponding to its identifier.</p> <p><a name="struct-definition" id="struct-definition"></a></p> <h2 id="toc203"><span>struct definition</span></h2> <p>A struct provides names for elements in a predefined set of data and permits the data to be accessed directly without the intermediation of getters and setters. C++, Java, and C# classes can be used to define structs by making the data members public. However, public data members violates the <a href="http://en.wikipedia.org/wiki/Uniform_access_principle">uniform access principle</a>.</p> <p><strong>C++:</strong></p> <p>From <em>The C++ Programming Language: 3rd Edition</em>:</p> <div class="code"> <pre> <code>by definition, a struct is a class in which members are by default public; that is, struct s { ... is simply shorthand for class s { public: ...</code> </pre></div> <p><a name="struct-declaration" id="struct-declaration"></a></p> <h2 id="toc204"><span>struct declaration</span></h2> <p><a name="struct-initialization" id="struct-initialization"></a></p> <h2 id="toc205"><span>struct initialization</span></h2> <p><strong>C</strong></p> <p>The literal format for a struct can only be used during initialization. If the member names are not provided, the values must occur in the order used in the definition.</p> <p><a name="struct-member-assignment" id="struct-member-assignment"></a></p> <h2 id="toc206"><span>struct member assignment</span></h2> <p><a name="struct-member-access" id="struct-member-access"></a></p> <h2 id="toc207"><span>struct member access</span></h2> <p><strong>C</strong></p> <p>The period operator used for member access has higher precedence than the pointer operator. Thus parens must be used<br /> to get at the member of a struct referenced by a pointer:</p> <div class="code"> <pre> <code>struct medal_count { char* country; int gold; int silver; int bronze; } struct medal_count spain = { "Spain", 3, 7 4 }; struct medal_count *winner = &spain; printf("The winner is %s with %d gold medals", (*winner).country, (*winner).gold);</code> </pre></div> <p><em>ptr->mem</em> is a shortcut for <em>(*ptr).mem</em>:</p> <div class="code"> <pre> <code>printf("The winner (%s) earned %d silver medals", winner->country, winner->silver);</code> </pre></div> <p><a name="generic-types-note" id="generic-types-note"></a></p> <h1 id="toc208"><span><a href="cpp#generic-types">Generic Types</a></span></h1> <p><a name="define-generic" id="define-generic"></a></p> <h2 id="toc209"><span>define generic type</span></h2> <p><a name="instantiate-generic" id="instantiate-generic"></a></p> <h2 id="toc210"><span>instantiate generic type</span></h2> <p><a name="objects-note" id="objects-note"></a></p> <h1 id="toc211"><span><a href="cpp#objects">Objects</a></span></h1> <p><a name="define-class" id="define-class"></a></p> <h2 id="toc212"><span>define class</span></h2> <p><a name="constructor" id="constructor"></a></p> <h2 id="toc213"><span>constructor</span></h2> <p><a name="create-object" id="create-object"></a></p> <h2 id="toc214"><span>create object</span></h2> <p><a name="destructor" id="destructor"></a></p> <h2 id="toc215"><span>destructor</span></h2> <p><strong>C++</strong></p> <p>The C++ compiler will normally see to it that the destructor for a class and all its superclasses is called. The compiler may not be aware of the true class of the object if it was upcast to one of its base class. If the destructor was not declared virtual, then the derived class destructor and any other base class constructors will not get called. Thus many developers declare all destructors virtual.</p> <p><strong>Java</strong></p> <p>Java does not chain finalize() methods, so the derived class should explicitly call the parent.</p> <p><a name="destroy-object" id="destroy-object"></a></p> <h2 id="toc216"><span>destroy object</span></h2> <p><strong>Java</strong></p> <p>finalize() is called by the Java garbage collector.</p> <p><a name="define-method" id="define-method"></a></p> <h2 id="toc217"><span>define method</span></h2> <p><a name="invoke-method" id="invoke-method"></a></p> <h2 id="toc218"><span>invoke method</span></h2> <p><a name="define-class-method" id="define-class-method"></a></p> <h2 id="toc219"><span>define class method</span></h2> <p><a name="invoke-class-method" id="invoke-class-method"></a></p> <h2 id="toc220"><span>invoke class method</span></h2> <p><a name="receiver" id="receiver"></a></p> <h2 id="toc221"><span>name of receiver</span></h2> <p><a name="access-control" id="access-control"></a></p> <h2 id="toc222"><span>access control</span></h2> <p><strong>objective c:</strong></p> <p>Access control only applies to members; all methods are public. gcc 4.0 does not enforce the access restrictions; it merely gives warnings.</p> <p><a name="anonymous-class" id="anonymous-class"></a></p> <h2 id="toc223"><span>anonymous class</span></h2> <p><a name="inheritance-polymorphism-note" id="inheritance-polymorphism-note"></a></p> <h1 id="toc224"><span><a href="cpp#inheritance-polymorphism">Inheritance and Polymorphism</a></span></h1> <p><a name="dynamic-dispatch" id="dynamic-dispatch"></a></p> <h2 id="toc225"><span>dynamic dispatch</span></h2> <p><a name="static-dispatch" id="static-dispatch"></a></p> <h2 id="toc226"><span>static dispatch</span></h2> <p>Method dispatch is <em>static</em> if the method is determined by the variable type, and <em>dynamic</em> if it is determined by the value type. These techniques of method dispatch yield different results when both the base class and the derived class have implementations for a method, and an instance of the derived class is being stored in a variable with type of the base class.</p> <p>When dispatch is static, the compiler can determine the code that will be executed for the method call. When dispatch is dynamic, the code that will be executed is a runtime decision. C++ implementations usually achieve this by storing function pointers in the object: qv <a href="http://en.wikipedia.org/wiki/Virtual_method_table">virtual method table</a>.</p> <p>The use of the keyword <em>static</em> in the declaration of a class method in C++, Java, and C# is perhaps unfortunate. Class methods are always statically dispatched, so the concepts are not unrelated.</p> <p><a name="subclass" id="subclass"></a></p> <h2 id="toc227"><span>subclass</span></h2> <p><a name="superclass-constructor" id="superclass-constructor"></a></p> <h2 id="toc228"><span>superclass constructor</span></h2> <p><a name="underivable-class" id="underivable-class"></a></p> <h2 id="toc229"><span>mark class underivable or method overrideable</span></h2> <p><a name="root-class" id="root-class"></a></p> <h2 id="toc230"><span>root class</span></h2> <p>Name of the root class, if there is one.</p> <p><strong>objective c:</strong></p> <p>It is possible to define a root class other than NSObject.</p> <p><a name="root-class-methods" id="root-class-methods"></a></p> <h2 id="toc231"><span>root class methods</span></h2> <p>A selection of methods available on the root class.</p> <p><a name="reflection-note" id="reflection-note"></a></p> <h1 id="toc232"><span><a href="cpp#reflection">Reflection</a></span></h1> <p><a name="type-class" id="type-class"></a></p> <h2 id="toc233"><span>get type class of object</span></h2> <p><a name="get-type-class-string" id="get-type-class-string"></a></p> <h2 id="toc234"><span>get type class from string</span></h2> <p><a name="get-type-class-identifier" id="get-type-class-identifier"></a></p> <h2 id="toc235"><span>get type class from type identifier</span></h2> <p><strong>c++:</strong></p> <p><em>typeid</em> returns a value of type <em>type_info</em>. The assignment method and copy constructor of <em>type_info</em> are private.</p> <p><a name="class-name" id="class-name"></a></p> <h2 id="toc236"><span>class name</span></h2> <p>*c++:**</p> <p>The string returned by <em>type_info.name()</em> contains more than the class name. The code below displayed the string "Z4mainE3Foo" when run on my system.</p> <div class="code"> <pre> <code>class Foo { int i; }; puts(typeid(Foo).name());</code> </pre></div> <p><a name="get-methods" id="get-methods"></a></p> <h2 id="toc237"><span>get methods</span></h2> <p><a name="has-method" id="has-method"></a></p> <h2 id="toc238"><span>has method</span></h2> <p><a name="invoke-method-object" id="invoke-method-object"></a></p> <h2 id="toc239"><span>invoke method object</span></h2> <p><a name="net-web-note" id="net-web-note"></a></p> <h1 id="toc240"><span><a href="cpp#net-web">Net and Web</a></span></h1> <p><a name="url-encode-note" id="url-encode-note"></a></p> <h2 id="toc241"><span><a href="cpp#url-encode">url encode/decode</a></span></h2> <p>How to URL encode and URL decode a string.</p> <p>URL encoding is also called percent encoding. It is used to escape special characters in GET query string parameters.</p> <p>Reserved characters according to <a href="http://www.ietf.org/rfc/rfc3986.txt">RFC 3986</a> are replaced by a percent sign % followed by a two hex digit representation of the ASCII code. The reserved characters are:</p> <div class="code"> <pre> <code>! * ' ( ) ; : @ & = + $ , / ? # [ ]</code> </pre></div> <p>Spaces can optionally be represented by a plus sign +.</p> <p><a name="unit-tests-note" id="unit-tests-note"></a></p> <h1 id="toc242"><span><a href="cpp#unit-tests">Unit Tests</a></span></h1> <p><a name="debugging-profiling-note" id="debugging-profiling-note"></a></p> <h1 id="toc243"><span><a href="cpp#debugging-profiling">Debugging and Profiling</a></span></h1> <p><a name="cpp" id="cpp"></a></p> <h1 id="toc244"><span><a href="cpp#top">C++</a></span></h1> <p><a href="http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2012/n3337.pdf">C++11 Standard (pdf)</a><br /> <a href="http://www.cplusplus.com/reference/">Standard C++ Library Reference</a><br /> <a href="http://gcc.gnu.org/onlinedocs/libstdc++/">The GNU C++ Library</a><br /> <a href="http://google-styleguide.googlecode.com/svn/trunk/cppguide.xml">Google C++ Style Guide</a><br /> <a href="http://doc.qt.digia.com/qq/qq13-apis.html">Designing Qt-Style C++ APIs</a></p> <p><strong>C++11 and gcc</strong></p> <p><tt>gcc</tt> provides a C++11 compiler which will run on Linux, Mac OS X, and Windows. For a complete implementation of the C++11 standard one should use <tt>gcc</tt> 4.8 or later, but <tt>gcc</tt> 4.6 is sufficient for the examples in this sheet. Support for C++11 features must be requested with the <tt>-std=c++0x</tt> flag.</p> <p><strong>C++11 on Mac OS X</strong></p> <p>The version of <tt>gcc</tt> that comes with XCode in Mac OS X 10.8 as part of the command line tools package is 4.2. This version of <tt>gcc</tt> does not support C++11, but the command line tools package also provides the <tt>clang</tt> compiler which does:</p> <div class="code"> <pre> <code>clang++ -std=c++11 -stdlib=libc++ -o hello hello.cpp</code> </pre></div> <p>Like <tt>gcc</tt>, <tt>clang</tt> does not compile C++11 by default, hence the <tt>-std=c++11</tt> flag. One must also specify the C++ standard library, since the <tt>libc++</tt> library which comes with <tt>clang</tt> will support C++11, but the <tt>libstdc++</tt> library provided with <tt>gcc</tt> 4.2 will not, and <tt>clang</tt> uses the <tt>gcc</tt> library by default.</p> <p><strong>C++11 Windows</strong></p> <p>The Visual Studio 2012 C++ compiler does not completely support C++11, but it has support for the features mentioned in this sheet. For a complete C++11 compiler, install MinGW and <tt>gcc</tt> 4.8.</p> <p><strong>Compatibility with C</strong></p> <p><strong>What's New in C++11</strong></p> <p><a name="objective-c" id="objective-c"></a></p> <h1 id="toc245"><span><a href="cpp#top">Objective-C</a></span></h1> <p><a href="https://developer.apple.com/library/mac/#documentation/Cocoa/Conceptual/ProgrammingWithObjectiveC/Introduction/Introduction.html">Programming with Objective-C</a> Apple<br /> <a href="http://www.gnu.org/software/gnustep/resources/documentation/Developer/Base/ProgrammingManual/manual_toc.html">GNUstep</a><br /> <a href="http://developer.apple.com/mac/library/documentation/cocoa/reference/foundation/objc_classic/index.html">Mac OS X Foundation Framework</a></p> <p><a name="java" id="java"></a></p> <h1 id="toc246"><span><a href="cpp#top">Java</a></span></h1> <p><a href="http://java.sun.com/javase/6/docs/api/">Java 1.6 API</a><br /> <a href="https://jdk7.java.net/">JDK 7 Project</a><br /> <a href="http://java.sun.com/docs/books/jvms/second_edition/html/VMSpecTOC.doc.html">JVM Specification 2nd Ed</a><br /> <a href="http://java.sun.com/docs/books/jls/">The Java Language Specification 3rd Ed</a></p> <p>A Java program is created from source code via an explicit compilation step using <tt>javac</tt>. The executable is then launched using the virtual machine <tt>java</tt>. Both <tt>javac</tt> and <tt>java</tt> use the Java runtime jar <tt>rt.jar</tt>, which contains the Java standard libraries.</p> <div class="code"> <pre> <code>$ ls -l $(which javac) -rwxrwxr-x 1 root wheel 99296 Oct 8 2013 /Library/JVM/jdk7.0/Contents/Home/bin/javac $ ls -l $(which java) -rwxrwxr-x 1 root wheel 99216 Oct 8 2013 /Library/JVM/jdk7.0/Contents/Home/bin/java $ ls -l $JAVA_HOME/jre/lib/rt.jar -rw-rw-r-- 1 root wheel 64181940 Oct 8 2013 /Library/JVM/jdk7.0/Contents/Home/jre/lib/rt.jar $ cat Main.java public class Main { public static void main(String[] argv) throws Throwable { System.out.println("Hello, World!"); } } $ javac Main.java $ ls -l Main.class -rw-r--r-- 1 hpglot staff 463 Dec 27 06:12 Main.class $ java Main Hello, World!</code> </pre></div> <p>Using <tt>dtruss</tt> on Mac OS X to verify that <tt>javac</tt> and <tt>java</tt> use <tt>rt.jar</tt>:</p> <div class="code"> <pre> <code>$ sudo dtruss javac Main.java 2>&1 | grep -E '^open' | grep rt.jar open("/Library/JVM/jdk7.0/Contents/Home/jre/lib/rt.jar\0", 0x0, 0x0) = 4 0 $ sudo dtruss java Main 2>&1 | grep -E '^open' | grep rt.jar open("/Library/JVM/jdk7.0/Contents/Home/jre/lib/rt.jar\0", 0x0, 0x0) = 4 0</code> </pre></div> <p><a name="c-sharp" id="c-sharp"></a></p> <h1 id="toc247"><span><a href="cpp#top">C#</a></span></h1> <p><a href="http://standards.iso.org/ittf/PubliclyAvailableStandards/c042926_ISO_IEC_23270_2006(E).zip">C# Standard: ECMA-334</a><br /> <a href="http://www.go-mono.com/docs/">Mono API</a><br /> <a href="http://msdn.microsoft.com/en-us/library/67ef8sbd.aspx">C# Programming Guide</a> Microsoft</p> </div> </div> </div> <div id="license-area" class="license-area"> <a href="https://github.com/clarkgrubb/hyperpolyglot/issues">issue tracker</a> | content of this page licensed under <a rel="license" href="http://creativecommons.org/licenses/by-sa/3.0/"> creative commons attribution-sharealike 3.0</a> <br> </div> </div> </div> </div> <script type="text/javascript"> var _gaq = _gaq || []; _gaq.push(['_setAccount', 'UA-17129977-2']); _gaq.push(['_trackPageview']); (function() { var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true; ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js'; var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s); })(); </script> </body> </html>