一、集合類
對象的集合,指如鏈表,數組,隊列這一類的數據結構。在C++標準庫中把這些成為Container, 在Java中稱為Collection。
C++STL常用Container:?vector, list, map, set, queue, stack, priority_queue
Java基礎類:ArrayList, HashMap, HashSet
注意:
在C++中采用了模板適應各種類型。Java中沒有模板,由于各個對象都是派生自Object類, 元素會自動向上轉換。Java的這種機制可能會引起一些非法的類型轉換,從而導致程序錯誤。當從Java的Collection取對象時,返回的是Object對象,因此需要程序員進行顯式的向下轉換。另外,Java的Collection不支持基本類型,解決辦法是利用基本包裝類(如Integer, Float, Double等)。
在JDK 5.0中加入了泛型類型,形式上有點像C++的模板,例如定義ArrayList<String> v, 這種的語法清晰地顯示了v是存放String的容器。Java的泛型編程超出本文范圍,請參閱相關資料。?考慮到兼容性的問題,本文對Collection的定義沒有采用泛型的形式。
vector與ArrayList
C++ | Java | |
定義 | vector<int> vTest(50) | ArrayList vTest = new ArrayList() |
追加 | int x = 5; vTest.push_back( x ) | Integer x = new Interger(5) vTest.add( x ) |
元素個數 | vTest.size() | vTest.size() |
判斷空 | vTest.empty() // TRUE為空 | vTest.isEmpty() |
插入 | vector<int>::iterator iter; int y = 15; iter = vTest.begin(); vTest.insert(iter+4, y) | int y = 15; vTest.add( 5, y) |
刪除 | vTest.erase( vTest.begin() ) | vTest.remove(5) |
迭代 | vector<int>::iterator iter; iter = vTest.begin(); int xx = 0; while( iter != vTest.end() ) { ?xx = *iter; ?iter++; } | iterator it = vTest.iterator(); //取得第一個 while( it.hasNext() ) { ?? Integer xx = (Integer) it.next(); ?//向下轉換 } |
獲取 | vTest[i] 或?vTest.at(i) | 讀取?vTest.get(i) 寫入?vTest.set(i, new Integer(15)); |
清空 | vTest.clear() | vTest.clear() |
list與LinkedList
???? 雙向鏈表。
map與HashMap
????????C++ STL中的map一般是用紅黑樹(RB-Tree)實現,Java的HashMap多用散列(hash)的方法實現。
Java的HashMap一般都要重載equals()和hashCode()方法 。
??
C++ | Java | |
定義 | map<string, int> mapTest | HashMap mapTest = new HashMap() |
插入 | mapTest[ string(“hello”) ] = 1; 或者 typedef map<string,int>::value_type valType; string str = “hello”; mapTest.insert(valType(str, 1)); | ? mapTest.put(“hello”, new Integer(1) ); |
查找 | mapTest.count(“hello”) 判斷個數 map<string,int>::iterator it; it = mapTest.find(“hello”); int x = (*it).second; ? | 判斷存在: ?? mapTest.containsKey() 獲取 Integer x = (Integer)mapTest.get(“hello”) |
?
二、算法
????下面講述的C++的泛型算法只適用于隨機訪問的容器,如數組,vector等。對List和Map不使用。List有自己一套算法方法。
C++ | Java | |
定義 | vector<string> vec; //假設已插入數據 vector<string>::iterator it; | ArrayList vec = new ArrayList(); //假設已插入數據 Iterator it = vec.iterator(); |
排序 | sort(vec.begin(), vec.end()) | Collections.sort(vec); |
查找 | find(vec.begin(), vec.end(), “hello”) ? | 二分查找: int pos = Collections.binarySearch(vec, “hello”); |
復制 | int ia[] = {1,2,3,4,5} vector<int> vec; copy(ia, ia+5, back_inserter(vec)); | ? |
三、字符串
??? C++采用string類,Java采用String類。java的String類一經初始化就不能改變,每次增加或刪除字符時會構建一個新的String類,在頻繁改變操作中會大量內存耗費。可變的String應采用StringBuffer類。
C++ | Java | |
定義 | string str = “cpp string”; string str(“hello”); 轉換為C風格字符串 str.c_str(); | String str = “java string” |
追加 | str = str + “world”; str.append(“world”); | str = “hello” + “world”; str.concat(“world”); |
長度 | str.length() | str.length(); |
比較 | str.compare(otherstr); 判斷相等 str == otherstr | str.CompareTo(otherstr); 判斷相等: str.equals(otherstr); 正則表達式匹配 str.matches(“^#(.)+$”); |
子串 | str.substr(5,6) //第5個字符,長度6 | str.substring(5,8) //第5到第8個字符 分割: String str = “CHN,JPN,RUS”; String vecstr[]; vecstr = str.split(“,”); 結果為String數組 “CHN”, “JPN”, “RUS” |
查找 | int idx = str.find(“cpp”); if( idx != string::npos ) { ?... ...?//找到位置 } 查找第一個出現0~9字符的位置 int idx = find_first_of(str, “1234567890”); | int idx = str.indexOf(“cpp”); if( idx != -1) { ?.. ... } |
清空 | str.clear() | str.clear()
|