移植Java代码到C++的技巧一

  一、关联性容器(map vs TreeMap):

  我们在用Java进行编程时经常会用到Map这样的容器接口,而它的具体实现类一般来自于TreeMap和HashMap,前者提供了有序性,而后者则在存取效率方面有着一定的优势。相比于Java,C++的STL也提供了map容器,其功能等同于Java中的TreeMap,至于HashMap,它并没有进入C++标准模板库。而幸运的是,现有的主流C++编译器供应商均提供了标准模板库的扩展包,并在该扩展包中实现了hash_map容器类,其接口标准和STL中的容器基本一致。

  在本条目中我们将主要列举TreeMap(Java)和map(C++)在设计上的主要差别,以及关键功能实现上的明显差异,并基于此给出在实际代码移植过程中该如何应对这些差异所带来的诸多问题:

  1、插入相同键时两种容器表现不同,其中Java的TreeMap会将后插入的键值对直接覆盖容器中已经存在的键值对,而C++中的map容器则恰恰相反,一旦容器中该键已经存在,那么新插入的键值对将不会被存入该容器对象中,见如下Java代码及输出结果。

  public class MyTest { private static void testGet() { TreeMap<String,String> tm = new TreeMap<String,String>(); tm.put(“Hello”, “World”); if (tm.get(“Hello1”) == null) System.out.println(“Hello1 is NOT found.”); } public static void main(String[] args) { TreeMap<Integer,Integer> tm = new TreeMap<Integer,Integer>(); tm.put(5, 10); tm.put(5, 20); Set<Entry<Integer,Integer>> entries = tm.entrySet(); Iterator<Entry<Integer,Integer>> it = erator(); while (it.hasNext()) { Entry<Integer,Integer> e = it.next(); System.out.printf(“key = %d, value = %d\n”,e.getKey().intValue(),e.getValue().intValue()); } } } //The count of the TreeMap is 1 //key = 5, value = 20

  见如下C++代码及输出结果。

  using namespace std; int main() { map<int,int> m; m.insert(make_pair(5,10)); m.insert(make_pair(5,20)); printf(“The count of the map is %d.\n”,m.size()); map<int,int>::iterator it = m.begin(); for (; it != m.end(); ++it) { printf(“key = %d, value = %d\n”,(*it).first,(*it).second); } return 0; } //The count of the map is 1 //key = 5, value = 10

  从以上结果便可以清晰的看出二者之间的差别,那么我们又该如何修改C++的代码,以使其和原有的Java代码逻辑保持一致呢?现在答案已经非常清楚了,我们只需在插入前先行判断该键是否存在,如果存在则删除已有键值对,之后再将新键值对插入到该容器对象中,或者通过已有键的迭代器访问该键的值,并赋予新值。至于该使用哪种方式,则需要依据实际情况而定,通常而言,第一种方法可以应对所有场景,但是效率相比于第二种方法要略低一些。见如下修改后的C++代码:

  using namespace std; int main() { map<int,int> m; m.insert(make_pair(5,10)); map<int,int>::iterator it = m.find(5); if (it != m.end()) m.erase(it); m.insert(make_pair(5,20)); //或实现为以下方式 //if (it != m.end()) // it->second = 20; //else // m.insert(make_pair(5,20)); printf(“The count of the map is %d.\n”,m.size()); it = m.begin(); for (; it != m.end(); ++it) printf(“key = %d, value = %d\n”,(*it).first,(*it).second); return 0; } //The count of the map is 1 //key = 5, value = 10

  2、在Java中,TreeMap的参数类型必须为对象类型,不能为原始数据类型,如int、double等,否则编译器将报编译错误。而在C++中,对模参的类型则没有此类限制。如果当Java源代码中TreeMap的值参数类型为自定义对象时,那么在C++中与其对应的值模参类型很有可能为该自定义对象的指针类型,否则将只能是该对象类型本身,这样在插入对象时,就会有对象copy的动作发生,从而对性能造成一定的负面影响,通常而言,我们会选择使用该类的指针类型作为该容器的值模参类型。见如下Java代码:

  public class MyTest { private int _value; public MyTest(int value) { _value = value; } public static void main(String[] args) { TreeMap<Integer,MyTest> tm = new TreeMap<Integer,MyTest>(); tm.put(5, new MyTest(10)); System.out.println(“The count of the TreeMap is ” + tm.size()); Set<Entry<Integer,MyTest>> entries = tm.entrySet(); Iterator<Entry<Integer,MyTest>> it = erator(); while (it.hasNext()) { Entry<Integer,MyTest> e = it.next(); System.out.printf(“key = %d, value = %d\n”,e.getKey().intValue(),e.getValue()._value); } } } //The count of the TreeMap is 1 //key = 5, value = 10

  以上为Java的完整代码和输出结果。如果我们仍然用该种方式完成C++的代码实现,这样正确吗?答案是输出结果是一致的,但是在C++代码中却存在着一个极为低级的失误。对,内存泄露。见如下C++代码及代码注释:

  using namespace std; class MyTest { public: MyTest(int value) { _value = value; } ~MyTest() {} public: int getValue() const { return _value; } private: int _value; }; int main() { map<int,MyTest*> m; m.insert(make_pair(5,new MyTest(10))); map<int,MyTest*>::iterator it = m.find(5); printf(“The count of the map is %d.\n”,m.size()); it = m.begin(); for (; it != m.end(); ++it) printf(“key = %d, value = %d\n”,(*it).first,(*it).second->getValue()); return 0; } //The count of the map is 1 //key = 5, value = 10

  在执行以上代码之后,如果借助于Valgrind(Linux gcc)或BoundChecker(Visual C++)等内存检测工具,便可以清楚的看到插入的MyTest对象指针在程序退出之前没有被正常释放,从而导致了内存泄露。见如下修订后的C++代码:

  //该宏是我在实际项目中经常用到的工具宏之一 #define RELEASE_MAP(Type1,Type2,variable) \ do {\ map<Type1,Type2*>::iterator it = (variable).begin();\ for (; it != (variable).end(); ++it) \ delete (it->second); \ (variable).clear(); \ } while(0) int main() { map<int,MyTest*> m; m.insert(make_pair(5,new MyTest(10))); map<int,MyTest*>::iterator it = m.find(5); printf(“The count of the map is %d.\n”,m.size()); it = m.begin(); for (; it != m.end(); ++it) printf(“key = %d, value = %d\n”,(*it).first,(*it).second->getValue()); RELEASE_MAP(int,MyTest,m); return 0; }

  修订后代码的输出结果与之前的代码是完全相同的,只是容器中对象指针的内存泄露问题就此得以解决。然而这种类似的内存资源管理问题给你带来的困扰,将会在整个代码移植的过程中贯穿始末。有的时候我甚至直接忽略了内存资源泄露问题,并将C++的代码写的和Java中的源码完全一致,直到某一时刻才突然惊醒的发现,之后就是不得不沮丧的进行重构,按照C++的机制和习惯方式重新实现该部分逻辑。

  这里我们将不再就此类问题做过多的赘述和解释,后面会有一个单独的条目来演示和分析该类问题。

  3、作为有序的关联性容器,键对象的排序机制自然是一个无法回避的问题,但幸运的是,尽管这两种容器在某些处理细节上存在一定的差异,然而整个排序机制的思路却是如出一辙,可以这样说,它们是神似而形不似,而造成“形不似”的主要原因则主要来自于这两种语言的本身。下面我们先介绍Java中TreeMap的键对象排序机制。

  1)一种常用的方式是让键对象成为Comparable<T>接口的实现类,这样TreeMap的内部则可以利用接口方法compareTo()的返回值来判断该键对象在容器中所在的位置。见如下代码:

  public class MyTest implements Comparable<MyTest> { private int key; public MyTest(int key) { this.key = key; } @Override public boolean equals(Object o) { if (!(o instanceof MyTest)) return false; MyTest t = (MyTest)o; System.out.println(“equals of MyTest is called here.”); return key == t.key; } @Override public int compareTo(MyTest o) { return key – o.key; } public static void main(String[] args) { TreeMap<MyTest,Integer> tm = new TreeMap<MyTest,Integer>(); tm.put(new MyTest(5), 5); tm.put(new MyTest(2), 2); tm.put(new MyTest(10), 10); tm.put(new MyTest(10), 20); for (Entry<MyTest,Integer> e : tm.entrySet()) System.out.println(“Key = ” + e.getKey().key + “, Value = ” + e.getValue()); } } //compareTo of MyTest is called here.   //compareTo of MyTest is called here.   //compareTo of MyTest is called here.   //compareTo of MyTest is called here.   //Key = 2, Value = 2   //Key = 5, Value = 5   //Key = 10, Value = 20

  从输出结果可以看出,MyTest对象在被插入到容器时,该类覆盖实现的compareTo方法被多次调用,而且最终的排序结果也是和compareTo方法中的逻辑完全吻合。这里还需要额外指出的是,TreeMap和C++中的map一样,都是不允许重复键的插入,从而保证容器对象中已有键对象的唯一性。这是如何实现的呢?在Java中,当compareTo函数返回0的时候表示两个对象是相等的,我们从上例中可以看出,通常用于JDK对象相等性比较的equals函数并没有被调用,由此可以进一步确认,键对象的相等性比较完全是由compareTo函数来决定的。在此方面,C++中的map容器亦采用了相同的设计理念,只是在细节处理方面不同且更为灵活。我们将在本条目的后面给出更具体的说明。

  2)另外一种方式是在构造TreeMap对象时传入Comparator<T>接口的实现类,而该实现类的compare方法将帮助TreeMap对象来确定如何排序容器中的键对象。见如下代码:

  public class MyTest { private int key; public MyTest(int key) { this.key = key; } public static void main(String[] args) { TreeMap<MyTest,Integer> tm = new TreeMap<MyTest,Integer>(new Comparator<MyTest>() { @Override public int compare(MyTest first,MyTest second) { System.out.println(“compare of Comparator is called here.”); return first.key – second.key; } }); tm.put(new MyTest(5), 5); tm.put(new MyTest(2), 2); tm.put(new MyTest(10), 10); for (Entry<MyTest,Integer> e : tm.entrySet()) System.out.println(“Key = ” + e.getKey().key + “, Value = ” + e.getValue()); } } //compare of Comparator is called here.   //compare of Comparator is called here.   //Key = 2, Value = 2   //Key = 5, Value = 5   //Key = 10, Value = 10

  从上面的输出结果可以看出,Comparator匿名实现类的compare方法确实决定了TreeMap容器中键对象的排序顺序。

  现在是时候该展示一下C++中map容器的排序机制了,这里我们先看一下map的类声明形式。

  template <class Key, class Type, class Traits = less<Key>, class Allocator=allocator<pair <const Key, Type> > > class map

  其中前两个模参分别对应于键对象类型和值对象类型,这一点和Java中TreeMap是一样的。第三个模板参数则在map容器键对象的排序规则上扮演着极为重要的角色。至于最后一个参数,主要用于对象的分配机制,由于和本条目关系不大,我们可以直接略过它。

  1)从该类的模参声明中可以看到STL的设计者为模参Traits提供了缺省类型参数less<Key>,该类型仍为模板类,其模参类型等同于map中的键类型。这里我们先看一下less<Key>的声明。

  1 template<class _Ty>2 struct less : public binary_function<_Ty, _Ty, bool> {3 bool operator()(const _Ty& _Left, const _Ty& _Right) const {4 return (_Left < _Right);5 }6 };

  该类只有一个公有方法,即圆括号操作符重载函数,在C++中通常称这种类型的类为函数类。该方法的实现逻辑要求模参类型必须具备“小于”的可比较性,对于原始数据类型这是没有问题的,如int,short,float等。相比而言,对于自定义的类来说,则必须重载小于操作符。见如下C++代码:

  using namespace std; class MyTest { public: MyTest(int value) { _value = value; } ~MyTest() {} public: bool operator< (const MyTest& o) const { printf(“operator < of MyTest is called here.\n”); return _value – o._value < 0; } public: int getValue() const { return _value; } private: int _value; }; int main() { map<MyTest,int> m; MyTest t5(5); MyTest t2(2); MyTest t10(10); m.insert(make_pair(t5,5)); m.insert(make_pair(t2,2)); m.insert(make_pair(t10,10)); map<MyTest,int>::iterator it = m.begin(); printf(“The count of the map is %d.\n”,m.size()); for (; it != m.end(); ++it) printf(“key = %d, value = %d\n”,(*it).first.getValue(),(*it).second); return 0; } //operator < of MyTest is called here.   //operator < of MyTest is called here.   //operator < of MyTest is called here.   //operator < of MyTest is called here.   //operator < of MyTest is called here.   //The count of the map is 3   //key = 2, value = 2   //key = 5, value = 5   //key = 10, value = 10

  从上面的输出结果可以看到MyTest的小于操作符重载函数被多次调用,而且该函数的逻辑也确实应用于map容器对象的排序中。有意思的是我们仅仅插入三个键值对到该map容器,然而该函数却被调用5次,这是为什么呢?很简单,和Java的compareTo方法的返回值不同,该方法的返回值为布尔类型,只能表示小于或者不小于,无法标明新对象的键值和容器中已经存在对象的键值是否相等。为了弥补这一问题,在新对象被插入到容器中的正确位置之前,map容器的insert方法需要从正反两个方向将新元素的键值与容器中部分键对象进行比较,如果在与同一个对象进行两次比较之后均返回true,则证明该键值已经存在,否则将新对象插入指定的位置。

  2)在上面的示例代码中,map<MyTest,int>类型的键类型为MyTest对象类型,因此在每次插入新数据时都会产生一次键对象的拷贝构造,从而影响了插入效率。如果插入的是对象指针类型,则可以避免对象copy的发生,然而不幸的是,less<Key>会将对象指针视为整型,因此MyTest的小于操作符重载将不会被调用,那么我们也就无法得到我们期望的排序结果,因为less<Key>只是基于指针地址做了简单而毫无意义的整数比较。那么该如何修订这一问题呢?见如下代码示例。

  using namespace std; class MyTest { public: MyTest(int key) { _key = key; } ~MyTest() {} public: int _key; }; struct MyTestComparator { bool operator()(MyTest* first,MyTest* second) const { printf(“operator() of MyTestComparator is called here.\n”); return first->_key – second->_key < 0; } }; using namespace std; int main() { MyTest* mt0 = new MyTest(0); MyTest* mt2 = new MyTest(2); MyTest* mt1 = new MyTest(1); map<MyTest*,int,MyTestComparator> mtMap; mtMap.insert(make_pair(mt0,0)); mtMap.insert(make_pair(mt2,2)); mtMap.insert(make_pair(mt1,1)); printf(“The count of map is %d.\n”,mtMap.size()); map<MyTest*,int,MyTestComparator>::iterator it = mtMap.begin(); for (; it != mtMap.end(); ++it) { printf(“key = %d, value = %d\n”,(*it).first->_key,(*it).second); delete (*it).first; } return 0; } //operator() of MyTestComparator is called here.   //operator() of MyTestComparator is called here.   //operator() of MyTestComparator is called here.   //operator() of MyTestComparator is called here.   //operator() of MyTestComparator is called here.   //operator() of MyTestComparator is called here.   //operator() of MyTestComparator is called here.   //operator() of MyTestComparator is called here.   //The count of the map is 3   //key = 0, value = 0   //key = 1, value = 1   //key = 2, value = 2

  在上面的代码中我们并没有使用map中缺省的less<Key>对象来比较容器中的键对象,而是自己重新定义了一个类来专门比较MyTest*类型的键。这一点有些类似于Java中的Comparator<T>接口。

  最后我们简单的总结一下Java和C++的泛型技术,由于C++是在语言设计之初就已经将模板和泛型等特征考虑了进来,而Java则是半路出家,因此C++在这方面的支持也就更灵活,更具伸缩性,在运行时也更加高效。

当你成功得意的时候,最重要的是瞧得起别人。

移植Java代码到C++的技巧一

相关文章:

你感兴趣的文章:

标签云: