comparision: C++ vs java

 

 

C++

Java

Create instance

Obj a();

Obj* a = new a();

Obj a = new a();

Create array

int a[3];

int a[] = {1, 2, 3};

int[] a = new int[3];

int a[] = new int[3];

int[] a = new int[]{1, 2, 3};

foreach

for each(E element in collection)

for(E element : collection)

==

Compare value(need override)

Compare address

string

variable

constant

 

Vector List and String

 

C++

Java

vector<>

string

List

String

Empty?

vec.empty()

list.isEmpty()

Size

vec.size()   /    length()(Only string)

list.size()

str.length()

Read element

vec[i]

list.get(i);

str.charAt(i);

Write element

list.set(i, newElement)

N/A

First/Last element

vec.front()/back()

N/A

Add element at end

vec.push_back(e)

list.add(e)

N/A

Add element at i

vec.insert(vec.begin()+i, e)

list.add(i, e)

N/A

Add range

vec.insert(vec.begin()+i,b.begin(), b.end())

list.add(b)

list.add(i, b)

N/A

Remove

vec.erase(vec.begin()+i)

list.remove(i)

N/A

Substring

N/A

s.substr(start, size)

N/A

s.substring(start,end)

 

Map and Set

 

C++

Java

map

set

 

Map

Set

HashType

unordered_map

unordered_set

HashMap

HashSet

TreeType

map

set

不允许重复元素

从小到大排序

TreeMap

TreeSet

Has e?

m.count(key)

s.count(e)

m.containsKey(key)

s.contains(e)

Add

m[key]=val

s.insert(e)

m.put(key,val)

s.add(e)

Remove

m.erase(key)

s.erase(key)

可以直接删除,因为没有重复的key值

s.erase(key)

可以直接删除,因为没有重复的key值

m.remove(key)

s.remove(e)

Iterator

map<int,int>::iterator iter=m.begin();

iter++;

set<int>::iterator iter=s.begin();

iter++;

Iterator<Map.Entry<A,B>> it=m.entrySet().iterator();

Map.Entry<A,B> entry=it.next();

entry.getKey();

entry.getValue();

Iterator<A> it = s.iterator;

val = it.next();

m.keySet();

m.values();

 

 

 

 

Queue and Deque

 

C++

Java

queue

priority_queue

deque

Queue

PriorityQueue

Deque

Create

queue<T>

(based on deque)

priority_queue<T>

priority_queue

<T,vector<T>,cmp>

(based on vector)

(Max queue by default)

Deque<T>

(Basic

Container)

Queue<T> queue=new LinkedList<T>()

/ArrayList<T>()

Queue<T> a = new PriorityQueue<T>();

(Min queue by default)

Deque<T> deque = new LinkedList<T>();/ArrayDeque<T>();

 

Add front

N/A

push(e)

push_front()

N/A

offer(e)

offerFirst(e)

Add back

push(e)

push_back()

offer(e)

offerLast(e)

Remove front

pop()

pop_front()

poll()

//remove and return first

pollFirst()

Remove back

N/A

pop_back()

N/A

pollLast()

Get first

front()                 top()

peek()

peekFirst()

Get last

back()               不能读取队尾

N/A

peekLast()

Others

N/A

Max queue:

priority_queue<int>

N/A

Overide compareTo:

N/A

Min queue:

priority_queue

<int,deque<int>, greater<int> >

Use comparator:

struct cmp{

operatorbool ()(int x, int y){

return x > y; // min

}

};

 

priority_queue<int, vector<int>, cmp> p;

Use comparator:

PriorityQueue<T,cmp>();

Override < operator :

struct node{

int x, y;

friend booloperator< (node a, node b){

return a.x > b.x; //low x high p

}

};

priority_queue<node>q;

//auto i , auto& i 有什么差别
unordered_map<Key,T>::iterator it;
(*it).first;             // the key value (of type Key)
(*it).second;            // the mapped value (of type T)
(*it);                   // the "element value" (of type pair<const Key,T>)

for(auto& i : mapname)  一般这么隐式调用

unordered_map<Key,T> + priority_queue :可以实现排序

 

priority_queue referrrence :https://blog.csdn.net/xiaoquantouer/article/details/52015928

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值