STL Cheat Sheet - C++ vs Java

STL Cheat Sheet - C++ vs Java

Standard modules are implemented differently or same things are done differently in different languages. One says add() while another says push_back().

This is a cheatsheet for myself to remember how to do things in diffrent languages. I made this when I was a fresh grad and used to use both C++ and Java to solve problems. Hope this helps someone.

Feel free to recommend modifications or add things.

Topic

C++

Java

List

-   vector <int> v;

-   v.push_back(10);

-   v.pop_back();

-   v.size();

-   v.empty();

-   v.at(0)/v[0]

-   ArrayList<Integer> list = new ArrayList<Integer>();

-   list.add(10);

Sort/Reverse Array

-   sort/reverse(arr, arr+n)

-   sort/reverse(arr, arr+n, comp)

-   Arrays.sort()

-   No reverse of Array

Sort/Reverse List

-   sort/reverse(v.begin(), v,end())

-    Collections.sort/reverse(list)

-   Collections.sort(list, Collections.reverseOrder());

-   Collections.sort(al, new MyComparator());

Comparator

bool function(T a, T b){

  return a < b;

}

class MyComparator implements Comparator<T>{

    public int compare(T a, T b){return a.val - b.val;}

}

Operator Overloading

bool operator < (T other) const {

}

// oops, no such thing

class T implements Comparable <T> {

            @Override

            public int compareTo(T ob) { return Integer.compare(T.val, this.val);}

 

};

Iterator

for(vector<int>::iterator i=v.begin(); i!=v.end(); i++) {

  cout << *i << endl;

  cout << i->val << endl;

}

for(Integer i : list) {

}

Set

Auto Sorted!

-   set <int> s;

-   s.insert(10);

 

Queue

-   queue <int> q;

-   q.push(10)

-   q.pop()

-   q.front()

-   q.back()

-   Queue<Integer> q = new LinkedList<>();

-   q.add(10);

-   q.remove();

-   q.peek();

-   q.size();

-   q.isEmpty();

Priority Queue

-   Object must be comparable

-   priority_queue <int> q;

-   q.push(10)

-   q.pop()

-   q.top()

-   q.empty()

-   Object must be comparable

-   Queue<Integer> q = new PriorityQueue<Integer>();

-   q.add(10);

-   q.remove();

-   q.peek();

-   q.size();

-   q.isEmpty();

Stack

-   stack <int> s;

-   s.push(10);

-   s.pop(); // no return

-   s.top()

-   s.size()

-   s.empty()

-   Stack<Integer> s = new Stack<Integer>();

-   s.push(10);

-   s.pop()

-   s.peek()

-   s.empty()

String

-   str [0]

-   str.at(0)

-   str.length()

 

 

Author

Mehedi Hasan Masum

Posted on

2018-01-07

Licensed under

CC BY-NC-SA 4.0

Comments

Your browser is out-of-date!

Update your browser to view this website correctly.&npsb;Update my browser now

×