一、集合类
对象的集合,指如链表,数组,队列这一类的数据结构。在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()
|