# top//a side-by-side reference sheet//
[#grammar-execution grammar and execution] | [#variables-expressions variables and expressions] | [#arithmetic-logic arithmetic and logic] | [#strings strings] | [#regexes regexes] | [#dates-time dates and time] | [#fixed-length-arrays fixed-length arrays] | [#resizable-arrays resizable arrays] | [#tuples tuples] | [#dictionaries dictionaries] | [#functions functions] | [#execution-control execution control] | [#exceptions exceptions] | [#concurrency concurrency] | [#file-handles file handles] | [#files files] | [#file-fmt file formats] | [#directories directories] | [#processes-environment processes and environment] | [#libraries-namespaces libraries and namespaces] | [#user-defined-types user-defined types] | [#generic-types generic types] | [#objects objects] | [#inheritance-polymorphism inheritance and polymorphism] | [#reflection reflection] | [#net-web net and web] | [#unit-tests unit tests] | [#debugging-profiling debugging and profiling]
||||||||||~ # version[#version-note version]|| ||~ ||~ [#cpp c++]||~ [#objective-c objective c]||~ [#java java]||~ [#c-sharp c#]|| ||# version-used[#version-used-note version used] _ @< >@||##gray|//C++11// _ //gcc 4.8// _ //clang 3.5//##||##gray|//gcc 4.2//##||##gray|//java 1.7//##||##gray|//mono 2.10 (C# 4.0)//##|| ||# show-version[#show-version-note show version] _ @< >@||$ g++ @@–@@version||$ gcc @@–@@version||$ javac -version||$ mcs @@–@@version|| ||# implicit-prologue[#implicit-prologue-note implicit prologue]||#include _ #include _ _ using namespace std;|| || || || ||||||||||~ # grammar-execution[#grammar-execution-note grammar and execution]|| ||~ ||~ c++||~ objective c||~ java||~ c#|| ||# hello-world[#hello-world-note hello world]||$ cat hello.cpp _ #include _ _ using namespace std; _ _ int main(int argc, char@@@@ arg) { _ @< >@cout @@<<@@ “Hello, World!” @@<<@@ endl; _ } _ _ $ g++ -std=c++0x hello.cpp _ _ $ ./a.out||$ cat hello.m _ #include _ _ int main(int argc, char @@@@argv) { _ @< >@printf(“Hello, World!\n”); _ } _ _ $ gcc hello.m _ _ $ ./a.out||$ cat Hello.java _ public class Hello { _ @< >@public static void main(String[] args) { _ @< >@@< >@System.out.println(“Hello, World!”); _ @< >@} _ } _ _ $ javac Hello.java _ _ $ java Hello||$ cat hello.cs _ using System; _ _ public class Hello { _ @< >@public static void Main() { _ @< >@@< >@Console.WriteLine(“Hello, World!”); _ @< >@} _ } _ _ $ mcs hello.cs _ _ $ mono hello.exe|| ||# file-suffixes[#file-suffixes-note file suffixes] _ ##gray|//source, header, object file//##||foo.cpp _ foo.h _ foo.o||Foo.m _ Foo.h _ Foo.o||Foo.java _ ##gray|//none//## _ Foo.class _ _ ##gray|Foo.java //must define a single top level class// Foo##||Foo.cs _ ##gray|//none//## _ Foo.exe ##gray|//or//## Foo.dll _ _ ##gray|//although files are often named after a class they contain, this is not required//##|| ||# block-delimiters[#block-delimiters-note block delimiters] _ @< >@||{ }||{ }||{ }||{ }|| ||# stmt-terminator[#stmt-terminator-note statement terminator] _ @< >@||;||;||;||;|| ||# top-level-stmt[#top-level-stmt-note top level statements]||##gray|//A source file will normally have// #include //directives at the top, followed by declarations, definitions, and namespaces containing declarations and definitions. _ _ After the preprocessor has finished processing a source file, the compilation unit will only contain declarations, definitions, and namespaces at the top level.//##|| ||##gray|//each file contains the following elements in order: _ _ (1) optional package directive _ (2) zero or more import directives _ (3) one public class definition and zero or more private class definitions//##|| || ||# eol-comment[#eol-comment-note end-of-line comment] _ @< >@||##gray|@@//@@ comment##||##gray|@@//@@ comment##||##gray|@@//@@ comment##||##gray|@@//@@ comment##|| ||# multiple-line-comment[#multiple-line-comment-note multiple line comment]||##gray|/* comment _ another comment /##||##gray|/ comment _ another comment /##||##gray|/ comment _ another comment /##||##gray|/ comment _ another comment */##|| ||||||||||~ # variables-expressions[#variables-expressions-note variables and expressions]|| ||~ ||~ c++||~ objective c||~ java||~ c#|| ||# local-var[#local-var-note local variable]||int i; _ int j = 3; _ int k(7);||int i; _ int j = 3;||int i; _ int j = 3;||int i; _ int j = 3;|| ||# uninitialized-local-var[#uninitialized-local-var-note uninitialized local variable]||##gray|//The behavior is undefined. _ _ Most implementations do not zero-initialize stack variables, so the value will be whatever happened to be in memory.//##||##gray|//behavior is undefined. _ _ Most implementations do not zero-initialize stack variables, so the value will be whatever happened to be in memory.//##||##gray|//compiler error//##||##gray|//compiler prevents use of uninitialized local variable//##|| ||# global-var[#global-var-note global variable]||##gray|@@//@@ in foo.cpp and outside of any function _ @@//@@ or class definition:## _ int foo = 7; _ _ ##gray|@@//@@ in bar.cpp and outside of any function _ @@//@@ or class definition:## _ extern int foo;||##gray|//in// foo.cpp //outside of any function or class definition://## _ int foo = 7; _ _ ##gray|//in// bar.cpp //outside of any function or class definition://## _ extern int foo;||##gray|//foo/Foo.java://## _ package foo; _ _ ##gray|@@//@@ globals must be declared inside a _ @@//@@ class:## _ public class Foo { _ @< >@public static int bar; _ } _ _ ##gray|//UseFoo.java://## _ import foo.Foo; _ _ public class UseFoo { _ @< >@public static void main(String[] args) { _ @< >@@< >@System.out.println(Foo.bar); _ @< >@} _ }|| || ||# uninitialized-global-var[#uninitialized-global-var-note uninitialized global variable]||##gray|//Zero initialized: numeric types and pointers are set to zero. Classes, structs, and arrays have all of their members or elements zero-initialized recursively.//##|| ||##gray|//Zero initialized.//##|| || ||# write-once-var[#write-once-var-note write-once variable]||const int i = 7;||const int i = 7;||final int i = 7;||const int i = 7;|| ||# assignment[#assignment-note assignment]||int n; _ n = 3;|| ||int n; _ n = 3;|| || ||# compound-assignment[#compound-assignment-note compound assignment] _ ##gray|//arithmetic, bit//##||+= -= *= /= %= _ @@<<= >>= @@&= ^= |=|| ||+= -= = /= %= _ @@<<= >>= @@&= ^= |= _ _ ##gray|@@>>=@@ //is arithmetic right shift,// @@>>>=@@ //is logical right shift//##|| || ||# incr-decr[#incr-decr-note increment and decrement]||int n = 1; _ int one = n++; _ int three = ++n; _ int two = @@–@@n;|| ||int n = 1; _ int one = n++; _ int three = ++n; _ int two = @@–@@n;|| || ||# addr[#addr-note address]||int i(3); _ int ip = &i;|| ||##gray|//none//##|| || ||# dereference[#dereference-note dereference]||int i(3); _ int* ip = &i; _ int i2 = ip + 1;|| ||##gray|//none//##|| || ||# type-size[#type-size-note type size]||cout @@<<@@ sizeof(int) @@<<@@ endl; _ cout @@<<@@ sizeof(int) @@<<@@ endl;|| ||##gray|//none//##|| || ||# addr-arith[#addr-arith-note address arithmetic]|| || ||##gray|//none//##|| || ||# unique-ptr[#unique-ptr-note unique pointer]|| || ||##gray|//none//##|| || ||# ref-cnt-ptr[#ref-cnt-ptr-note reference count pointer]|| || ||##gray|//none//##|| || ||# weak-ptr[#weak-ptr-note weak pointer]|| || ||##gray|//none//##|| || ||# allocate-heap[#allocate-heap-note allocate heap]||int* ip = new int;||#include _ _ int *ip = malloc(sizeof(int));||##gray|@@//@@ Primitive types are stack allocated. _ @@//@@ Use a wrapper class to store on the _ @@//@@ heap:## _ Integer i = new Integer(0);||object i = 0;|| ||# uninitialized-heap[#uninitialized-heap-note uninitialized heap]||##gray|//Memory allocated by the// new //operator is zero-initialized.//##|| ||##gray|//zero-initialized//##|| || ||# free-heap[#free-heap-note free heap]||delete ip;||#include _ _ free(ip);||##gray|//garbage collected//##||##gray|//garbage collected//##|| ||# null[#null-note null] _ @< >@||NULL||NULL||null||null|| ||# coalesce[#coalesce-note coalesce] _ @< >@||string s1 = s2 @@||@@ “was null”;||NSString *s1 = s2 @@||@@ @"was null”;||String s1 = s2 == null ? “was null” : s2;||string s1 = s2 ?? “was null”;|| ||||||||||~ # arithmetic-logic[#arithmetic-logic-note arithmetic and logic]|| ||~ ||~ c++||~ objective c||~ java||~ c#|| ||# boolean-type[#boolean-type-note boolean type] _ @< >@||bool||BOOL||boolean||bool|| ||# true-false[#true-false-note true and false] _ @< >@||true false||YES NO||true false||true false|| ||# falsehoods[#falsehoods-note falsehoods] _ @< >@||false 0 0.0 NULL||0 0.0 NULL||false||false|| ||# logical-op[#logical-op-note logical operators]||&& @@||@@ ! _ and or not||&& @@||@@ !||&& @@||@@ !||&& @@||@@ !|| ||# relational-op[#relational-op-note relational operators]||== != < > <= >=||== != < > <= >=||== != < > <= >=||== != < > <= >=|| ||# int-type[#int-type-note integer type]||signed char n1;@< >@@< >@##gray|@@//@@ 1+ bytes## _ short int n2;@< >@@< >@@< >@##gray|@@//@@ 2+ bytes## _ int n3;@< >@@< >@@< >@@< >@@< >@@< >@##gray|@@//@@ 2+ bytes## _ long int n4;@< >@@< >@@< >@##gray|@@//@@ 4+ bytes## _ long long int n5;@< >@##gray|@@//@@ 4+ bytes##||signed char ##gray|//1+ byte//### _ short int ##gray|//2+ bytes//## _ int ##gray|//2+ bytes//## _ long int ##gray|//4+ bytes//## _ long long int ##gray|//4+ bytes//##||byte n1;@< >@##gray|@@//@@ 1 byte## _ short n2; ##gray|@@//@@ 2 bytes## _ int n3;@< >@##gray|@@//@@ 4 bytes## _ long n4;@< >@##gray|@@//@@ 8 bytes##||sbyte ##gray|//1 byte//## _ short ##gray|//2 bytes//## _ int ##gray|//4 bytes//## _ long ##gray|//8 bytes//##|| ||# unsigned-type[#unsigned-type-note unsigned type]||unsigned char n1;@< >@@< >@@< >@@< >@@< >@##gray|@@//@@ 1+ bytes## _ unsigned short int n2;@< >@@< >@@< >@##gray|@@//@@ 2+ bytes## _ unsigned int n3;@< >@@< >@@< >@@< >@@< >@@< >@##gray|@@//@@ 2+ bytes## _ unsigned long int n4;@< >@@< >@@< >@##gray|@@//@@ 4+ bytes## _ unsigned long long int n5; ##gray|@@//@@ 4+ bytes##||unsigned char: 8+ _ unsigned short int ##gray|//2 bytes+//## _ unsigned int ##gray|//2 bytes+//## _ unsigned long int ##gray|//4+ bytes//## _ unsigned long long int ##gray|//4+ bytes//##||char n1;@< >@##gray|@@//@@2 bytes##||byte ##gray|//1 byte//## _ ushort ##gray|//2 bytes//## _ uint ##gray|//4 bytes//## _ ulong ##gray|//8 bytes//##|| ||# float-type[#float-type-note float type]||float x1;@< >@@< >@@< >@@< >@##gray|@@//@@ 4 bytes## _ double x2;@< >@@< >@@< >@##gray|@@//@@ 8 bytes## _ long double x3; ##gray|@@//@@ 16 bytes##||float _ double _ long double||float x1;@< >@##gray|@@//@@ 4 bytes## _ double x2; ##gray|@@//@@ 8 bytes##||float ##gray|//4 bytes//## _ double ##gray|//8 bytes//##|| ||# fixed-type[#fixed-type-note fixed type] _ @< >@||##gray|//none//##||##gray|//none//##||##gray|//none//##||decimal ##gray|//12 bytes//##|| ||# arithmetic-op[#arithmetic-op-note arithmetic operators]||+ - * / %||+ - * / %||+ - * / %||+ - * / %|| ||# int-div[#int-div-note integer division]||##gray|@@//@@ evaluates to 2:## _ 7 / 3||##gray|@@//@@ evaluates to 2:## _ 7 / 3||##gray|@@//@@ evaluates to 2:## _ 7 / 3||##gray|@@//@@ evaluates to 2:## _ 7 / 3|| ||# int-div-zero[#int-div-zero-note integer division by zero]||##gray|//process sent a//## SIGFPE ##gray|//signal//##||##gray|//process sent a//## SIGFPE ##gray|//signal//##||##gray|//throws//## java.lang.ArithmeticException||##gray|//Syntax error if divisor is a constant. Otherwise throws//## System.DivideByZeroException|| ||# float-div[#float-div-note float division] _ @< >@||7 / static_cast(3)||7 / (float)3||7 / (float)3||7 / (float)3|| ||# float-div-zero[#float-div-zero-note float division by zero] _ ##gray|//dividend is positive, zero, negative//##||inf _ nan _ -inf _ _ ##gray|//There are no portably defined literals or constants for the above values.//##||inf _ nan _ -inf _ _ ##gray|//there are no portably defined literals or constants for the above values.//##||Float.POSITIVE_INFINITY _ Float.NaN _ Float.NEGATIVE_INFINITY _ _ ##gray|//constants with same names defined in//## Double||float.PositiveInfinity _ float.NaN _ float.NegativeInfinity _ _ ##gray|//constants with same names defined in//## double|| ||# power[#power-note power]||#include _ _ double x = pow(2.0, 32.0);||#include _ _ pow(2.0, 32.0);||Math.pow(2.0, 32.0);||System.Math.Pow(2.0, 32.0);|| ||# sqrt[#sqrt-note sqrt]||#include _ _ double x = sqrt(2);||#include _ _ sqrt(2)||Math.sqrt(2)||Math.Sqrt(2)|| ||# sqrt-negative-one[#sqrt-negative-one-note sqrt -1]||nan||nan||Double.NaN||double.NaN|| ||# transcendental-func[#transcendental-func-note transcendental functions]||#include _ _ exp log log2 log10 _ sin cos tan _ asin acos atan _ atan2||#include _ _ exp log log2 log10 _ sin cos tan _ asin acos atan _ atan2||Math.exp Math.log ##gray|//none//## Math.log10 _ Math.sin Math.cos Math.tan _ Math.asin Math.acos Math.atan _ Math.atan2||using System; _ @< >@ _ Math.Exp Math.Log ##gray|//none//## Math.Log10 _ Math.Sin Math.Cos Math.Tan _ Math.Asin Math.Acos Math.Atan _ Math.Atan2|| ||# transcendental-const[#transcendental-const-note transcendental constants]||#include _ _ double e = M_E; _ double pi = M_PI;||#include _ _ M_E _ M_PI||Math.E _ Math.PI||System.Math.E _ System.Math.PI|| ||# float-truncation[#float-truncation-note float truncation] _ ##gray|//towards zero, to nearest integer, towards -∞, towards ∞//##||#include _ @< >@ _ double x = 3.7; _ @< >@ _ long trnc = static_cast(x); _ long rnd = round(x); _ long flr = floorl(x); _ long cl = ceill(x);||#include _ @< >@ _ double d = 3.77; _ @< >@ _ long trnc = (long)d; _ long rnd = round(d); _ long flr = floorl(d); _ long cl = ceill(d);||(long)3.77 _ Math.round(3.77) _ (long)Math.floor(3.77) _ (long)Math.ceil(3.77)||using System; _ @< >@ _ (long)3.77 _ Math.Round(3.77) _ Math.Floor(3.77) _ Math.Ceiling(3.77)|| ||# absolute-val[#absolute-val-note absolute value]||#include @< >@@< >@##gray|@@//@@ fabs()## _ #include @< >@##gray|@@//@@ abs()## _ _ int n = -7; _ int absn = abs(n); _ _ double x = -7.77; _ double absx = fabs(x);||#include @< >@##gray|@@//@@ abs()## _ #include @< >@##gray|@@//@@ fabs()## _ _ int i = -7; _ int ai = abs(i); _ _ float x = -7.77; _ float ax = fabs(x);||Math.abs(-7) _ Math.abs(-7.77)||System.Math.Abs(-7) _ System.Math.Abs(-7.77)|| ||# int-overflow[#int-overflow-note integer overflow]||##gray|//modular arithmetic _ _ The C standard does not define behavior for signed integers, however.//##||##gray|//modular arithmetic _ _ The C standard does not define behavior for signed integers, however.//##||##gray|//modular arithmetic//##||##gray|//modular arithmetic//##|| ||# float-overflow[#float-overflow-note float overflow]||##gray|//no behavior defined by standard; many implementations return// inf##||##gray|//no behavior defined by standard; many implementations return// inf##||Float.POSITIVE_INFINITY||float.PositiveInfinity|| ||# float-limits[#float-limits-note float limits] _ ##gray|//largest finite float, smallest positive float//##||#include _ _ FLT_MAX _ FLT_MIN _ DBL_MAX _ DBL_MIN _ LDBL_MAX _ LDBLMIN|| ||Float.MAXVALUE _ Float.MIN_VALUE _ Double.MAX_VALUE _ Double.MIN_VALUE||float.MaxValue _ float.Epsilon _ double.MaxValue _ double.Epsilon|| ||# complex-construction[#complex-construction-note complex construction]||#include _ _ complex z(1.0, 2.0);|| || || || ||# complex-decomposition[#complex-decomposition-note complex decomposition] _ ##gray|//real and imaginary component, argument, absolute value, conjugate//##||z.real() _ z.imag() _ arg(z) _ abs(z) _ conj(z)|| || || || ||# random-num[#random-num-note random number] _ ##gray|//uniform integer, uniform float, normal float//##||#include _ _ defaultrandomengine dre; _ _ uniformintdistribution uid(0, 99); _ uniformrealdistribution _ @< >@urd(0.0, 1.0); _ normal_distribution nd(0.0, 1.0); _ _ int i = uid(dre); _ double x = urd(dre); _ double y = nd(dre);||#include _ _ ##gray|@@//@@ assuming 100 much smaller than RAND_MAX:## _ int i = rand() % 100; _ double x = drand48(); _ ##gray|//none//##||import java.util.Random; _ _ Random rnd = new Random(); _ _ int i = rnd.nextInt(100); _ double x = rnd.nextDouble(); _ double y = rnd.nextGaussian();||using System; _ _ Random rnd = new Random(); _ _ int i = rnd.Next(); _ double x = rnd.NextDouble(); _ ##gray|//none//##|| ||# random-seed[#random-seed-note random seed]||#include _ _ ##gray|@@//@@ set seed in constructor:## _ defaultrandomengine dre(17); _ _ ##gray|@@//@@ set seed of existing engine:## _ dre.seed(17);|| ||import java.util.Random; _ _ Random rnd = new Random(); _ _ rnd.setSeed(17); _ _ ##gray|@@//@@ seed can also be passed to constructor##||using System; _ _ Random rnd = new Random(17);|| ||# bit-op[#bit-op-note bit operators]||@@ << >> & | ^ ~ @@ _ @< >@bitand bitor compl _ _ ##gray|@@>>@@ //is arithmetic right shift on signed integers and logical right shift on unsigned integers//##||@@ << >> & | ^ ~ @@||@@ << >> & | ^ ~ @@ _ _ ##gray|@@>>@@ //is arithmetic right shift, @@>>>@@ is logical right shift//##||@@ << >> & | ^ ~ @@|| ||# binary-octal-hex-literals[#binary-octal-hex-literals-note binary, octal, and hex literals]||0b0101010 _ 052 _ 0x2a|| ||##gray|//none in Java 1.6//## _ 052 _ 0x2a||##gray|//none//## _ 052 _ 0x2a|| ||# radix[#radix-note radix] _ ##gray|//convert integer to and from string with radix//##|| || ||Integer.toString(42, 7) _ Integer.parseInt(“60”, 7)|| || ||||||||||~ # strings[#strings-note strings]|| ||~ ||~ c++||~ objective c||~ java||~ c#|| ||# str-type[#str-type-note string type] _ @< >@||string s(“lorem ipsum”); _ _ ##gray|@@//@@ convert to C string:## _ const char* s2 = s.c_str();||NSString* s = @"lorem ipsum”; _ _ ##gray|@@//@@ convert to C string:## _ const char* s2 = [s UTF8String];||java.lang.String||string|| ||# str-literal[#str-literal-note string literal] _ @< >@||##gray|@@//@@ const char:## _ “don’t say "no"”||@"don’t say "no””||"don’t say"no"”||"don’t say "no"”|| ||# newline-literal[#newline-literal-note newline in literal]||##gray|//Newlines in string literals are ignored.//##||##gray|//string literals can extend over multiple lines, but the newlines do not appear in the resulting string//##||##gray|//no//##||##gray|//string literals can extend over multiple lines, but the newlines do not appear in the resulting string//##|| ||# str-literal-escape[#str-literal-escape-note literal escapes] _ @< >@||\a \b \f \n \r \t \v _ \ " \’ _ \x##gray|//hh//## ##gray|//o//## ##gray|//oo//## ##gray|//ooo//##||\a \b \f \n \r \t \v _ \ " \’ _ \x##gray|//hh//## ##gray|//o//## ##gray|//oo//## ##gray|//ooo//##||\b \f \n \r \t _ \ " \’ _ \u##gray|//hhhh//## ##gray|//o//## ##gray|//oo//## ##gray|//ooo//##||\a \b \f \n \r \t \v _ \ " \’ _ \x##gray|//hh//## \x##gray|//hhhh//## ##gray|//o//## ##gray|//oo//## ##gray|//ooo//##|| ||# allocate-str[#allocate-str-note allocate string]||string s = new string(“hello”);||NSString *s = @"hello”;||String s = “hello”; _ String t = new String(s);||string s = “hello”; _ string t = string.Copy(s);|| ||# mutable-str[#mutable-str-note are strings mutable?]||string s(“bar”); _ s[2] = ‘z’;|| ||##gray|String //objects are immutable.//## _ _ ##gray|StringBuffer //has// append(), delete(), deleteCharAt(), insert(), replace(), setCharAt().##|| || ||# copy-str[#copy-str-note copy string]||string s(“bar”); _ _ ##gray|@@//@@ use assignment or copy constructor:## _ string s2 = s; _ string s3(s); _ _ ##gray|@@//@@ s contains “baz”;## _ ##gray|@@//@@ s2 and s3 contain “bar”:## _ s[2] = ‘z’;|| ||String s = “bar”; _ StringBuffer sb = new StringBuffer(s); _ sb.setCharAt(2, ‘z’); _ ##gray|@@//@@ s contains “bar”; s2 contains “baz”:## _ String s2 = sb.toString();|| || ||# fmt-str[#fmt-str-note format string]||#include _ _ ostringstream oss; _ oss @@<<@@ “Spain: “ @@<<@@ 7; _ string s(oss.str());||[NSString stringWithFormat:@”%@: %d”, @"Spain”, 7]||String.format(“%s: %d”, “Spain”, 7)||string.Format(“{0}: {1}”, “Spain”, 7)|| ||# compare-str[#compare-str-note compare strings]||string s1(“hello”); _ string s2(“world”); _ _ ##gray|@@//@@ negative if s1 lexically before s2; _ @@//@@ zero if s1 and s2 are equal:## _ int result1 = s1.compare(s2); _ _ bool result2 = s1 == s2;||[@"hello” compare:@"hello”]||"hello”.compareTo(“world”)||"hello”.CompareTo(“world”)|| ||# str-concat[#str-concat-note concatenate] _ ##gray|//and append//##||string s(“hello”); _ string s2 = s + “ world”; _ s += “ world”;||NSString *s1 = @"hello”; _ NSString *s2 = @” world”; _ NSString s3 = [s1 stringByAppendingString:s2];||"hello” + “ world”||"hello” + “ world”|| ||# str-replicate[#str-replicate-note replicate] _ @< >@||string hbar(80, ‘-’);|| ||import java.util.Arrays; _ _ char[] a = new char[80]; _ Arrays.fill(a, ‘-’); _ String s = new String(a);|| || ||# translate-case[#translate-case-note translate case]||#include _ _ string s(“foo”); _ _ ##gray|@@//@@ in place:## _ transform(s.begin(), s.end(), _ @< >@@< >@@< >@@< >@@< >@s.begin(), ::toupper); _ transform(s.begin(), s.end(), _ @< >@@< >@@< >@@< >@@< >@s.begin(), ::tolower); _ _ ##gray|@@//@@ non-destructive:## _ string s2; _ s2.resize(s.size(); _ transform(s.begin(), s.end(), _ @< >@@< >@@< >@@< >@@< >@s2.begin(), ::toupper);||[@"HELLO” lowercaseString]||"hello”.toUpperCase() _ “HELLO”.toLowerCase()||"hello”.ToUpper() _ HELLO”.ToLower()|| ||# trim[#trim-note trim]||#include _ _ string s(“ hello “); _ ##gray|@@//@@ trim in place on left:## _ s.erase( _ @< >@s.begin(), _ @< >@find_if( _ @< >@@< >@s.begin(), _ @< >@@< >@s.end(), _ @< >@@< >@not1(ptr_fun(isspace)) _ @< >@) _ ); _ _ ##gray|@@//@@ trim in place on right:## _ s.erase( _ @< >@find_if( _ @< >@@< >@s.rbegin(), _ @< >@@< >@s.rend(), _ @< >@@< >@not1(ptr_fun(isspace)) _ @< >@).base(), _ @< >@s.end() _ );||[@” hello “ stringByTrimmingCharactersInSet: [NSCharacterSet whitespaceCharacterSet]]||” hello “.trim()||” hello “.Trim()|| ||# pad[#pad-note pad] _ ##gray|//on right, on left//##||#include _ #include _ _ string s(“hello”); _ string rpad(s); _ rpad += string(10 - s.length(), ‘ ‘); _ _ ostringstream oss; _ oss @@<<@@ setw(10) @@<<@@ s; _ string lpad(oss.str());||[@"hello” stringByPaddingToLength:10 withString:@” “ startingAtIndex:0]|| ||"hello”.PadLeft(10)|| ||# num-to-str[#num-to-str-note number to string]||char buf[100]; _ long n = 123; _ _ sprintf(buf, “%ld”, n); _ ##gray|/ prevent buffer overflow: /## _ snprintf(buf, 100, “%ld”, n);|| ||Integer.toString(14) _ Long.toString(14) _ Double.toString(14.7)||14.ToString() _ 14.7.ToString()|| ||# str-to-num[#str-to-num-note string to number]||#include _ _ stringstream ss(“7 14.3 12”); _ int n1; _ double x; _ long n2; _ _ ss @@>>@@ n1 @@>>@@ x @@>>@@ n2;||[@"14” integerValue] _ [@"14” longLongvalue] _ [@"14.7” floatValue] _ [@"14.7” doubleValue]||Byte.parseByte(“14”) _ Short.parseShort(“14”) _ Integer.parseInt(“14”) _ Long.parseLong(“14”) _ Float.parseFloat(“14.7”) _ Double.parseDouble(“14.7”)||byte.Parse(“14”) _ short.Parse(“14”) _ int.Parse(“14”) _ long.Parse(“14”) _ float.Parse(“14”) _ double.Parse(“14”) _ decimal.Parse(“14”)|| ||# join[#join-note join] _ @< >@|| || || ||System.String.Join(“, “, names)|| ||# split[#split-note split]|| ||[@"Bob Ned Amy” componentsSeparatedByString:@” “]||"Bob Ned Amy”.split(“ “)||string[] names = “Bob Ned Amy”.Split(‘ ‘);|| ||# serialize[#serialize-note serialize]|| || || || || ||# str-length[#str-length-note string length] _ @< >@||string s(“hello”); _ size_t len = s.length();||[s length]||s.length()||s.Length|| ||# index-substr[#index-substr-note index of substring] _ @< >@||string(“hello”).find(“ll”)||[@"hello” rangeOfString:@"ll”].location||"hello”.indexOf(“ll”)||"hello”.IndexOf(“ll”)|| ||# extract-substr[#extract-substr-note extract substring]||string(“hello”).substr(2, 2)||[@"hello” substringWithRange:NSMakeRange(2, 2)]||"hello”.substring(2,4)||"hello”.Substring(2, 2)|| ||# char-type[#char-type-note character type]||char _ wchar_t|| ||char _ Character||char _ Char|| ||# char-literal[#char-literal-note character literal]||char n = ‘X’;|| ||char n = ‘X’;||char n = ‘X’;|| ||# test-char[#test-char-note test character] _ ##gray|//letter, digit, whitespace, uppercase letter, lowercase letter//##||##gray|@@//@@ functions have this signature: _ @@//@@ _ @@//@@ @< >@int ()(int): _ @@//@@## _ isalpha _ isdigit _ isspace _ isupper _ islower|| || ||System.Char.IsLetter(Char) _ System.Char.IsNumber(Char) _ System.Char.IsWhiteSpace(Char) _ System.Char.IsUpper(Char) _ System.Char.IsLower(Char)|| ||||||||||~ # regexes[#regexes-note regular expressions]|| ||~ ||~ c++||~ objective c||~ java||~ c#|| ||# regex-type[#regex-type-note regex type]||regex _ wregex|| || || || ||# char-class-abbrev[#char-class-abbrev-note character class abbreviations]||. \d \D \s \S \w \W|| || || || ||# regex-anchors[#regex-anchors-note anchors]||^ $ \b \B|| || || || ||# regex-lookahead[#regex-lookahead-note lookahead] _ ##gray|//positive, negative//##||(?=##gray|//subpattern//##) _ (?!##gray|//subpattern//##)|| || || || ||# regex-test[#regex-test-note match test]||#include _ _ regex rx(“.ll.”); _ bool match = regex_match(“hello”, rx);||NSPredicate pred = [NSPredicate predicateWithFormat:@"SELF MATCHES %@”, @”.ll.”]; _ BOOL is_match = [pred evaluateWithObject:@"hello”];||boolean isMatch = “hello”.matches(“.ll.*”);||using System.Text.RegularExpressions; _ Regex regex = new Regex(“ll”); _ bool isMatch = regex.IsMatch(“hello”);|| ||# case-insensitive-regex[#case-insensitive-regex-note case insensitive match test]||#include _ _ regex rx(“lorem”, icase); _ bool match = regex_match(“Lorem”, rx);|| || || || ||# regex-modifiers[#regex-modifiers-note modifiers]|| || || || || ||# subst[#subst-note substitution]|| || ||String s1 = “hello”.replace(“ll”,"LL”); _ String s2 = “hello”.replaceAll(“l”,"L”);||using System.Text.RegularExpressions; _ Regex r1 = new Regex(“ll”); _ String s1 = r1.Replace(“hello”, “LL”, 1); _ Regex r2 = new Regex(“l”); _ String s2 = r2.Replace(“hello”, “L”);|| ||# match-prematch-postmatch[#match-prematch-postmatch-note match, prematch, postmatch]|| || || || || ||# group-capture[#group-capture-note group capture]|| || || || || ||||||||||~ # dates-time[#dates-time-note dates and time]|| ||~ ||~ c++||~ objective c||~ java||~ c#|| ||# date-time-type[#date-time-type-note date and time type] _ @< >@|| || ||java.util.Date||System.DateTime|| ||# current-date-time[#current-date-time-note current date and time]|| || ||import java.util.Date; _ _ long millis = System.currentTimeMillis(); _ Date dt = new Date(millis);||using System; _ _ DateTime dt = DateTime.Now;|| ||# unix-epoch[#unix-epoch-note to unix epoch, from unix epoch]|| || ||long epoch = dt.getTime() / 1000; _ _ Date dt2 = new Date(epoch * 1000);||using System; _ _ long tenM = 10 * 1000 * 1000; _ long sec = dt.ToFileTimeUtc() / tenM; _ long epoch = sec - 11644473600; _ _ long ft = (epoch + 11644473600) * tenM; _ DateTime dt2 = DateTime.FromFileTimeUtc(ft);|| ||# date-time-to-str[#date-time-to-str-note date and time to string]|| || ||dt.toString()||dt.ToString()|| ||# format-date[#format-date-note format date]|| || ||String s = “yyyy-MM-dd HH:mm:ss”; _ DateFormat fmt = new SimpleDateFormat(s); _ String s2 = fmt.format(dt);||String s = “yyyy-MM-dd HH:mm:ss”); _ String s2 = dt.ToString(s);|| ||# parse-date[#parse-date-note parse date]|| || ||String s = “2011-05-03 17:00:00”; _ Date dt2 = fmt.parse(s);||using System; _ using System.Globalization; _ _ CultureInfo enUS = _ @< >@new CultureInfo(“en-US”); _ _ DateTime dt2 = DateTime.ParseExact( _ @< >@"2011-05-03 17:00:00”, _ @< >@"yyyy-MM-dd HH:mm:ss”, _ @< >@enUS);|| ||# date-subtraction[#date-subtraction-note date subtraction]|| || ||##gray|//difference in milliseconds as a long://## _ dt2.getTime() - dt.getTime()|| || ||# add-duration[#add-duration-note add duration]|| || ||long day_ms = 24 * 3600 * 1000; _ Date dt = new Date(dt.getTime() + day_ms));|| || ||# date-parts[#date-parts-note date parts]|| || ||import java.util.Date; _ import java.util.Calendar; _ import java.util.GregorianCalendar; _ _ Date dt = new Date(); _ GregorianCalendar cal = new GregorianCalendar(); _ cal.setTime(dt); _ _ cal.get(Calendar.YEAR) _ cal.get(Calendar.MONTH) + 1 _ cal.get(Calendar.DAYOFMONTH)|| || ||# time-parts[#time-parts-note time parts]|| || ||import java.util.Date; _ import java.util.Calendar; _ import java.util.GregorianCalendar; _ _ Date dt = new Date(); _ GregorianCalendar cal = new GregorianCalendar(); _ cal.setTime(dt); _ _ cal.get(Calendar.HOUROFDAY) _ cal.get(Calendar.MINUTE) _ cal.get(Calendar.SECOND)|| || ||# build-datetime[#build-datetime-note build broken-down datetime]|| || ||import java.util.GregorianCalendar; _ _ int yr = 2015, mo = 5, dy = 31; _ int hr = 9, mi = 0, ss = 0; _ GregorianCalendar cal = _ @< >@new GregorianCalendar(yr, mo - 1, dy, hr, mi, ss); _ Date dt = cal.getTime();|| || ||||||||||~ # fixed-length-arrays[#fixed-length-arrays-note fixed-length arrays]|| ||~ ||~ c++||~ objective c||~ java||~ c#|| ||# fixed-len-array-stack[#fixed-len-array-stack-note declare on stack]||int a[10];||int a[10];||##gray|//arrays must be allocated on heap//##||##gray|//arrays must be allocated on heap//##|| ||# fixed-len-array-heap[#fixed-len-array-heap-note declare on heap]||int* a = new int[10];||#include _ int *a = calloc(10, sizeof(int));||int[] a = new int[10];||int[] a = new int[10];|| ||# free-fixed-len-array-heap[#free-fixed-len-array-heap-note free heap]||delete[] a;||#include _ free(a);||##gray|//garbage collected//##||##gray|//garbage collected//##|| ||# fixed-len-array-init-list[#fixed-len-array-init-list-note initialization list]||int a[] = {1, 2, 3};|| NSArray *a = [NSArray arrayWithObjects:@"hello”, @"goodbye”, nil];||int[] a = {1,2,3};||int[] a = {1,2,3};|| ||# fixed-len-array-size[#fixed-len-array-size-note size]||int a[10]; _ _ ##gray|@@//@@ stack arrays only:## _ size_t len = sizeof(a) / sizeof(a[0]);||[a count]||a.length||a.Length|| ||# fixed-len-array-lookup[#fixed-len-array-lookup-note lookup] _ @< >@||int first = a[0];||[a objectAtIndex:0]||a[0]||a[0]|| ||# fixed-len-array-update[#fixed-len-array-update-note update] _ @< >@||a[0] = 7;|| || || || ||# fixed-len-array-out-of-bounds[#fixed-len-array-out-of-bounds-note out-of-bounds]||##gray|//No defined behavior//## _ _ ##gray|//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// SIGSEGV.##||##gray|//raises//## NSRangeException exception||ArrayIndexOutOfBoundsException||IndexOutOfRangeException|| ||# copy-fixed-len-array[#copy-fixed-len-array-note copy]||const size_t LEN(4); _ int src[LEN] = {3, 2, 4, 1}; _ int dest[LEN]; _ _ ##gray|@@//@@ 3rd arg is number of bytes to copy:## _ memcpy(dest, src, LEN * sizeof(src[0]));|| || || || ||# fixed-len-array-as-func-arg[#fixed-len-array-as-func-arg-note as function argument]||void _ reverse(int* a, size_t len) { _ @< >@for (int i = 0; i < len / 2; ++i) { _ @< >@@< >@int tmp = a[len - i - 1]; _ @< >@@< >@a[len - i - 1] = a[i]; _ @< >@@< >@a[i] = tmp; _ @< >@} _ } _ _ const size_t LEN(4); _ int a[LEN] = {3, 2, 4, 1}; _ reverse(a, LEN);|| || || || ||# iterate-over-fixed-len-array[#iterate-over-fixed-len-array-note iterate]||const size_t LEN(4); _ int a[LEN] = {3, 2, 4, 1}; _ _ for (int i = 0; i < LEN; ++i) { _ @< >@cout @@<<@@ “value at “ @@<<@@ i @@<<@@ “ is “ _ @< >@@< >@@< >@@< >@@@<<@@ a[i] @@<<@@ endl; _ }||NSEnumerator i = [a objectEnumerator]; _ id o; _ while (o = [i nextObject]) { _ @< >@##gray|//do something with o//## _ }||for (String name : names) {||foreach (string name in names) {|| ||# sort-fixed-len-array[#sort-fixed-len-array-note sort]||#include _ _ int _ comp(const void ap, const void* bp) { _ @< >@int a = (int)ap; _ @< >@int b = (int)bp; _ @< >@return a < b ? -1 : (a == b ? 0 : 1); _ } _ _ const size_t LEN(4); _ int a[LEN] = {3, 2, 1, 4}; _ _ qsort(a, LEN, sizeof(a[0]), &comp);|| || || || ||||||||||~ # resizable-arrays[#resizable-arrays-note resizable arrays]|| ||~ ||~ c++||~ objective c||~ java||~ c#|| ||# decl-resizable-array[#decl-resizable-array-note declare]||#include _ _ vector a;||NSMutableArray *a = [NSMutableArray arrayWithCapacity:10];||java.util.Vector vec = new java.util.Vector();||using System.Collections.Generic; _ List l = new List();|| ||# resizable-array-init-list[#resizable-array-init-list-note initialization list]||#include _ _ vector a = {1, 2, 3}; _ vector a2({7, 8, 9});|| || || || ||# resizable-array-size[#resizable-array-size-note size] _ @< >@||size_t len = a.size();||[a count]||vec.size()||l.Count|| ||# resizable-array-capacity[#resizable-array-capacity-note capacity] _ ##gray|//get, increase//##||size_t cap = a.capacity(); _ _ ##gray|@@//@@ will not decrease capacity:## _ a.reserve(10);|| || || || ||# resizable-array-empty-test[#resizable-array-empty-test-note empty test] _ ##gray|//and clear//##||bool is_empty = a.empty(); _ a.clear();|| || || || ||# resizable-array-lookup[#resizable-array-lookup-note lookup]||int n = a[0]; _ _ ##gray|@@//@@ can raise outofrange:## _ int n2 = a.at(0);||[a objectAtIndex:0]||vec.elementAt(0)||l[0]|| ||# resizable-array-update[#resizable-array-update-note update] _ @< >@||a[2] = 4;|| || || || ||# resizable-array-out-of-bounds[#resizable-array-out-of-bounds-note out-of-bounds behavior]||##gray|//using [] with out-of-bounds index has undefined behavior//##||##gray|//raises//## NSRangeException||##gray|//throws//## ArrayIndexOutOfBoundsException||##gray|//throws//## System.ArgumentOutOfRangeException|| ||# resizable-array-elem-index[#resizable-array-elem-index-note element index]||#include _ _ vector a({6, 7, 8, 9}); _ _ auto iter = find(a.cbegin(), a.cend(), 8); _ if (iter != a.cend()) { _ @< >@size_t pos = *iter; _ }|| || || || ||# slice-resizable-array[#slice-resizable-array-note slice]||#include _ _ vector a({6, 7, 8, 9}); _ _ ##gray|@@//@@ a2 contains {7, 8}:## _ vector a2(a.cbegin() + 1, _ @< >@@< >@@< >@@< >@@< >@@< >@@< >@@< >@a.cbegin() + 3);|| || || || ||# slice-resizable-array-to-end[#slice-resizable-array-to-end-note slice to end]||#include _ _ vector a({6, 7, 8, 9}); _ _ ##gray|@@//@@ a2 contains {7, 8, 9}:## _ vector a2(a.cbegin() + 1, a.cend());|| || || || ||# resizable-array-back[#resizable-array-back-note manipulate back]||#include _ _ vector a({6, 7, 8}); _ _ a.push_back(9); _ int elem = a.pop_back();||[a addObject:@"hello”]; _ [a removeLastObject];||vec.add(“hello”); _ ##gray|//or//## _ vec.add(vec.size(), “hello”); _ vec.removeElementAt(vec.size()-1);||l.Add(“hello”); _ l.RemoveAt(l.Count - 1);|| ||# resizable-array-front[#resizable-array-front-note manipulate front]||#include _ _ vector a({6, 7, 8}); _ _ ##gray|@@//@@ slower than manipulating back:## _ a.insert(a.cbegin(), 5); _ int elem = a[0]; _ a.erase(a.cbegin());|| || || || ||# concat-resizable-array[#concat-resizable-array-note concatenate]||#include _ _ vector a1({1, 2, 3}); _ vector a2({4, 5, 6}); _ _ a1.insert(a1.cend(), _ @< >@@< >@@< >@@< >@@< >@a2.cbegin(), _ @< >@@< >@@< >@@< >@@< >@a2.cend());|| || || || ||# replicate-resizable-array-elem[#replicate-resizable-array-elem-note replicate element]||#include _ _ ##gray|@@//@@ array of 10 zeros:## _ vector a(10, 0);|| || || || ||# copy-resizable-array[#copy-resizable-array-note copy]||#include _ _ vector a({1, 2, 3}); _ ##gray|@@//@@ copy constructor:## _ vector a2(a); _ vector a3; _ _ ##gray|@@//@@ assignment performs copy:## _ a3 = a;|| || || || ||# resizable-array-as-func-arg[#resizable-array-as-func-arg-note array as function argument]||##gray|//use reference or pointer to avoid copying array//##|| || || || ||# iterate-over-resizable-array[#iterate-over-resizable-array-note iterate]||#include _ _ int sum(0); _ vector a({1, 2, 3}); _ _ for (const auto& n: a) { _ @< >@sum += n; _ }||NSEnumerator *i = [a objectEnumerator]; _ id o; _ while (o = [i nextObject]) { _ @< >@##gray|//do something with o//## _ }||for ( String s : vec ) { _ @< >@##gray|//do something with s//## _ }||foreach ( string s in l ) { _ @< >@##gray|//do something with s//## _ }|| ||# indexed-array-iteration[#indexed-array-iteration-note iterate over elements and indices]||#include _ _ vector a({6, 7, 8}); _ _ for (auto iter = a.cbegin(); _ @< >@@< >@@< >@iter != a.cend(); _ @< >@@< >@@< >@++iter) { _ _ @< >@cout @@<<@@ “value at “ @@<<@@ iter - a.cbegin() _ @< >@@< >@@< >@@< >@@@<<@@ “ is “ @@<<@@ *iter @@<<@@ endl; _ }|| || || || ||# reverse-array[#reverse-array-note reverse]||#include _ _ vector a({1, 2, 3}); _ vector a2(a.crbegin(), a.crend());|| || || || ||# sort-array[#sort-array-note sort]||#include _ _ vector a({3, 2, 4, 1}); _ sort(a.begin(), a.end());|| || || || ||# dedupe-array[#dedupe-array-note dedupe]||#include _ #include _ _ vector a({1, 1, 2, 2, 3}); _ set tmp(a.cbegin(), a.cend()); _ ##gray|@@//@@ often unnecessary since sets provide _ @@//@@ many of the same methods as vectors:## _ vector a2(tmp.cbegin(), tmp.cend());|| || || || ||# membership[#membership-note membership]||#include _ _ vector a({1, 2, 3}); _ if (find(a.cbegin(), a.cend(), 7) != _ @< >@@< >@a.cend()) { _ _ @< >@cout @@<<@@ “contains 7” @@<<@@ endl; _ }|| || || || ||||||||||~ # tuples[#tuples-note tuples]|| ||~ ||~ c++||~ objective c||~ java||~ c#|| ||# tuple-ctor[#tuple-ctor-note constructor]||tuple tup(“foo”, 1, 3.7); _ _ ##gray|@@//@@ invokes default constructors for elements:## _ tuple tup2; _ _ ##gray|@@//@@ element types are inferred:## _ auto tup3 = make_tuple(“foo”, 1, 3.7);|| || || || ||# tuple-lookup[#tuple-lookup-note lookup]||tuple tup(“foo”, 1, 3.7); _ _ string s = get(tup); _ int i = get(tup); _ float x = get(tup);|| || || || ||# tuple-decompose[#tuple-decompose-note decompose]||tuple tup(“foo”, 1, 3.7); _ string s; _ float x; _ _ tie(s, ignore, x) = tup;|| || || || ||# tuple-update[#tuple-update-note update] _ @< >@||get(tup) = “bar”;|| || || || ||# tuple-len[#tuple-len-note length] _ @< >@||tuple_size::value|| || || || ||# pair-ctor[#pair-ctor-note pair constructor]||pair p2(“foo”, 7); _ _ ##gray|@@//@@ invokes default constructors for elements:## _ pair p1; _ _ ##gray|@@//@@ element types are inferred:## _ auto p3 = make_pair(“foo”, 7);|| || ||using System.Collections.Generic; _ KeyValuePair pr = new KeyValuePair(“hello”,5); _ System.Console.WriteLine(“{0} {1}”, pr.Key, pr.Value);|| ||# pair-lookup[#pair-lookup-note pair lookup]||auto p = make_pair(“foo”, 7); _ _ string s = p.first; _ int i = p.second;|| || || || ||# pair-update[#pair-update-note pair update]||p.first = “bar”; _ p.second = 8;|| || || || ||||||||||~ # dictionaries[#dictionaries-note dictionaries]|| ||~ ||~ c++||~ objective c||~ java||~ c#|| ||# dict-ctor[#dict-ctor-note constructor]||#include _ _ map m;||NSMutableDictionary *dict = [NSMutableDictionary dictionaryWithCapacity:10];||java.util.TreeMap m = new java.util.TreeMap();||using System.Collections.Generic; _ Dictionary dict = new Dictionary();|| ||# dict-lookup[#dict-lookup-note lookup]||m[“hello”] = 5; _ cout @@<<@@ m[“hello”] @@<<@@ endl;||[dict setObject:@"5” forKey:@"hello”]; _ [dict objectForKey:@"hello”]||m.put(“hello”, 5); _ m.get(“hello”)||dict.Add(“hello”, 5); _ dict[“hello”]|| ||# dict-size[#dict-size-note size] _ @< >@||m.size()||[dict count]||m.size()||dict.Count|| ||# dict-delete[#dict-delete-note delete] _ @< >@||m.erase(m.find(“hello”));||[dict removeObjectForKey:@"hello”];||m.remove(“hello”);||dict.Remove(“hello”);|| ||# dict-missing-key[#dict-missing-key-note missing key behavior]||##gray|//returns element created by default constructor of value type//##||NULL||null||##gray|//throws//## KeyNotFoundException _ ##gray|//in// System.Collections.Generic##|| ||# dict-iter[#dict-iter-note iterate]||map::iterator mi; _ for (mi = m.begin(); mi != m.end(); ++mi) { _ @< >@printf(“%s %d”, mi->first, mi->second) _ }||NSEnumerator *i = [dict keyEnumerator]; _ id key; _ while ((key = [i nextObject])) { _ @< >@##gray|//do something with key//## _ }||for ( java.util.Map.Entry e : m.entrySet() ) { _ @< >@##gray|//use e.getKey() or e.getValue()//## _ }||foreach ( KeyValuePair e in dict) { _ @< >@##gray|//use e.Key and e.Value//## _ }|| ||||||||||~ # functions[#functions-note functions]|| ||~ ||~ c++||~ objective c||~ java||~ c#|| ||# decl-func[#decl-func-note declare]||##gray|@@//@@ parameter names are optional:## _ int _ add(int m, int n);|| || || || ||# def-func[#def-func-note define]||int _ add(int m, int n) { _ @< >@return m + n; _ }|| || || || ||# call-func[#call-func-note call] _ @< >@||int sum = add(3, 7);|| || || || ||# def-static-class-method[#def-static-class-method-note define static class method]||##gray|@@//@@ Ops.h:## _ class Ops { _ public: _ @< >@static int add(int m, int n); _ }; _ _ ##gray|@@//@@ Ops.cpp:## _ int Ops::add(int m, int n) { _ @< >@return m + n; _ }|| || || || ||# invoke-static-class-method[#invoke-static-class-method-note invoke static class method]||int sum = Ops::add(3, 7); _ _ ##gray|@@//@@ class name not needed _ @@//@@ inside class namespace:## _ int sum = add(3, 7);|| || || || ||# overload-func[#overload-func-note overload]||int add(int m, int n) { _ @< >@return m + n; _ } _ _ float add(float x, float y) { _ @< >@return x + y; _ }||##gray|//method overloading only//##||##gray|//yes//##||##gray|//yes//##|| ||# default-arg[#default-arg-note default argument]||#include _ _ float _ logarithm(float x, float base = 10.0) { _ @< >@return log(x) / log(base); _ }||##gray|//none//##||##gray|//use method overloading//##||##gray|//use method overloading//##|| ||# variable-num-arg[#variable-num-arg-note variable number of arguments]|| ||##gray|//use C; use method overloading for finite arities//##||public static String concat(String first, String… rest) { _ @< >@StringBuilder sb = new StringBuilder(first); _ @< >@for (String arg: rest) { _ @< >@@< >@sb.append(arg); _ @< >@} _ @< >@return sb.toString(); _ } _ String s = Concat.concat(“Hello”, “, “, “World”, “!”);||public static string concat(params string[] args) { _ @< >@return System.String.Join(““,args); _ } _ string s = Concat.concat(“Hello”, “, “, “World”, “!”)|| ||# named-param[#named-param-note named parameters]||##gray|//none//##||+(float)weight: (float) w height: (float) h { _ @< >@return (w * 703) / (h * h); _ } _ +(float)height: (float) h weight: (float) w { _ @< >@return [BMI weight: w height: h]; _ } _ [BMI weight:155 height:70]; _ [BMI height:70 weight:155];||##gray|//none//##||##gray|//added in C# 4.0://## _ static int BMI(int weight, int height) { _ @< >@return (weight * 703) / (height * height); _ } _ BMI(weight: 123, height: 64); _ BMI(height: 64, weight: 123);|| ||# pass-by-val[#pass-by-val-note pass by value]||int add1(int n) { _ @< >@return ++n; _ } _ _ int i(7); _ _ ##gray|@@//@@ set i2 to 8 w/o modifying i:## _ int i2 = add1(i);||void use_integer(int i) { _ @< >@##gray|//function body//## _ } _ int i = 7; _ use_integer(i);||##gray|//primitive types are always passed by value//##||##gray|//primitive types are always passed by value//##|| ||# pass-by-ref[#pass-by-ref-note pass by reference]||int add1(int& n) { _ @< >@return ++n; _ } _ _ int i(7); _ _ ##gray|@@//@@ set i and i2 to 8:## _ int i2 = add1(i);||##gray|//none//##||##gray|//objects and arrays are always passed by reference//##||##gray|//objects and arrays are always passed by reference//## _ _ ##gray|//also out parameter//##|| ||# pass-by-addr[#pass-by-addr-note pass by address]||int add1(int* n) { _ @< >@return ++*n; _ } _ _ int i(7); _ _ ##gray|@@//@@ set i and i2 to 8:## _ int i2 = add1(&i);||void use_iptr(int *i) { _ @< >@##gray|//function body//## _ } _ int i = 7; _ use_iptr(&i);||##gray|//none//##||##gray|//none//##|| ||# retval[#retval-note return value] _ @< >@||##gray|//argument of// return; //type must be declared//##|| || || || ||# no-retval[#no-retval-note no return value]||void _ message(const string& msg) { _ @< >@cout @@<<@@ msg @@<<@@ endl; _ }|| || || || ||# recursive-func[#recursive-func-note recursive function]||int _ factorial(int n) { _ @< >@if (n <= 1) { _ @< >@@< >@return 1; _ @< >@} _ @< >@return n * factorial(n - 1); _ }|| || || || ||# anon-func-literal[#anon-func-literal-note anonymous function]||auto add = { _ @< >@return n + m; _ };|| || || || ||# invoke-anonymous-func[#invoke-anonymous-func-note invoke anonymous function]||##gray|@@//@@on variable holding anon. function:## _ int sum = add(3, 7); _ _ ##gray|@@//@@ on lambda expression:## _ int sum2 = { _ @< >@return n + m; _ }(3, 7);|| || || || ||# closure[#closure-note closure]|| || || || || ||# func-private-state[#func-private-state-note function with private state]||int _ counter() { _ @< >@static int i = 0; _ @< >@return ++i; _ }|| || || || ||# func-as-val[#func-as-val-note function as value]|| || || || || ||# overload-op[#overload-op-note overload operator]||Rational Rational::operator+(Rational& o) { _ @< >@return Rational(this->num * o.denom + o.num * this->denom, this->denom * o.denom); _ }||##gray|//none//##||##gray|//none//##||public static Rational operator+(Rational a, Rational b) { _ @< >@return new Rational(a.num*b.denom + b.num a.denom,a.denomb.denom); _ }|| ||||||||||~ # execution-control[#execution-control-note execution control]|| ||~ ||~ c++||~ objective c||~ java||~ c#|| ||# if[#if-note if]||int signum; _ _ if (n > 0) { _ @< >@signum = 1; _ } _ else if (n == 0) { _ @< >@signum = 0; _ } _ else { _ @< >@signum = -1; _ }||if (i>0) { _ @< >@signum = 1; _ } else if (i==0) { _ @< >@signum = 0; _ } else { _ @< >@signum = -1; _ }||if (i>0) { _ @< >@signum = 1; _ } else if (i==0) { _ @< >@signum = 0; _ } else { _ @< >@signum = -1; _ }||if (i>0) { _ @< >@signum = 1; _ } else if (i==0) { _ @< >@signum = 0; _ } else { _ @< >@signum = -1; _ }|| ||# dangling-else[#dangling-else-note dangling else]||if (n == 0) _ @< >@if (m == 0) _ @< >@@< >@cout @@<<@@ “n and m are zero” @@<<@@ endl; _ @< >@else _ @< >@@< >@cout @@<<@@ “n is zero; m isn’t” @@<<@@ endl;|| || || || ||# switch[#switch-note switch]||const int INVALIDBINARYDIGIT(-1); _ int bin_digit; _ _ switch(n) { _ case 0: _ case 1: _ @< >@bin_digit = n; _ @< >@break; _ default: _ @< >@bindigit = INVALIDBINARY_DIGIT; _ @< >@break; _ }||switch(i) { _ case 0: _ @< >@0; _ @< >@break; _ case 1: _ @< >@1; _ @< >@break; _ default: _ @< >@-1; _ @< >@break; _ }||switch(i) { _ case 0: _ @< >@0; _ @< >@break; _ case 1: _ @< >@1; _ @< >@break; _ default: _ @< >@-1; _ @< >@break; _ }||switch(i) { _ case 0: _ @< >@0; _ @< >@break; _ case 1: _ @< >@1; _ @< >@break; _ default: _ @< >@-1; _ @< >@break; _ }|| ||# while[#while-note while]||int i(1), fact(1), n(10); _ _ while (i < n) { _ @< >@fact *= i; _ @< >@++i; _ }||int i = 0; _ while (i@i++; _ }||int i = 0; _ while (i@i++; _ }||int i = 0; _ while (i@i++; _ }|| ||# for[#for-note for]||int fact, n(10); _ _ for (int i = 1, fact = 1; i <= n; ++i) { _ @< >@fact *= i; _ }||int i, n; _ for (i=1,n=1; i<=10; i++) { _ @< >@n *= i; _ }||int n = 1; _ for (int i=1; i<=10; i++) { _ @< >@n *= i; _ }||int i, n; _ for (i=1,n=1; i<=10; i++) { _ @< >@n *= i; _ }|| ||# break[#break-note break]||int data[4] = {3, 2, 0, 1}; _ int i; _ bool has_zero(false); _ _ for (i = 0; i < 4; ++i) { _ @< >@if (data[i] == 0) { _ @< >@@< >@has_zero = true; _ @< >@@< >@break; _ @< >@} _ }|| || || || ||# break-nested-loops[#break-nested-loops-note break out of nested loops]||int data[2][2] = @@{{@@3, 2}, {0, 1@@}}@@; _ int i, j; _ bool has_zero(false); _ _ for (i = 0; i < 2; ++i) { _ @< >@for (j = 0; j < 2; ++j) { _ @< >@@< >@if (data[i][j] == 0) { _ @< >@@< >@@< >@has_zero = true; _ @< >@@< >@@< >@goto endofloops; _ @< >@@< >@} _ @< >@} _ } _ :endofloops|| || || || ||# continue[#continue-note continue]||int a[4] = {3, 2, 0, 1}; _ _ for (int i = 0; i < 4; ++i) { _ @< >@if (a[i] == 0) { _ @< >@@< >@continue; _ @< >@} _ @< >@cout @@<<@@ 1.0 / a[i] @@<<@@ endl; _ }|| || || || ||# goto[#goto-note goto]|| || || || || ||||||||||~ # exceptions[#exceptions-note exceptions]|| ||~ ||~ c++||~ objective c||~ java||~ c#|| ||# base-exc[#base-exc-note base exception]||##gray|//Any type can be thrown. _ _ All exceptions thrown by the language or the standard library derive from// exception, //defined in// .##|| ||##gray|//Any type which implements the interface// java.lang.Throwable //can be thrown. _ _ Exceptions thrown by the language and the standard libraries derive from// java.lang.Errror //or// java.lang.Exception.##|| || ||# predefined-exc[#predefined-exc-note predefined exceptions]||#include _ #include _ #include _ #include _ _ exception _ @< >@logic_error _ @< >@@< >@domain_error _ @< >@@< >@invalid_argument _ @< >@@< >@length_error _ @< >@@< >@outofrange _ @< >@runtime_error _ @< >@@< >@system_error _ @< >@@< >@@< >@ios_base::failure _ @< >@bad_cast _ @< >@bad_exception _ @< >@bad_alloc|| ||##gray|//java.lang.Throwable//## _ @< >@java.lang.Error _ @< >@java.lang.Exception _ @< >@@< >@java.lang.IOException _ @< >@@< >@java.lang.RuntimeException _ @< >@@< >@@< >@java.lang.ArithmeticException _ @< >@@< >@@< >@java.lang.IllegalArgumentException _ @< >@@< >@@< >@java.lang.IndexOutOfBoundsException _ @< >@@< >@@< >@java.lang.NullPointerException|| || ||# raise-exc[#raise-exc-note raise exception]||#include _ #include _ _ void risky() { _ @< >@if (rand() < 10) { _ @< >@@< >@throw runtime_error(“bam!”); _ @< >@} _ }||NSException *exc = [NSException exceptionWithName:@"error” reason:@"failed” userInfo:nil]; _ @throw exc;||throw new Exception(“failed”);||throw new System.Exception(“failed”);|| ||# handle-exc[#handle-exc-note handle exception]||#include _ _ try { _ @< >@risky(); _ } _ catch (const exception &e) { _ @< >@cout @@<<@@ e.what() @@<<@@ endl; _ }||@try { _ @< >@[NSException raise:@"error” format:@"failed”]; _ } @catch (NSException *e) { _ @< >@printf([[e reason] UTF8String]); _ }||try { _ @< >@throw new Exception(“failed”); _ } _ catch (Exception e) { _ @< >@System.out.println(e.getMessage()); _ }||try { _ @< >@throw new System.Exception(“failed”); _ } catch (System.Exception e) { _ @< >@System.Console.WriteLine(e.Message); _ }|| ||# def-exc[#def-exc-note define exception]||#include _ _ class Bam : public runtime_error { _ public: _ @< >@Bam() : runtime_error(“bam!”) {} _ }; _ _ throw Bam();|| || || || ||# re-raise-exc[#re-raise-exc-note re-raise exception]||#include _ _ try { _ @< >@risky(); _ } _ catch (const exception& e) { _ @< >@cout @@<<@@ “an error occurred@@…@@” @@<<@@ endl; _ @< >@throw; _ }|| || || || ||# catch-all-handler[#catch-all-handler-note catch-all handler]||#include _ _ try { _ @< >@risky(); _ } _ catch (@@…@@) { _ @< >@cout @@<<@@ “an error was ignored” _ @< >@@< >@@< >@@< >@@@<<@@ endl; _ }|| || || || ||# multiple-handlers[#multiple-handlers-note multiple handlers]||#include _ _ try { _ @< >@risky(); _ } _ catch (const system_error &e) { _ @< >@cout @@<<@@ “system error: “ @@<<@@ e.name() _ @< >@@< >@@< >@@< >@@@<<@@ endl; _ } _ catch (const exception &e) { _ @< >@cout @@<@@< “exception: “ @@<<@@ e.what() _ @< >@@< >@@< >@@< >@@@<<@@ endl; _ } _ catch (@@…@@) { _ @< >@cout @@<<@@ “unknown error” @@<<@@ endl; _ }|| || || || ||# uncaught-exc[#uncaught-exc-note uncaught exception behavior]||##gray|//calls// terminate() //which by default calls// abort()##|| || || || ||# error-msg[#error-msg-note error message]||#include _ _ try { _ @< >@risky(); _ } _ catch (const exception &e) { _ @< >@const chae *msg = e.what(); _ }|| || || || ||# errno[#errno-note system call errno]||#include _ _ try { _ @< >@risky(); _ } _ catch (const system_error &e { _ @< >@int errcodeval = e.code().value(); _ }|| || || || ||# finally-clause[#finally-clause-note finally clause]||##gray|//none//##||@try { _ @< >@##gray|//risky code//## _ } @finally { _ @< >@##gray|//perform cleanup//## _ }||try { _ @< >@##gray|//risky code//## _ } finally { _ @< >@##gray|//perform cleanup//## _ }||try { _ @< >@##gray|//risky code//## _ } finally { _ @< >@##gray|//perform cleanup//## _ }|| ||# exc-specification[#exc-specification-note exception specification]||##gray|@@//@@ Use noexcept to declare that a function _ @@//@@ does not raise exceptions; _ @@//@@ declaring which exceptions a function _ @@//@@ raises is deprecated in C++11.## _ int _ add(int a, int b) noexcept { _ @< >@return a + b; _ }||##gray|//no//##||##gray|//yes//##||##gray|//no//##|| ||||||||||~ # concurrency[#concurrency-note concurrency]|| ||~ ||~ c++||~ objective c||~ java||~ c#|| ||# start-thread[#start-thread-note start thread] _ @< >@|| || || || || ||# terminate-current-thread[#terminate-current-thread-note terminate current thread]|| || || || || ||# terminate-other-thread[#terminate-other-thread-note terminate other thread]|| || || || || ||# list-threads[#list-threads-note list threads]|| || || || || ||# wait-on-thread[#wait-on-thread-note wait on thread] _ @< >@|| || || || || ||# lock[#lock-note lock]|| || || || || ||# create-msg-queue[#create-msg-queue-note create message queue]|| || || || || ||# send-msg[#send-msg-note send message]|| || || || || ||# receive-msg[#receive-msg-note receive message]|| || || || || ||||||||||~ # file-handles[#file-handles-note file handles]|| ||~ ||~ c++||~ objective c||~ java||~ c#|| ||# std-file-handles[#std-file-handles-note standard file handles]||cin _ cout _ cerr _ clog ##gray|@@//@@ buffered cerr by default##|| ||System.in _ System.out _ System.err|| || ||# read-line-stdin[#read-line-stdin-note read line from stdin]||string s; _ _ cin @@>>@@ s;|| || || || ||# printf[#printf-note write formatted string to stdout] _ @< >@||cout @@<<@@ “count: “ @@<<@@ 7 @@<<@@ endl;||printf(“count: %d\n”, 7);||System.out.printf(“count: %d”, 7);||System.Console.WriteLine(“count: {0}”, 7);|| ||[#read-file read from file]||#include _ _ string line; _ ifstream f(“/etc/passwd”); _ _ if (f.is_open()) { _ @< >@while (!f.eof()) { _ @< >@@< >@getline(f, line); _ @< >@@< >@##gray|@@//@@ process line## _ @< >@} _ @< >@f.close(); _ @< >@if ( 0 != f.fail() ) { _ @< >@@< >@##gray|@@//@@ handle error## _ @< >@} _ } _ else { _ @< >@##gray|@@//@@ handle error## _ }||NSError *error = nil; _ NSString *s = [NSString stringWithContentsOfFile: @”/etc/passwd” encoding:NSUTF8StringEncoding error:&error]; _ _ if ( error != nil ) { _ @< >@##gray|@@//@@ handle error## _ } _ _ NSArray a = [s componentsSeparatedByString:@”\n”]; _ id line; _ _ while (line = [i nextObject]) { _ @< >@##gray|@@//@@ process line## _ }||import java.io.BufferedReader; _ import java.io.FileReader; _ _ BufferedReader in = new BufferedReader(new FileReader(“/etc/passwd”)); _ String line; _ _ while ((line = in.readLine()) != null) { _ @< >@##gray|@@//@@ process line## _ }||using System.IO; _ _ StreamReader sr = new StreamReader(“/etc/passwd”); _ string line; _ _ while ((line = sr.ReadLine()) != null) { _ @< >@##gray|@@//@@ process line## _ }|| ||[#write-file write to file]||#include _ _ ofstream f(“/tmp/test4”); _ int i; _ _ for (i = 0; i < 10; ++i) { _ @< >@f @@<<@@ i @@<<@@ endl; _ } _ f.close(); _ if (0 != f.fail()) { _ @< >@##gray|@@//@@ handle error## _ }|| ||import java.io.BufferedWriter; _ import java.io.FileWriter; _ _ BufferedWriter fout = new BufferedWriter(new FileWriter(“/tmp/test2”)); _ int i; _ _ for (i = 0; i < 10; i++) { _ @< >@fout.write(String.format(“%d”, i)); _ @< >@fout.newLine(); _ } _ fout.close();||using System.IO; _ _ StreamWriter fout = new StreamWriter(“/tmp/test3”); _ int i; _ _ for (i = 0; i < 10; i++) { _ @< >@fout.WriteLine(i.ToString()); _ } _ fout.Close();|| ||||||||||~ # files[#files-note files]|| ||~ ||~ c++||~ objective c||~ java||~ c#|| ||# file-test[#file-test-note file exists test, file regular test]|| || ||import java.io.File; _ _ File f = new File(“/etc/hosts”); _ f.exists() _ f.isFile()||System.IO.File.Exists(“/etc/hosts”)|| ||# file-size[#file-size-note file size]|| || ||import java.io.File; _ _ File f = new File(“/etc/hosts”); _ f.length()|| || ||# readable-writable-executable[#readable-writable-executable is file readable, writable, executable]|| || ||import java.io.File; _ _ File f = new File(“/etc/hosts”); _ _ f.canRead() _ f.canWrite() _ f.canExecute()|| || ||# chmod[#chmod-note set file permissions]|| || ||import java.io.File; _ _ File f = new File(“/tmp/foo”); _ _ ##gray|@@//@@ sets owner perms; to turn perms off _ @@//@@ set arg to false:## _ f.setReadable(true); _ f.setWritable(true); _ f.setExecutable(true); _ _ ##gray|@@//@@ if 2nd arg is false, perms are _ @@//@@ for owner, group, and other:## _ f.setReadable(true, false); _ f.setWritable(true, false); _ f.setExecutable(true, false);|| || ||# file-cp-rm-mv[#file-cp-rm-mv-note copy file, remove file, rename file]|| || ||import java.io.File; _ _ ##gray|//??//## _ _ File f2 = new File(“/tmp/foo”); _ f2.delete(); _ _ File f3 = new File(“/tmp/bar”); _ f3.renameTo(new File(“/tmp/bar”));|| || ||||||||||~ # file-fmt[#file-fmt-note file formats]|| ||~ ||~ c++||~ objective c||~ java||~ c#|| ||csv|| || || || || ||json|| || || || || ||build xml|| || || || || ||parse xml|| || || || || ||parse html|| || || || || ||||||||||~ # directories[#directories-note directories]|| ||~ ||~ c++||~ objective c||~ java||~ c#|| ||# build-pathname[#build-pathname-note build pathname]|| || ||import java.io.File; _ _ File root = File.listRoots()[0]; _ File etc = new File(root, “etc”); _ File hosts = new File(etc, “hosts”); _ String path = hosts.getPath();|| || ||# dirname-basename[#dirname-basename-note dirname and basename]||#include _ _ string s1 = dirname(“/etc/hosts”); _ string s2 = basename(“/etc/hosts”);|| ||import java.io.File; _ _ File f = new File(“/etc/hosts”); _ String dirname = f.getParent(); _ String basename = f.getName();|| || ||# absolute-pathname[#absolute-pathname-note absolute pathname]||#include _ #include _ _ char buf[PATH_MAX]; _ if (realpath(“..”, buf) == NULL) { _ @< >@throw exception(); _ } _ else { _ @< >@string path(buf); _ }|| ||import java.io.File; _ _ File f = new File(“foo”); _ String abspath = f.getAbsolutePath(); _ _ ##gray|@@//@@ getCanonicalPath() expands .. and .:## _ File f2 = new File(“../foo”); _ String abspath2 = f2.getCanonicalPath(); _ File f3 = new File(“./foo”); _ String abspath3 = f3.getCanonicalPath();|| || ||# iterate-dir[#dir-iterate-note iterate over directory by file]|| || ||import java.io.File; _ _ File dir = new File(“/etc”); _ _ ##gray|@@//@@ iterate over names:## _ for (String name: dir.list()) { _ @< >@System.out.println(name); _ } _ _ ##gray|@@//@@ iterate over file objects:## _ for (File f: dir.listFiles()) { _ @< >@System.out.println(f.getName()); _ }|| || ||# glob[#glob-note glob paths]|| || || || || ||# mkdir[#mkdir-note make directory]|| || ||import java.io.File; _ _ File f = new File(“/tmp/foo/bar”); _ f.mkdirs();|| || ||# recursive-cp[#recursive-cp-note recursive copy]|| || || || || ||# rmdir[#rmdir-note remove empty directory]|| || || || || ||# rm-rf[#rm-rf-note remove directory and contents]|| || || || || ||# dir-test[#dir-test-note directory test] _ @< >@|| || ||import java.io.File; _ _ File f = new File(“/tmp”); _ f.isDirectory()|| || ||# unused-dir[#unused-dir-note generate unused directory name]|| || || || || ||# system-tmp-dir[#system-tmp-dir-note system temporary file directory]|| || || || || ||||||||||~ # processes-environment[#processes-environment-note processes and environment]|| ||~ ||~ c++||~ objective c||~ java||~ c#|| ||[#main signature of main]||int main(int argc, char@@@@ argv) {||int main(int argc, char @@@@argv) {||public class //Foo// { _ @< >@public static void main(String[] args) {||public class //Foo// { _ @< >@public static void Main(string[] args) {|| ||[#first-argument first argument] _ @< >@||##gray|//pathname of executable//##||##gray|//pathname of executable//##||##gray|//first command line argument//##||##gray|//first command line argument//##|| ||[#environment-variable environment variable]||#include _ _ char home = getenv(“HOME”); _ setenv(“EDITOR”, “emacs”, 1); _ unsetenv(“EDITOR”);||NSString *home = [[[NSProcessInfo processInfo] environment] objectForKey:@"HOME”];||String home = System.getenv(“HOME”);||using System.Environment; _ string home = GetEnvironmentVariable(“HOME”); _ SetEnvironmentVariable(“EDITOR”, “emacs”); _ SetEnvironmentVariable(“EDITOR”, null);|| ||[#iterate-environment-variable iterate through environment variables]|| ||NSEnumerator i = [[[NSProcessInfo processInfo] environment] keyEnumerator]; _ id key; _ while ((key = [i nextObject])) { _ @< >@##gray|//use NSString key//## _ }||import java.util.Map; _ Map env = System.getenv(); _ for (String name : env.keySet()) { _ @< >@String value = env.get(name)); _ }||using System.Collections; _ using System.Environment; _ IDictionary env = GetEnvironmentVariables(); _ foreach (DictionaryEntry de in env) { _ @< >@##gray|//use de.Key or de.Value//## _ }|| ||||||||||~ # libraries-namespaces[#libraries-namespaces-note libraries and namespaces]|| ||~ ||~ c++||~ objective c||~ java||~ c#|| ||# std-lib-name[#std-lib-name-note standard library name]||##gray|//C++ Standard Library//##||##gray|//Foundation Framework//##||##gray|//Java API//##||##gray|//Base Class Library//##|| ||[#declare-namespace declare namespace]||namespace foo { _ @< >@namespace bar { _ @< >@@< >@class Baz { _ @< >@@< >@@< >@static const int ANSWER = 42; _ @< >@@< >@}; _ @< >@} _ }|| ||package foo.bar; _ public class Baz { _ @< >@public static final int ANSWER = 42; _ }||namespace foo { _ @< >@namespace bar { _ @< >@@< >@public class Baz { _ @< >@@< >@@< >@public const int ANSWER = 42; _ @< >@@< >@}; _ @< >@} _ }|| ||[#namespaces-per-file multiple namespaces per file]||##gray|//yes//##|| ||##gray|//no//##||##gray|//yes//##|| ||[#namespace-directory-mapping namespaces map to directories]||##gray|//no//##|| ||##gray|//yes//##||##gray|//no//##|| ||[#import-namespace import namespace]||using namespace foo::bar; _ _ cout @@<<@@ Baz::ANSWER @@<<@@ endl;|| ||import foo.bar.; _ System.out.println(Baz.ANSWER);||using foo.bar; _ System.Console.WriteLine(Baz.ANSWER);|| ||[#import-part-namespace import part of namespace]||using namespace foo; _ _ cout @@<<@@ bar::Baz::ANSWER @@<<@@ endl;|| ||##gray|//none//##||##gray|//none//##|| ||[#import-symbol import symbol]||using foo::bar::Baz; _ cout @@<<@@ Baz::ANSWER @@<<@@ endl;|| ||import foo.bar.Baz; _ System.out.println(Baz.ANSWER);||##gray|//none//##|| ||[#import-static-symbol import static symbol]||##gray|//none//##|| ||import static foo.bar.Baz.ANSWER; _ System.out.println(ANSWER);||##gray|//none//##|| ||[#import-position import position] _ @< >@||##gray|//anywhere a statement is legal//##|| ||##gray|//after package and before type definitions//##||##gray|//outside of class definitions//##|| ||[#not-imported-symbol using a symbol that hasn’t been imported]||cout @@<<@@ foo::bar::Baz::ANSWER @@<<@@ endl;|| ||System.out.println(foo.bar.Baz.ANSWER);||using System.Console; _ WriteLine(foo.bar.Baz.ANSWER);|| ||# app-env[#app-env-note application environment]|| || || || || ||# multiple-installations[#multiple-installations-note multiple installations]|| || ||##gray|//set// JAVAHOME //environment variable to directory containing a// bin //subdirectory with// java, javac, //and other command line tools. Put// $JAVAHOME/bin //at front of search path.//##|| || ||# pkg-manager[#pkg-manager-note package manager]|| || || || || ||||||||||~ # user-defined-types[#user-defined-types-note user-defined types]|| ||~ ||~ c++||~ objective c||~ java||~ c#|| ||# typedef[#typedef-note typedef]||typedef int customer_id; _ customerid cid = 3;||typedef int customerid; _ customerid cid = 3;||##gray|//none//##||##gray|//none//##|| ||# enum[#enum-note enum]||enum dayof_week { mon, tue, wed, thu, fri, sat, sun }; _ dayofweek d = tue;||enum dayofweek { mon, tue, wed, thu, fri, sat, sun }; _ enum dayofweek d = tue;||public enum DayOfWeek { MON, TUE, WED, THU, FRI, SAT, SUN }; _ DayOfWeek d = DayOfWeek.TUE;||public enum DayOfWeek { MON, TUE, WED, THU, FRI, SAT, SUN }; _ DayOfWeek d = DayOfWeek.TUE;|| ||[#struct-definition struct definition]||class MedalCount { _ public: _ @< >@const char country; _ @< >@int gold; _ @< >@int silver; _ @< >@int bronze; _ };||struct medal_count { _ @< >@const char country; _ @< >@int gold; _ @< >@int silver; _ @< >@int bronze; _ };||public class MedalCount { _ @< >@public String country; _ @< >@public int gold; _ @< >@public int silver; _ @< >@public int bronze; _ }||public class MedalCount { _ @< >@public string country; _ @< >@public int gold; _ @< >@public int silver; _ @< >@public int bronze; _ }|| ||[#struct-declaration struct declaration]||MedalCount spain;||struct medalcount spain;||MedalCount spain = new MedalCount();||MedalCount spain = new MedalCount();|| ||[#struct-initialization struct initialization]||MedalCount spain = { “Spain”, 3, 7, 4 };||struct medalcount spain = { “Spain”, 3, 7, 4}; _ struct medal_count france = { .gold = 8, .silver = 7, .bronze = 9, .country = “France” };||##gray|//no object literal syntax; define a constructor//##||##gray|//no object literal syntax; define a constructor//##|| ||[#struct-member-assignment struct member assignment]||spain.country = “Spain”; _ spain.gold = 3; _ spain.silver = 7; _ spain.bronze = 4;||spain.country = “Spain”; _ spain.gold = 3; _ spain.silver = 7; _ spain.bronze = 4;||spain.country = “Spain”; _ spain.gold = 3; _ spain.silver = 7; _ spain.bronze = 4;||spain.country = “Spain”; _ spain.gold = 3; _ spain.silver = 7; _ spain.bronze = 4;|| ||[#struct-member-access struct member access]||int spaintotal = spain.gold + spain.silver + spain.bronze;||int spaintotal = spain.gold + spain.silver + spain.bronze;||int spaintotal = spain.gold + spain.silver + spain.bronze;||int spaintotal = spain.gold + spain.silver + spain.bronze;|| ||||||||||~ # generic-types[#generic-types-note generic types]|| ||~ ||~ c++||~ objective c||~ java||~ c#|| ||[#define-generic define generic type]||template _ class Foo { _ public: _ @< >@A a; _ @< >@Foo(A a); _ }; _ @< >@ _ template _ Foo::Foo(A a) : a(a) { _ }|| ||public class Foo { _ @< >@public A a; _ @< >@public Foo(A a) { _ @< >@@< >@this.a = a; _ @< >@} _ }||public class Foo { _ @< >@public A a; _ @< >@public Foo(A a) { _ @< >@@< >@this.a = a; _ @< >@} _ }|| ||[#instantiate-generic instantiate generic type]||Foo f = Foo(“foo”);|| ||Foo f = new Foo(“foo”);||Foo f = new Foo(“foo”);|| ||[#generic-function generic function]||template _ C add(C a, C b) { _ @< >@return a + b; _ }|| || || || ||[#generic-array generic array]||template _ class Foo { _ public: _ @< >@C a[10]; _ };|| ||##gray|//not permitted. Use// Object //as the element type for the array or use an// ArrayList.##||public class Bar { _ @< >@public C[] a; _ @< >@public Bar(C c) { _ @< >@@< >@this.a = new C[10]; _ @< >@} _ }|| ||[#value-parameter value parameter]||template _ int add(int i) { _ @< >@return N+i; _ } _ @< >@ _ cout @@<<@@ add(3) @@<<@@ endl;|| || || || ||[#template-parameter template parameter]|| || || || || ||[#template-specialization template specialization]|| || || || || ||[#multiple-type-parameters multiple type parameters]||template _ class Pair { _ public: _ @< >@A a; _ @< >@B b; _ @< >@Pair(A a, B b); _ }; _ @< >@ _ template _ Pair::Pair(A a, B b) : _ @< >@a(a), b(b) { } _ @< >@ _ Pair p = _ @< >@Pair(7, “foo”);|| || || || ||[#generic-type-parameters generic type parameters]||Pair > p = _ @< >@Pair >( _ @< >@@< >@7, Foo(“foo”));|| || || || ||[#template-parameters template parameters]|| || || || || ||variadic template|| || || || || ||||||||||~ # objects[#objects-note objects]|| ||~ ||~ c++||~ objective c||~ java||~ c#|| ||# str-equal[#str-equal-note semantics of ==]||##gray|//value comparison//##||##gray|//object identity comparison//##||##gray|//object identity comparison//##||##gray|//value comparison//##|| ||[#define-class define class]||##gray|//Rational.hpp://## _ class Rational { _ @< >@public: _ @< >@int num, denom; _ @< >@Rational(int num, int denom); _ @< >@virtual ~Rational(); _ @< >@Rational operator+(Rational& addend); _ @< >@static Rational max(Rational& a, Rational& b); _ };||##gray|//Rational.h://## _ #import _ @interface Rational : NSObject { _ @< >@int num; _ @< >@int denom; _ } _ @property int num, denom; _ -(Rational) initWith: (int) n: (int) d; _ -(Rational) add: (Rational ) o; _ @end _ ##gray|//Rational.m://## _ #include “Rational.h” _ @implementation Rational _ @synthesize num, denom; _ -(Rational) add: (Rational*) o { _ @< >@int sum_n = self.num * o.denom + o.num * self.denom; _ @< >@int sum_d = self.denom * o.denom; _ @< >@Rational* sum = [[Rational alloc] initWith: sumn: sumd]; _ @< >@return sum; _ } _ @end||public class Rational { _ @< >@public int num; _ @< >@public int denom; _ @< >@public Rational add(Rational o) throws Exception { _ @< >@@< >@return new Rational(this.numo.denom + o.numthis.denom,this.denomo.denom); _ @< >@} _ @< >@public static Rational max(Rational a, Rational b) { _ @< >@@< >@return (a.numb.denom > a.numb.denom) ? a : b; _ @< >@} _ }||public class Rational { _ @< >@public int num; _ @< >@public int denom; _ }|| ||[#class-definition-location class definition location]||##gray|//top level, class block, or function block//##||##gray|//top level//##||##gray|//top level, class block, or function block for anonymous classes//##|| || ||[#constructor constructor]||Rational::Rational(int n, int d) : num(n), denom(d) { _ @< >@if (denom == 0) { _ @< >@@< >@throw “zero denominator”; _ @< >@} _ @< >@int div = gcd(n,d); _ @< >@num = num / div; _ @< >@denom = denom / div; _ }||-(Rational) initWith: (int) n: (int) d { _ @< >@self = [super init]; _ @< >@if (self) { _ @< >@@< >@self.num = n; _ @< >@@< >@self.denom = d; _ @< >@} _ @< >@return self; _ }||public Rational(int n, int d) throws Exception { _ @< >@if (d == 0) { _ @< >@@< >@throw new Exception(“zero denominator”); _ @< >@} _ @< >@if ( d < 0 ) { _ @< >@@< >@this.num = -1 * n; _ @< >@@< >@this.denom = -1 * d; _ @< >@} _ @< >@else { _ @< >@@< >@this.num = n; _ @< >@@< >@this.denom = d; _ @< >@} _ }||public Rational(int n, int d) { _ @< >@if (0 == d) { _ @< >@@< >@throw new System.Exception(“zero denominator”); _ @< >@} _ @< >@if (d < 0) { _ @< >@@< >@this.num = -1 * n; _ @< >@@< >@this.denom = -1 * d; _ @< >@} _ @< >@else { _ @< >@@< >@this.num = n; _ @< >@@< >@this.denom = d; _ @< >@} _ }|| ||[#create-object create object]||Rational r1(7, 3); _ Rational* r2 = new Rational(8, 5);||Rational r = [[Rational alloc] initWith: 7: 3];||Rational r = new Rational(7,3);||Rational r = new Rational(7,3);|| ||[#destructor destructor]||Rational::~Rational() {};||-(void) dealloc { _ @< >@[super dealloc]; _ @< >@printf(“deallocated…”); _ }||protected void finalize() throws Throwable { _ @< >@super.finalize(); _ }||~Rational() { _ @< >@##gray|//perform cleanup//## _ }|| ||[#destroy-object destroy object] _ @< >@||delete r2;||[r release];||##gray|//none//##||##gray|//none//##|| ||[#define-method define method]||int Rational::height() { _ @< >@return (abs(num) > abs(denom)) ? abs(num) : abs(denom); _ }||-(int) height { _ @< >@if ( abs(self.num) > abs(self.denom) ) { _ @< >@@< >@return abs(self.num); _ @< >@} _ @< >@return abs(self.denom); _ }||public int height() { _ @< >@return (Math.abs(this.num) > this.denom) ? Math.abs(this.num) : this.denom; _ }||public int Height() { _ @< >@return (System.Math.Abs(this.num) > this.denom) ? System.Math.Abs(this.num) : this.denom; _ }|| ||[#invoke-method invoke method]||r1.height(); _ r2->height();||[r1 height];||r.height();||r.Height();|| ||[#define-class-method define class method]||##gray|//declare static in class definition//##||##gray|//precede definition with +://## _ +(Rational) max: (Rational) a: (Rational) b { _ @< >@if ( a.num * b.denom > b.num * a.denom ) { _ @< >@@< >@return a; _ @< >@} _ @< >@return b; _ }||##gray|//declare static in class definition//##||##gray|//declare static in class definition//##|| ||[#invoke-class-method invoke class method]|| || || || || ||[#receiver name of receiver]||this||self||this||this|| ||[#access-control access control]||##gray|//access keywords define regions://## _ class Foo { _ @< >@int privateInt1; _ @< >@int privateInt2; _ public: _ @< >@int publicInt1; _ @< >@int publicInt2; _ protected: _ @< >@int protectedInt1; _ @< >@int protectedInt2; _ private: _ @< >@int privateInt3; _ @< >@int privateInt4; _ };||##gray|//access keywords define regions://## _ @interface Foo : NSObject { _ @< >@int protectedInt1; _ @< >@int protectedInt2; _ @public _ @< >@int publicInt1; _ @< >@int publicInt2; _ @protected _ @< >@int protectedInt3; _ @< >@int protectedInt4; _ @private _ @< >@int privateInt1; _ @< >@int privateInt2; _ } _ @end||##gray|//access keywords required for methods and members://## _ public class Foo { _ @< >@private int privateInt; _ @< >@protected int protectedInt; _ @< >@public int publicInt; _ }||##gray|//access keywords available for methods and members://## _ public class Foo { _ @< >@private int privateInt1; _ @< >@int privateInt2; _ @< >@protected int protectedInt; _ @< >@public int publicInt; _ }|| ||[#anonymous-class anonymous class]||##gray|//possible but not useful//##||##gray|//none//##||(new Object() { public void hello() { System.out.println(“hello!”); } }).hello();|| || ||||||||||~ # inheritance-polymorphism[#inheritance-polymorphism-note inheritance and polymorphism]|| ||~ ||~ c++||~ objective c||~ java||~ c#|| ||[#dynamic-dispatch dynamic dispatch]||##gray|//declare as virtual in base class//##||##gray|//dispatch always dynamic//##||##gray|//dispatch dynamic by default//##||##gray|//declare as virtual in base class and override in derived class//##|| ||[#static-dispatch static dispatch]||##gray|//dispatch static by default//##||##gray|//dispatch always dynamic//##||##gray|//declare as final, private, or static (i.e. make it a class method)//##||##gray|//dispatch static by default; compiler error if same method defined in base and derived class and not marked virtual in base class//##|| ||[#subclass subclass]||class Integer : public Rational { _ @< >@public: _ @< >@Integer(int n); _ @< >@virtual ~Integer(); _ };|| ||public class RInteger extends Rational { _ @< >@public RInteger(int n) throws Throwable { _ @< >@@< >@super(n, 1); _ @< >@} _ }|| || ||[#superclass-constructor invoking superclass constructor]||Integer::Integer(int n) : Rational(n, 1) { _ }|| ||super(n, 1);|| || ||[#underivable-class mark class underivable or method unoverrideable]||##gray|//none//##||##gray|//none//##||final||sealed|| ||[#root-class root class] _ @< >@||##gray|//none//##||NSObject||java.lang.Object||System.Object|| ||[#root-class-methods root class methods]||##gray|//none//##||autorelease _ class _ conformsToProtocol: _ hash _ isEqual: _ isKindOfClass: _ isProxy _ performSelector: _ performSelector:withObject: _ performSelector:withObject:withObject: _ release _ respondsToSelector: _ retain _ retainCount _ self _ superclass||clone() _ equals() _ finalize() _ getClass() _ hashCode() _ toString()||Equals() _ Finalize() _ GetHashCode() _ GetType() _ MemberwiseClone() _ ReferenceEquals() _ ToString()|| ||||||||||~ # reflection[#reflection-note reflection]|| ||~ ||~ c++||~ objective c||~ java||~ c#|| ||[#type-class get type class of object]|| || || o = new Object(); _ Class c = o.getClass();||object o = new object(); _ System.Type t = o.GetType(); _ ##gray|//or//## _ System.type t = typeof(o);|| ||[#get-type-class-string get type class from string]|| || ||Class c = Class.forName(“java.io.File”);||using System; _ Type t = Type.GetType(“object”);|| ||[#get-type-class-identifier get type class from type identifier]||typeid(Foo)|| || ||System.Type t = typeof(object);|| ||[#class-name class name] _ @< >@||typeid(Foo).name()|| ||String name = c.getName();||t.ToString();|| ||[#get-methods get methods]|| || ||import java.lang.reflect.; _ Method[] m = c.getMethods();||using System.Reflection; _ System.Type t = typeof(object); _ MethodInfo[] a = t.GetMethods();|| ||[#has-method has method]|| || || import java.lang.reflect.; _ Class c = Class.forName(“java.io.File”); _ Method[] a = c.getMethods(); _ boolean hasMethod = false; _ for (int i=0; i < a.length; i++) { _ @< >@if (a[i].getName() == “toString”) { _ @< >@@< >@hasMethod = true; _ @< >@} _ }||##gray|//null if method not found://## _ MethodInfo m = t.GetMethod(“ToString”);|| ||[#invoke-method-object invoke method object]|| || ||import java.lang.reflect.; _ Class c = Class.forName(“java.io.File”); _ Method m = c.getMethod(“toString”); _ Object o = new Object(); _ m.invoke(o);||m.Invoke(o);|| ||||||||||~ # net-web[#net-web-note net and web]|| ||~ ||~ c++||~ objective c||~ java||~ c#|| ||get local hostname, dns lookup, reverse dns lookup|| || || || || ||http get|| || || || || ||http post|| || || || || ||absolute url|| || || || || ||parse url|| || || || || ||# url-encode[#url-encode-note url encode/decode]|| || ||import java.net.URLEncoder; _ import java.net.URLDecoder; _ _ String url = “@@http://www.google.com@@”; _ String s = URLEncoder.encode(url, “utf8”); _ String s2 = URLDecoder.decode(s, “utf8”);|| || ||base64 encode/decode|| || || || || ||||||||||~ # unit-tests[#unit-tests-note unit tests]|| ||~ ||~ c++||~ objective c||~ java||~ c#|| ||# test-class[#test-class-note test class]||$ cat > test_foo.cpp _ #include _ #include _ #include _ #include “test_foo.h” _ _ using namespace CppUnit; _ _ void TestFoo::test_01() { _ @< >@CPPUNITASSERTEQUAL(1, 1); _ } _ _ Test TestFoo::suite() { _ @< >@TestSuite* suiteOfTests = new TestSuite(“Foo”); _ @< >@suiteOfTests->addTest( _ @< >@new TestCaller( _ @< >@@< >@"test_01”, _ @< >@@< >@&TestFoo::test_01)); _ _ @< >@return suiteOfTests; _ } _ _ $ cat > test_foo.h _ #include _ _ class TestFoo: public CppUnit::TestCase { _ public: _ @< >@void test_01(); _ _ @< >@static CppUnit::Test* suite(); _ };|| || || || ||# run-all-tests[#run-all-tests-note run all tests]||$ cat > test_runner.cpp _ #include _ #include “test_foo.h” _ _ int main( int argc, char** argv) _ { _ @< >@CppUnit::TextUi::TestRunner runner; _ @< >@runner.addTest(TestFoo::suite()); _ @< >@runner.run(); _ @< >@return 0; _ } _ _ $ sudo apt-get install libcppunit-dev _ _ $ cat > Makefile _ testrunner: testrunner.o test_foo.o _ @< >@@< >@@< >@@< >@g++ -o $@ $^ -lcppunit _ _ check: test_runner _ @< >@@< >@@< >@@< >@./test_runner _ _ $ make check|| || || || ||# assert-equal[#assert-equal-note equality assertion]||#include _ _ CPPUNITASSERTEQUAL(1, 1); _ CPPUNITASSERTEQUAL(“foo”, “bar”); _ CPPUNITASSERTEQUAL_MESSAGE(“1 != 1”, _ @< >@1, 1);|| || || || ||# assert-approx[#assert-approx-note approximate assertion]|| || || || || ||# assert-exc[#assert-exc-note exception assertion]|| || || || || ||# setup[#setup-note setup]|| || || || || ||# teardown[#teardown-note teardown]|| || || || || ||||||||||~ # debugging-profiling[#debugging-profiling-note debugging and profiling]|| ||~ ||~ c++||~ objective c||~ java||~ c#|| ||flag for stronger warnings||$ g++ -Wall foo.cpp|| || || || ||suppress warnings||$ g++ -w foo.cpp|| || || || ||treat warnings as errors||$ g++ -Werror foo.cpp|| || || || ||run debugger||$ g++ -g -o foo foo.cpp _ _ $ gdb foo _ (gdb) b main _ (gdb) run|| || || || ||# debugger-cmds[#debugger-cmds-note debugger commands] _ ##gray|//help, list source, (re)load executable, next, step, set breakpoint, show breakpoints, delete breakpoint, continue, backtrace, up stack, down stack, print, run, quit//##||@@>@@ h _ @@>@@ l ##gray|[FIRSTLINENO, LASTLINENO]## _ @@>@@ file ##gray|PATH## _ @@>@@ n _ @@>@@ s _ @@>@@ b ##gray|[FILE:]LINENO## _ @@>@@ i _ @@>@@ d ##gray|NUM## _ @@>@@ c _ @@>@@ bt _ @@>@@ up _ @@>@@ do _ @@>@@ p ##gray|EXPR## _ @@>@@ r ##gray|[ARG1[, [ARG2 @@…@@]]## _ @@>@@ q|| || || || ||benchmark code|| || || || || ||profile code||##gray|//gprof does not work on Mac OS X://## _ _ $ g++ -pg -o foo foo.cpp _ _ $ ./foo _ _ $ gprof foo|| || || || ||memory tool||$ sudo apt-get install valgrind _ _ $ g++ -o foo foo.cpp _ _ $ valgrind ./foo|| || || || ||~ ||~ ##EFEFEF|@@____________________________________________@@##||~ ##EFEFEF|@@_________________________________________@@##||~ ##EFEFEF|@@_________________________________________@@##||~ ##EFEFEF|@@____________________________________________@@##||
# version-note + [#version Version]
# version-used-note ++ [#version-used version used]
The compiler version used for this sheet.
# show-version-note ++ [#show-version show version]
How to get the compiler version.
# implicit-prologue-note ++ [#implicit-prologue implicit prologue]
Code which examples in the sheet assume to have already been executed.
# grammar-execution-note + [#grammar-execution Grammar and Execution]
# hello-world-note ++ [#hello-world hello world]
How to write, compile, and run a “Hello, World!” program.
# file-suffixes-note ++ [#file-suffixes file suffixes]
For source files, header files, and compiled object files.
C++
The {{gcc}} compiler will treat a file with any of the following suffixes as C++ source:
code .cc .cp .cxx .cpp .CPP .c++ .C /code
GNU {{make}} has built-in rules which treat the following suffixes as C++ source:
The Google C++ Style Guide recommends that {{.cc}} be used as the suffix for C++ source files. Visual Studio uses {{.cpp}} as the C++ source file suffix.
One sometimes sees suffixes for headers which distinguish C++ code from C code, but the Google C++ Style Guide and Visual Studio both use {{.h}} as the C++ header suffix.
The C++11 standard library uses no suffix at all in headers, at least in the {{#include}} 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.
# block-delimiters-note ++ [#block-delimiters block delimiters]
How blocks are delimited.
A block contains a sequence of statements. Blocks for function bodies in function definitions; to define the branches of {{if}} statements and the bodies of {{while}} loops.
Class definition bodies are blocks, though the statements that appear in them are restricted to declarations and definitions.
Bare blocks can be used to limit the scope of variables which are declared inside them.
# stmt-terminator-note ++ [#stmt-terminator statement terminator]
How statements are terminated.
# top-level-stmt-note ++ [#top-level-stmt top level statements]
Statements that can appear at the top level of a source file.
# eol-comment-note ++ [#eol-comment end-of-line comment]
The syntax for a comment which is terminated by the end of the line.
# multiple-line-comment-note ++ [#multiple-line-comment multiple line comment]
The syntax for a comment which can span multiple lines.
The {{/* /}} delimiters do not nest. Using them to comment out code which already contains a {{/ */}} comment usually results in a syntax error.
# variables-expressions-note + [#variables-expressions Variables and Expressions]
# local-var-note ++ [#local-var local variable]
How to declare a variable which is allocated on the stack.
# uninitialized-local-var-note ++ [#uninitialized-local-var uninitialized local variable]
The value contained by a local variable that wasn’t initialized.
# global-var-note ++ [#global-var global variable]
How to declare a global variable.
# uninitialized-global-var-note ++ [#uninitialized-global-var uninitialized global variable]
The value assigned to an uninitialized global variable.
# write-once-var-note ++ [#write-once-var write-once variable]
How to declare a constant variable.
# assignment-note ++ [#assignment assignment]
How to assign a value to a variable.
# compound-assignment-note ++ [#compound-assignment compound assignment]
The compound assignment operators.
If {{}} is a binary operator and the language has the compound assignment operator {{=}}, then the following are equivalent:
# incr-decr-note ++ [#incr-decr increment and decrement]
The C-style increment and decrement operators.
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.
c++
Since the compound assignment operators also return the value after mutation, the following are equivalent for primitive types:
code cout << ++i << endl; cout << (i += 1) << endl; /code
The parens are necessary because compound assignment has lower precedence than the {{@@<<@@}} operator.
The caveat about primitive types is necessary because the {{++}} operator can be overloaded. In fact the prefix and postfix versions can be overloaded separately.
In the case of the the postfix operator, the following are equivalent for primitive types:
code cout << i– << endl; cout << (i += 1, i - 1) << endl; /code
The compiler may have to allocate a temporary variable to hold the value of {{i - 1}}, which means the postfix version might be slower.
# addr-note ++ [#addr address]
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.
# dereference-note ++ [#dereference dereference]
How to get the value stored at a memory address.
# type-size-note ++ [#type-size type size]
How to get the size of a type in bytes.
# allocate-heap-note ++ [#allocate-heap allocate heap]
How to allocate memory for a primitive type on the heap.
C++
//new// and //delete// can be used to manage the memory of both primitive types and objects.
objective c
Object C has a different memory management schemes for primitive types and objects. Objects are allocated with //alloc// and freed by means of //NSAutoreleasePool//. 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 //malloc//.
Arrays of objects can be created with //NSArray// and //NSMutableArray//.
java
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.
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:
code Integer i = new Integer(0); /code
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.
C#
C# behavior is like Java. Note that C# lacks specific wrapper classes for each primitive data type.
# free-heap-note ++ [#free-heap free heap]
How to free the memory for a primitive type that was allocated on the heap.
# null-note ++ [#null null]
C++
A typical definition:
code const int NULL = 0; /code
# coalesce-note ++ [#coalesce coalesce]
The equivalent of the COALESCE function from SQL.
C++, Objective C++:
The short circuit or operator @@||@@ 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.
Java:
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.
# arithmetic-logic-note + [#arithmetic-logic Arithmetic and Logic]
# boolean-type-note ++ [#boolean-type boolean type]
C
The following definitions are common:
code typedef int BOOL; #define TRUE 1 #define FALSE 0 /code
Objective C
From objc.h:
code typedef signed char BOOL; #define YES (BOOL)1 #define NO (BOOL)0 /code
C#
bool is an alias for System.Boolean
# true-false-note ++ [#true-false true and false]
Literals for the boolean values true and false.
C
The following definitions are common:
code typedef int BOOL; #define TRUE 1 #define FALSE 0 /code
Objective C
From objc.h:
code typedef signed char BOOL; #define YES (BOOL)1 #define NO (BOOL)0 /code
# falsehoods-note ++ [#falsehoods falsehoods]
Values which evaluate as false in the conditional expression of an {{if}} statement.
# logical-op-note ++ [#logical-op logical operators]
The logical operators.
In all languages on this sheet the && and @@||@@ operators short circuit: i.e. && will not evaluate the 2nd argument if the 1st argument is false, and @@||@@ 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.
C++
C++ defines //and//, //or//, and //not// to be synonyms of &&, @@||@@, and !, with the same semantics and precedence.
Java
The arguments of the logical operators must be of type //boolean//.
C#
The arguments of the logical operators must be of type //bool//.
# relational-op-note ++ [#relational-op relational operators]
Binary operators which return boolean values.
# int-type-note ++ [#int-type integer type]
Signed integer types.
C++
Whether //char// is a signed or unsigned type depends on the implementation.
C#
C# has the following aliases:
sbyte: System.SByte short: System.Int16 int: System.Int32 long: System.Int64
# unsigned-type-note ++ [#unsigned-type unsigned type]
Unsigned integer types.
C++
Whether //char// is a signed or unsigned type depends on the implementation.
C#
C# has the following aliases:
byte: System.Byte ushort: System.UInt16 uint: System.UInt32 ulong: System.UInt64
# float-type-note ++ [#float-type float type]
Floating point types.
C#
C# has the following aliases:
float: System.Single double: System.Double
# fixed-type-note ++ [#fixed-type fixed type]
Fixed-point decimal types.
C#:
C# has the following alias:
decimal: System.Decimal
# arithmetic-op-note ++ [#arithmetic-op arithmetic operators]
The arithmetic binary operators: addition, subtraction, multiplication, division, modulus.
# int-div-note ++ [#int-div integer division]
How to get the quotient of two integers.
# int-div-zero-note ++ [#int-div-zero integer division by zero]
The results of integer division by zero.
C++, Objective C
The behavior for division by zero is system dependent; the behavior described is nearly universal on Unix.
C#
It is a compilation error to divide by a zero constant. Division by a variable set to zero results in a runtime exception.
# float-div-note ++ [#float-div float division]
How to perform floating point division on two integers.
# float-div-zero-note ++ [#float-div-zero float division by zero]
The result of floating point division by zero.
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).
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.
# power-note ++ [#power power]
How to perform exponentiation.
C++
//powm1// is an abbreviation for “power minus one”. Hence the need to add one to get the answer.
# sqrt-note ++ [#sqrt sqrt]
The positive square root function.
# sqrt-negative-one-note ++ [#sqrt-negative-one sqrt -1]
The result of taking the square root of a negative number.
Here is a list of the standard mathematical functions whose domains do not cover the entire real number line:
||~ function||~ returns inf on||~ returns nan on||~ returns -inf on|| ||sqrt||inf||[-inf, 0)|| || ||log||inf||[-inf, 0)||0|| ||asin|| ||[-inf, -1) U (1, inf]|| || ||acos|| ||[-inf, -1) U (1, inf]|| ||
# transcendental-func-note ++ [#transcendental-func transcendental functions]
The exponential and logarithm functions; the trigonometric functions; the inverse trigonometric functions.
The arguments of the trigonometric functions are in radians as are the return values of the inverse trigonometric functions.
# transcendental-const-note ++ [#transcendental-const transcendental constants]
The transcendental constants //e// and //pi//.
# float-truncation-note ++ [#float-truncation float truncation]
Functions for converting a float to a nearby integer value.
C:
The {{math.h}} library also provides {{floor}} and {{ceil}} which return {{double}} values.
Java:
{{Math.floor}} and {{Math.ceil}} return {{double}} values.
# absolute-val-note ++ [#absolute-val absolute value]
The absolute value of a numeric quantity.
# int-overflow-note ++ [#int-overflow integer overflow]
What happens when an integer expression results in a value larger than what can be stored in the integer type.
# float-overflow-note ++ [#float-overflow float overflow]
What happens when a float expression results in a value larger than largest representable finite float value.
# float-limits-note ++ [#float-limits float limits]
The largest finite floating point number and the smallest positive floating point number.
# complex-construction-note ++ [#complex-construction complex construction]
How to construct a complex number.
# complex-decomposition-note ++ [#complex-decomposition complex decomposition]
How to get the components of a complex number. Both Cartesian and polar decompositions are illustrated. Also how to get the complex conjugate.
# random-num-note ++ [#random-num random number]
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.
c++:
The standard library includes functions for generating random numbers from other distributions].
# random-seed-note ++ [#random-seed random seed]
How to set the seed for the random number generator.
# bit-op-note ++ [#bit-op bit operators]
The bit operators: left shift, right shift, and, or, xor, and complement.
C++
//bitand//, //bitor//, and //compl// are synonyms of &, |, and ~ with identical precedence and semantics.
# binary-octal-hex-literals-note ++ [#binary-octal-hex-literals binary, octal, and hex literals]
Binary, octal, and hex integer literals.
# radix-note ++ [#radix radix]
How to convert integers to strings of digits of a given base. How to convert such strings into integers.
# strings-note + [#strings Strings]
# str-type-note ++ [#str-type string type]
The type for strings.
# str-literal-note ++ [#str-literal string literal]
The syntax for string literals.
# newline-literal-note ++ [#newline-literal newline in literal]
Can a newline be used in a string literal? Does the newline appear in the resulting string object?
# str-literal-escape-note ++ [#str-literal-escape literal escapes]
Escape sequences that can be used in string literals.
# allocate-str-note ++ [#allocate-str allocate string]
How to allocate a string.
Java
The following code
code String t = new String(s); /code
creates a copy of the string //s//. However, because Java strings are immutable, it would be safe to store the same string object it //t// as follows:
# mutable-str-note ++ [#mutable-str are strings mutable?]
# copy-str-note ++ [#copy-str copy string]
# fmt-str-note ++ [#fmt-str format string]
# compare-str-note ++ [#compare-str compare strings]
C++
//string::compare// 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.
Objective C //compare// will return -1, 0, or 1.
Java
//compareTo// will return a negative value, 0, or a positive value.
C#
//CompareTo// will return -1, 0, or 1.
# str-concat-note ++ [#str-concat concatenate]
# str-replicate-note ++ [#str-replicate replicate]
# translate-case-note ++ [#translate-case translate case]
# trim-note ++ [#trim trim]
# pad-note ++ [#pad pad]
# num-to-str-note ++ [#num-to-str number to string]
# str-to-num-note ++ [#str-to-num string to number]
C++
//strtoimax//, //strtol//, //strtoll//, //strtoumax//, //strtoul//, and //strtoull// take three arguments:
code intmax_t strtoimax(const char *str, char **endp, int base); /code
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.
//strtof//, //strtod//, and //strtold// take three arguments:
code double strtod(const char *str, char **endp); /code
Java
//parseInt// has an optional second argument for the base.
# join-note ++ [#join join]
java:
Use {{StringBuilder}} to implement join:
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
# split-note ++ [#split split]
# serialize-note ++ [#serialize serialize]
# str-length-note ++ [#str-length string length]
# index-substr-note ++ [#index-substr index of substring]
# extract-substr-note ++ [#extract-substr extract substring]
# char-type-note ++ [#char-type character type]
# char-literal-note ++ [#char-literal character literal]
# test-char-note ++ [#test-char test character]
# regexes-note + [#regexes Regular Expressions]
# regex-match ++ regex match
# regex-substitute ++ regex substitute
# dates-time-note + [#dates-time Date and Time]
# date-time-type-note ++ [#date-time-type date and time type]
The data type used to store a combined date and time.
# current-date-time-note ++ [#current-date-time current date and time]
How to get the current date and time.
# unix-epoch-note ++ [#unix-epoch to unix epoch, from unix epoch]
How to convert a date/time object to the Unix epoch. How to convert the Unix epoch to a date/time object.
The Unix epoch is the number of seconds since 1 January 1970 UTC.
c#:
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.
The magic constant (11644473600) used for the conversion can be calculated with the following code:
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
# format-date-note ++ [#format-date format date]
How to use a format string to display a date/time object.
The canonical example of doing this is the {{strftime}} function from the C standard library which defines used letters prefix by percent signs as conversion specification characters, e.g. %Y-%m-%d.
# parse-date-note ++ [#parse-date parse date]
How to use a format string to parse date data from a string.
# date-subtraction-note ++ [#date-subtraction date subtraction]
# add-duration-note ++ [#add-duration add duration]
# date-parts-note ++ [#date-parts date parts]
# time-parts-note ++ [#time-parts time parts]
# fixed-length-arrays-note + [#fixed-length-arrays Fixed-Length Arrays]
# fixed-len-array-stack-note ++ [#fixed-len-array-stack declare on stack]
How to allocate an array which is freed when the block in which it is defined goes out of scope.
# fixed-len-array-heap-note ++ [#fixed-len-array-heap declare on heap]
How to allocate an array on the heap.
# free-fixed-len-array-heap-note ++ [#free-fixed-len-array-heap free heap]
How to free an array that was allocated on the heap.
# fixed-len-array-init-list-note ++ [#fixed-len-array-init-list initialization list]
Objective C
NSArray can only store instances of NSObject. For primitive types, use C arrays.
Java
Java permits arrays to be declared with C-style syntax:
# fixed-len-array-size-note ++ [#fixed-len-array-size size]
How to get the size of a fixed-length array.
# fixed-len-array-lookup-note ++ [#fixed-len-array-lookup lookup]
How to get the value stored at an index in a fixed-length array.
C++
Arrays can be manipulated with pointer syntax. The following sets //x// and //y// to the same value:
code int a[] = {3,7,4,8,5,9,6,10}; int x = a[4]; int y = *(a+4); /code
# fixed-len-array-update-note ++ [#fixed-len-array-update update]
How to update the value stored at an index in a fixed-length array.
# fixed-len-array-out-of-bounds-note ++ [#fixed-len-array-out-of-bounds out-of-bounds]
What happens when an out-of-bounds index is used to access a value in a fixed-length array.
# copy-fixed-len-array-note ++ [#copy-fixed-len-array copy]
How to copy a fixed-length array.
# fixed-len-array-as-func-arg-note ++ [#fixed-len-array-as-func-arg as function argument]
How to pass a fixed-length array as a function argument.
# iterate-over-fixed-len-array-note ++ [#iterate-over-fixed-len-array iterate]
How to iterate over the values of the fixed-length array.
C++
Range-based for loops can be used with fixed-length arrays (but not pointers):
code int a[4] = {3, 2, 4, 1}; int sum(0);
for (const auto& n: a) { sum += n; } /code
# sort-fixed-len-array-note ++ [#sort-fixed-len-array sort]
# resizable-arrays-note + [#resizable-arrays Resizable Arrays]
# decl-resizable-array-note ++ [#decl-resizable-array declare]
# resizable-array-init-list-note ++ [#resizable-array-init-list initialization list]
# resizable-array-size-note ++ [#resizable-array-size size]
# resizable-array-capacity-note ++ [#resizable-array-capacity capacity]
# resizable-array-empty-test-note ++ [#resizable-array-empty-test empty test]
# resizable-array-lookup-note ++ [#resizable-array-lookup lookup]
# resizable-array-update-note ++ [#resizable-array-update update]
# resizable-array-out-of-bounds-note ++ [#resizable-array-out-of-bounds out-of-bounds behavior]
# resizable-arary-elem-index-note ++ [#resizable-array-elem-index element index]
# slice-resizable-array-note ++ [#slice-resizable-array slice]
# slice-resizable-array-to-end-note ++ [#slice-resizable-array-to-end slice to end]
# resizable-array-back-note ++ [#resizable-array-back manipulate back]
# resizable-array-front-note ++ [#resizable-array-front manipulate front]
# concat-resizable-array-note ++ [#concat-resizable-array concatenate]
# replicate-resizable-array-elem-note ++ [#replicate-resizable-array-elem replicate element]
# copy-resizable-array-note ++ [#copy-resizable-array copy]
# resizable-array-as-func-arg-note ++ [#resizable-array-as-func-arg array as function argument]
# iterate-over-resizable-array-note ++ [#iterate-over-resizable-array iterate]
How to iterate over a resizable array.
C++
The range-based for loop was introduced in C++11. It it can be used to iterate over an initialization list:
code for (const auto& n: {1, 2, 3}) { sum += n; } /code
# tuples-note + [#tuples Tuples]
# pair ++ pair
# dictionaries-note + [#dictionaries Dictionaries]
# map ++ map declaration
C:
For those interested in an industrial strength hashtable implementation for C, here is the [http://svn.ruby-lang.org/cgi-bin/viewvc.cgi/trunk/include/ruby/st.h?revision=26401&view=markup header file] and the [http://svn.ruby-lang.org/cgi-bin/viewvc.cgi/trunk/st.c?revision=26672&view=markup source file] for the hashtable used by Ruby. For those interested in a “Computer Science 101” implementation of a hashtable, here is a simpler [http://gist.github.com/400762 source file] and [http://gist.github.com/400764 header file].
# map-access ++ map access
# map-size ++ map size
# map-remove ++ map remove
# map-element-not-found ++ map element not found result
# map-iterator ++ map iterator
# functions-note + [#functions Functions]
# decl-func-note ++ [#decl-func declare function]
How to declare the type of a function.
# def-func-note ++ [#def-func define function]
How to define a function.
# invoke-func-note ++ [#invoke-func invoke function]
How to invoke a function.
# def-static-method-note ++ [#def-static-class-method define static class method]
# invoke-static-method-note ++ [#invoke-static-class-method invoke static class method]
# overload-func-note ++ [#overload-func overload function]
# default-arg-note ++ [#default-arg default argument]
# variable-num-arg-note ++ [#variable-num-arg variable number of arguments]
# named-param-note ++ [#named-param named parameters]
Objective C:
Named parameters must be invoked in the order in which they are defined in the method signature.
C#:
Named parameter do not need to be invoked in the order in which they are defined in the method signature. Additionally, their use in invocation is optional: the arguments can be provided without names in which case the definition order must be used.
# pass-by-val-note ++ [#pass-by-val pass by value]
# pass-by-ref-note ++ [#pass-by-ref pass by reference]
# pass-by-addr-note ++ [#pass-by-addr pass by address]
# retval-note ++ [#retval return value]
# no-retval-note ++ [#no-retval no return value]
# recursive-func-note ++ [#recursive-func recursive function]
# anonymous-func-note ++ [#anonymous-func anonymous function]
# invoke-anonymous-func-note ++ [#invoke-anonymous-func invoke anonymous function]
# closure-note ++ [#closure closure]
# func-private-state-note ++ [#func-private-state function with private state]
# func-as-val-note ++ [#func-as-val function as value]
# overload-op-note ++ [#overload-op overload operator]
C++
A note on how to overload postfix and prefix {{++}} and {{–}}.
# execution-control-note + [#execution-control Execution Control]
# if-note ++ [#if if]
The syntax for an {{if}} statement.
# dangling-else-note ++ [#dangling-else dangling else]
The curly braces around the branches of an {{if}} statement are optional when the branch contains a single statement.
From the perspective of the parser, the branches are statements. Curly branch delimited blocks are legal wherever a statement is legal.
When {{if}} statements are nested and the outer {{if}} statement does not put its if-clause in curly braces, the parser will match a subsequent {{else}} to the inner {{if}}. Code which puts the {{else}} on the same level of indentation as the outer {{if}} is deceptive.
# switch-note ++ [#switch switch]
The syntax for a {{switch}} statement.
A {{switch}} checks the value of integer expression and jumps to the correct label. This can faster than an {{if}} statement with numerous {{else if}} branches which tests the expression until a match is found.
Execution falls through to code after subsequent labels unless a {{break}} statement is encountered. This makes it possible to write code once which handles multiple values.
# while-note ++ [#while while]
The syntax for a {{while}} loop.
If the body of a {{while}} loop is a single statement, the curly braces are optional.
The languages in this sheet also have a {{do-while}} loop, which is a loop which always executes at least once.
Here is an example of a {{do-while}} loop which converts an unsigned integer to a string. The corresponding {{while}} loop would require extra handling when the integer is zero.
code /* unsigned int n; * char* s; */
do { s[i++] = n % 10 + ‘0’; } while ((n /= 10) > 0); s[i] = ‘\0’; reverse(s); /code
# for-note ++ [#for for]
The syntax for a C-style {{for}} loop.
The parens of the {{for}} loop contain three expressions set off by semicolons: the //initialization//, the //condition//, and the //increment//.
The initialization expression executes once before the loop starts.
The condition expression executes once before the start of each iteration. The loop stops if the condition is false.
The increment executes at the end of each iteration.
# break-note ++ [#break break]
The {{break}} statement terminates execution of the innermost containing loop or switch statement.
# break-nested-loops-note ++ [#break-nested-loops break out of nested loops]
A method for breaking out of nested loops.
# continue-note ++ [#continue continue]
The {{continue}} statement, which terminates execution of the current loop iteration.
# goto-note ++ [#goto goto]
# exceptions-note + [#exceptions Exceptions]
# base-exc-note ++ [#base-exc base exception]
The base class or interface for exceptions.
# predefined-exc-note ++ [#predefined-exc predefined exceptions]
A partial list of exceptions raised by the language or the standard library.
# raise-exc-note ++ [#raise-exc raise exception]
How to raise an exception.
C++
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.
Objective C
Objective C can only throw an instance of NSException or one of its subclasses.
Java
Java can only throw an implementation of java.lang.Throwable.
C#
C# can only throw an instance of System.Exception of one of its subclasses.
# handle-exc-note ++ [#handle-exc handle exception]
How to handle an exception.
C++
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.
Objective C
Exceptions are thrown and caught by pointer value.
# def-exc-note ++ [#def-exc define exception]
How to define a new exception type.
# re-raise-exc-note ++ [#re-raise-exc re-raise exception]
How to handle and re-raise an exception.
# catch-all-handler-note ++ [#catch-all-handler catch-all handler]
How to write a handler witch catches any exception.
# multiple-handlers-note ++ [#multiple-handlers multiple handlers]
# error-msg-note ++ [#error-msg error message]
# errno-note ++ [#errno system call errno]
# finally-clause-note ++ [#finally-clause finally clause]
C++
code Class Finally {
void (*finally)();
Finally(void (*f)()) : finally(f) {
}
~Finally() { do_cleanup(); } };
{ Cleanup c();
risky(); }
# exc-specification-note ++ [#exc-specification exception specification]
Java
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.
# concurrency-note + [#concurrency Concurrency]
# file-handles-note + [#file-handles File Handles]
# printf-note ++ [#printf printf]
How to print a formatted string to standard out.
# read-file ++ read from file
C
If there is an error, the global variable //errno// will be set to a nonzero value, and //strerror(errno)// will return an error message for the error.
# write-file ++ write to file
# files-note + [#files Files]
# file-fmt-note + [#file-fmt File Formats]
# directories-note + [#directories Directories]
# processes-environment-note + [#processes-environment Processes and Environment]
# main ++ signature of main
# first-argument ++ first argument
C
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.
# environment-variable ++ environment variable
# iterate-through-environment-variables ++ iterate through environment variables
# libraries-namespaces-note + [#libraries-namespaces Library and Namespaces]
# std-lib-name-note ++ [#std-lib-name standard library name]
The name of the standard library.
C++
[http://www.sgi.com/tech/stl/ Standard Template Library (STL)]
The STL might not be installed by default.
Objective C
[http://developer.apple.com/mac/library/documentation/cocoa/reference/foundation/objc_classic/index.html Foundation Framework]
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.
Java
[http://java.sun.com/javase/6/docs/api/ Java 1.6 API]
C#
[http://msdn.microsoft.com/en-us/library/ms229335(v=VS.100).aspx .NET Framework 4 Class Library] [http://www.go-mono.com/docs/ Mono Documentation]
The core of the .NET framework is called the Base Class Library. Mono implements the BCL, but not all of the .NET framework.
# user-defined-types-note + [#user-defined-types User-Defined Types]
# typedef-note ++ [#typedef typedef]
C
Because C integer types don’t have well defined sizes, //typedef// is sometimes employed to as an aid to writing portable code. One might include the following in a header file:
code typedef int int32_t; /code
The rest of the code would declare integers that need to be 32 bits in size using //int32_t// and if the code needed to be ported to a platform with a 16 bit //int//, only a single place in the code requires change. In practice the //typedef// abstraction is leaky because functions in the standard library such as //atoi//, //strtol//, or the format strings used by //printf// depend on the underlying type used.
Java
Java has well defined integer sizes so //typedef// is not needed as a portability aid. In other situations where a C programmer would use a //typedef// for data abstraction, a Java programmer must either define a class or retain the raw primitive type throughout the code.
# enum-note ++ [#enum enum]
C
Enums were added to the C standard when the language was standardized by ANSI in 1989.
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, //a// and //c// are both zero and //b// and //d// are both one.
code enum { a=0, b, c=0, d }; /code
A //typedef// can be used to make the //enum// keyword unnecessary in variable declarations:
code typedef enum { mon, tue, wed, thu, fri, sat, sun } dayofweek; dayofweek d = tue; /code
From the point of view of the C compiler, an enum is an //int//. The C compiler does not prevent assigning values to an enum type that are not in the enumerated list. Thus, the following code compiles:
code enum dayofweek { mon, tue, wed, thu, fri, sat, sun }; dayofweek d = 10;
typedef enum { mon, tue, wed, thu, fri, sat, sun } dayofweek2; dayofweek2 d2 = 10; /code
C++
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:
code enum dayofweek { mon, tue, wed, thu, fri, sat, sun }; dayofweek d = 10; /code
produces an error like the following:
code main.cpp: In function ‘int main()’: main.cpp:21: error: invalid conversion from ‘int’ to ‘main()::dayofweek’ /code
Java
Java added enums in 1.5.
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 //ordinal()// which returns the integer value, however.
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.
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.
C#
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.
When used as an argument in a C# style format string, an enum value returns the string corresponding to its identifier.
# struct-definition ++ struct definition
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 [http://en.wikipedia.org/wiki/Uniform_access_principle uniform access principle].
C++:
From //The C++ Programming Language: 3rd Edition//:
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: ...
# struct-declaration ++ struct declaration
# struct-initialization ++ struct initialization
C
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.
# struct-member-assignment ++ struct member assignment
# struct-member-access ++ struct member access
C
The period operator used for member access has higher precedence than the pointer operator. Thus parens must be used to get at the member of a struct referenced by a pointer:
code struct medal_count { char* country; int gold; int silver; int bronze; }
struct medalcount spain = { “Spain”, 3, 7 4 }; struct medalcount winner = &spain; printf(“The winner is %s with %d gold medals”, (winner).country, (*winner).gold); /code
//ptr->mem// is a shortcut for //(*ptr).mem//:
code printf(“The winner (%s) earned %d silver medals”, winner->country, winner->silver); /code
# generic-types-note + [#generic-types Generic Types]
# define-generic ++ define generic type
# instantiate-generic ++ instantiate generic type
# objects-note + [#objects Objects]
# define-class ++ define class
# constructor ++ constructor
# create-object ++ create object
# destructor ++ destructor
C++
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.
Java
Java does not chain finalize() methods, so the derived class should explicitly call the parent.
# destroy-object ++ destroy object
Java
finalize() is called by the Java garbage collector.
# define-method ++ define method
# invoke-method ++ invoke method
# define-class-method ++ define class method
# invoke-class-method ++ invoke class method
# receiver ++ name of receiver
# access-control ++ access control
objective c:
Access control only applies to members; all methods are public. gcc 4.0 does not enforce the access restrictions; it merely gives warnings.
# anonymous-class ++ anonymous class
# inheritance-polymorphism-note + [#inheritance-polymorphism Inheritance and Polymorphism]
# dynamic-dispatch ++ dynamic dispatch
# static-dispatch ++ static dispatch
Method dispatch is //static// if the method is determined by the variable type, and //dynamic// 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.
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 [http://en.wikipedia.org/wiki/Virtual_method_table virtual method table].
The use of the keyword //static// 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.
# subclass ++ subclass
# superclass-constructor ++ superclass constructor
# underivable-class ++ mark class underivable or method overrideable
# root-class ++ root class
Name of the root class, if there is one.
objective c:
It is possible to define a root class other than NSObject.
# root-class-methods ++ root class methods
A selection of methods available on the root class.
# reflection-note + [#reflection Reflection]
# type-class ++ get type class of object
# get-type-class-string ++ get type class from string
# get-type-class-identifier ++ get type class from type identifier
c++:
//typeid// returns a value of type //typeinfo//. The assignment method and copy constructor of //typeinfo// are private.
# class-name ++ class name
c++:*
The string returned by //type_info.name()// contains more than the class name. The code below displayed the string “Z4mainE3Foo” when run on my system.
code class Foo { int i; }; puts(typeid(Foo).name()); /code
# get-methods ++ get methods
# has-method ++ has method
# invoke-method-object ++ invoke method object
# net-web-note + [#net-web Net and Web]
# url-encode-note ++ [#url-encode url encode/decode]
How to URL encode and URL decode a string.
URL encoding is also called percent encoding. It is used to escape special characters in GET query string parameters.
Reserved characters according to [http://www.ietf.org/rfc/rfc3986.txt RFC 3986] are replaced by a percent sign % followed by a two hex digit representation of the ASCII code. The reserved characters are:
code ! * ‘ ( ) ; : @ & = + $ , / ? # [ ] /code
Spaces can optionally be represented by a plus sign +.
# unit-tests-note + [#unit-tests Unit Tests]
# debugging-profiling-note + [#debugging-profiling Debugging and Profiling]
# cpp + [#top C++]
[http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2012/n3337.pdf C++11 Standard (pdf)] [http://www.cplusplus.com/reference/ Standard C++ Library Reference] [http://gcc.gnu.org/onlinedocs/libstdc++/ The GNU C++ Library] [http://google-styleguide.googlecode.com/svn/trunk/cppguide.xml Google C++ Style Guide] [http://doc.qt.digia.com/qq/qq13-apis.html Designing Qt-Style C++ APIs]
C++11 and gcc
{{gcc}} 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 {{gcc}} 4.8 or later, but {{gcc}} 4.6 is sufficient for the examples in this sheet. Support for C++11 features must be requested with the {{-std=c++0x}} flag.
C++11 on Mac OS X
The version of {{gcc}} that comes with XCode in Mac OS X 10.8 as part of the command line tools package is 4.2. This version of {{gcc}} does not support C++11, but the command line tools package also provides the {{clang}} compiler which does:
code clang++ -std=c++11 -stdlib=libc++ -o hello hello.cpp /code
Like {{gcc}}, {{clang}} does not compile C++11 by default, hence the {{-std=c++11}} flag. One must also specify the C++ standard library, since the {{libc++}} library which comes with {{clang}} will support C++11, but the {{libstdc++}} library provided with {{gcc}} 4.2 will not, and {{clang}} uses the {{gcc}} library by default.
C++11 Windows
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 {{gcc}} 4.8.
Compatibility with C
What’s New in C++11
# objective-c + [#top Objective-C]
[https://developer.apple.com/library/mac/#documentation/Cocoa/Conceptual/ProgrammingWithObjectiveC/Introduction/Introduction.html Programming with Objective-C] Apple [http://www.gnu.org/software/gnustep/resources/documentation/Developer/Base/ProgrammingManual/manual_toc.html GNUstep] [http://developer.apple.com/mac/library/documentation/cocoa/reference/foundation/objc_classic/index.html Mac OS X Foundation Framework]
# java + [#top Java]
[http://java.sun.com/javase/6/docs/api/ Java 1.6 API] [https://jdk7.java.net/ JDK 7 Project] [http://java.sun.com/docs/books/jvms/second_edition/html/VMSpecTOC.doc.html JVM Specification 2nd Ed] [http://java.sun.com/docs/books/jls/ The Java Language Specification 3rd Ed]
A Java program is created from source code via an explicit compilation step using {{javac}}. The executable is then launched using the virtual machine {{java}}. Both {{javac}} and {{java}} use the Java runtime jar {{rt.jar}}, which contains the Java standard libraries.
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
Using {{dtruss}} on Mac OS X to verify that {{javac}} and {{java}} use {{rt.jar}}:
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
# c-sharp + [#top C#]
[http://standards.iso.org/ittf/PubliclyAvailableStandards/c042926_ISO_IEC_23270_2006(E).zip C# Standard: ECMA-334] [http://www.go-mono.com/docs/ Mono API] [http://msdn.microsoft.com/en-us/library/67ef8sbd.aspx C# Programming Guide] Microsoft