/images/art2.png

Common method comparison - c++ & java

Sort

C++

For c++, here is official document

sort()

This method sorts elements in the range [first, last).

Result is in ascending order by deflaut.

Introsort.

// sort algorithm example
#include <iostream>     // std::cout
#include <algorithm>    // std::sort
#include <vector>       // std::vector

bool myfunction (int i,int j) { return (i>j); }

int main () {
  int myints[] = {32,71,12,45,26,80,53,33};
  std::vector<int> myvector (myints, myints+8); // 32 71 12 45 26 80 53 33

  // using default comparison (operator <):
  std::sort (myvector.begin(), myvector.begin()+4); //(12 32 45 71)26 80 53 33

  // using function as comp
  std::sort (myvector.begin(), myvector.end(), myfunction); //80 71 53 45 33 32 26 12

  // print out content:
  std::cout << "myvector contains:";
  for (std::vector<int>::iterator it=myvector.begin(); it!=myvector.end(); ++it)
    std::cout << ' ' << *it;
  std::cout << '\n';
  return 0;
}

On average, linearithmic in the distance between first and last: Performs approximately N*log2N

Container comparison - c++ & java

Container comparison

C++ version is c++11. Java version is java se8.

C++JAVADescription
array / [ ][ ]固定大小的数组
vectorArrayList可变长度的数组
Vector可变长度的数组,支持同步操作,效率比 ArrayList 略差
listLinkedList双链表,便于增删
forward_list单链表,c++没有给他提供 size()的方法
dequeArrayDeque双向队列
stackStack栈,先进后出
queueQueue队,先进先出
priority_queuePriorityQueue支持优先级的队列
setTreeSet集合,数据有序,红黑树
unordered_setHashSet集合,数据无序,hash
mapTreeMapkey-value 映射,key 有序,红黑树
unordered_mapHashMapmap, 无序,hash
multiset集合,允许重复元素
multimapmap,允许重复的 key
unordered_multiset无序允许重复元素集合
unordered_multimap无序允许重复 key 的 map
LinkedHashSet按照插入顺序,支持 hash 查找
LinkedHashMap按照插入顺序,支持 hash 查找
HashTable类似 HashMap,效率略低
bitsetBitSet位操作

HashTable & HashMap

The HashMap class is roughly equivalent to Hashtable, except that it is asynchronized and permits nulls.

Review java - Array

Declaring an array

Both are allow

string argc[];
string[] argc;

Int array

int[] nums = new int[7];
nums[0] = 10;
Rabbit[] racers = new Rabbit[10];//10 empty rabbit array;
racers[0] = new Rabbit("B","F");

arraycopy()

System.arraycopy(nums, 0, nums, 0, nums.length);

Array vs ArrayList

  1. An array needs to know its size at the time of creation, arrayList does not.
  2. To assign an object in array you must assign it to a specific index.
  3. Array use array syntax ([]).
  4. ArrayList is parameterized. ArrayList<string>

ArrayList

ArrayList<Flower> flowerList = new ArrayList<Flower>();// <- ArrayList constructor
Flower f = new Flower();
Flower m = new Flower();

flowerList.add(f);
flowerList.add(m);

Review java - javadoc

JavaDocs

JavaDocs is able to easily generate a code “maintenance manual”

A doc comment is made up of two parts – a description and two or more tag.

/**
* Here is description
*
* @tag Comment for tag
*/

Tags

  • @author (classes and interfaces only, required)
  • @version (classes and interfaces only, required)
  • @param (methods and constructors only)
  • @return (methods only)
  • @exception
  • @see
  • @since
  • @serial (or @serialField or @serialData)
  • @deprecated

JDK Docs

External documentation can be created with javadoc

Review java - OO

Class & Object

A class only exists at compile time;

An object only exists at runtime.

Data Encapsulation

Data Encapsulation/information hiding: where the internal state and operation are hidden from others.

The more information Class A knows about Class B, the greater the possibility that changing Class A will adversely affect Class B. In an ideal world, making internal changes to Class A should have no, or very little, effect on other classes.

Review java - Variable

Basic types

Every type have a default value:

TypeRepresentationInitial valueStorageMax. value
bytesinged integer08 bits127
shortsinged integer016 bits32767
intsinged integer032 bits2147483647
longsinged integer064 bitsover 10^18
floatfloating point0.032 bitsover 10^38
doublefloating point0.064 bitsover 10^308
booleantrue or falsefalse1 bit
charUNICODE (not ASCII)u000016 bitsuFFFF

Difference between i++ and ++i

b = 1;
a = b++; // a = 1; b = 2
b = 1;
a = ++b; // a = 2; b = 2
int a = 1;
int res = a++ + a;
// res = 3 ; a = 2.
int a = 1;
int res = a + a++;
// res = 2; a = 2.
int a = 1;
int res = ++a + a;
// res = 4; a = 2.
int a = 1;
int res = a + ++a;
// res = 3; a = 2.

For loop

for(int i=0; i < n ; i++)

MarkDown overview

This is learning note for MarkDown

Heading 1

or

Heading 1

Heading 2

or

Heading 2

Italic and bold

italic or italic bold or bold italic and bold or italic and bold

Unordered list

  • line 1
  • line 2
  • line 3
    • sub 1
    • sub 2

or

  • line 1
  • line 2
  • line 3
    • sub 1
    • sub 2

Ordered list

  1. line 1
  2. line 2
    1. sub 1
    2. sub 2
  3. line 3

BlockQuotes

Use ‘>’ for a quotes

Welcome to Jekyll!

You’ll find this post in your _posts directory. Go ahead and edit it and re-build the site to see your changes. You can rebuild the site in many different ways, but the most common way is to run jekyll serve, which launches a web server and auto-regenerates your site when a file is updated.

To add new posts, simply add a file in the _posts directory that follows the convention YYYY-MM-DD-name-of-post.ext and includes the necessary front matter. Take a look at the source for this post to get an idea about how it works.