生活随笔
收集整理的這篇文章主要介紹了
C++标准库与Java基础类对照表
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
一、集合類
對象的集合,指如鏈表,數(shù)組,隊列這一類的數(shù)據(jù)結(jié)構(gòu)。在C++標(biāo)準(zhǔn)庫中把這些成為Container, 在Java中稱為Collection。
C++STL常用Container:?vector, list, map, set, queue, stack, priority_queue
Java基礎(chǔ)類:ArrayList, HashMap, HashSet
注意:
在C++中采用了模板適應(yīng)各種類型。Java中沒有模板,由于各個對象都是派生自O(shè)bject類, 元素會自動向上轉(zhuǎn)換。Java的這種機制可能會引起一些非法的類型轉(zhuǎn)換,從而導(dǎo)致程序錯誤。當(dāng)從Java的Collection取對象時,返回的是Object對象,因此需要程序員進行顯式的向下轉(zhuǎn)換。另外,Java的Collection不支持基本類型,解決辦法是利用基本包裝類(如Integer, Float, Double等)。
在JDK 5.0中加入了泛型類型,形式上有點像C++的模板,例如定義ArrayList<String> v, 這種的語法清晰地顯示了v是存放String的容器。Java的泛型編程超出本文范圍,請參閱相關(guān)資料。
?考慮到兼容性的問題,本文對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 ) |
| 元素個數(shù) | 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(); ?//向下轉(zhuǎn)換 } |
| 獲取 | 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)實現(xiàn),Java的HashMap多用散列(hash)的方法實現(xiàn)。
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”) 判斷個數(shù) map<string,int>::iterator it; it = mapTest.find(“hello”); int x = (*it).second; ? | 判斷存在: ?? mapTest.containsKey() 獲取 Integer x = (Integer)mapTest.get(“hello”) |
?
二、算法
????下面講述的C++的泛型算法只適用于隨機訪問的容器,如數(shù)組,vector等。對List和Map不使用。List有自己一套算法方法。
| | C++ | Java |
| 定義 | vector<string> vec; //假設(shè)已插入數(shù)據(jù) vector<string>::iterator it; | ArrayList vec = new ArrayList(); //假設(shè)已插入數(shù)據(jù) Iterator it = vec.iterator(); |
| 排序 | sort(vec.begin(), vec.end()) | Collections.sort(vec); |
| 查找 | find(vec.begin(), vec.end(), “hello”) ? | 二分查找: int pos = Collections.binarySearch(vec, “hello”); |
| 復(fù)制 | int ia[] = {1,2,3,4,5} vector<int> vec; copy(ia, ia+5, back_inserter(vec)); | ? |
三、字符串
??? C++采用string類,Java采用String類。java的String類一經(jīng)初始化就不能改變,每次增加或刪除字符時會構(gòu)建一個新的String類,在頻繁改變操作中會大量內(nèi)存耗費。可變的String應(yīng)采用StringBuffer類。
| | C++ | Java |
| 定義 | string str = “cpp string”; string str(“hello”); 轉(zhuǎn)換為C風(fēng)格字符串 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); 正則表達(dá)式匹配 str.matches(“^#(.)+$”); |
| 子串 | str.substr(5,6) //第5個字符,長度6 | str.substring(5,8) //第5到第8個字符 分割: String str = “CHN,JPN,RUS”; String vecstr[]; vecstr = str.split(“,”); 結(jié)果為String數(shù)組 “CHN”, “JPN”, “RUS” |
| 查找 | int idx = str.find(“cpp”); if( idx != string::npos ) { ?... ...?//找到位置 } 查找第一個出現(xiàn)0~9字符的位置 int idx = find_first_of(str, “1234567890”); | int idx = str.indexOf(“cpp”); if( idx != -1) { ?.. ... } |
| 清空 | str.clear() | str.clear() |
總結(jié)
以上是生活随笔為你收集整理的C++标准库与Java基础类对照表的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。