您的位置:首页 > 编程语言 > C语言/C++

Java的List模板类型与C++的list模板类型的比较

2010-07-15 13:03 246 查看
一点工作心得,理解的也未必正确,不对的话大家尽管指出。



Java的List只是一个容器接口,定义List必须用一些实现类如ArrayList来实现。

对List使用需要理解几个关键点:

①List的add()方法只能添加对象,无法添加基本数据类型!

②List不会自己去开辟空间来复制add()添加进来的对象,只是存这个对象的地址,用的时候再去查找对象取需要的值。所以将一个对象添加进List,这个对象在外部被修改后,List取值也会跟着变化。

③List允许重复添加对象,并且也为重复对象也开辟一个变量保存地址。

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
public class Demo {
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		List<int[]> list = new ArrayList<int[]>();
		for (int i = 0; i < 3; i++) {
			int[] value = new int[3];
			for (int j = 0; j < 2; j++) {
				value[j] = j;
			}
			value[2] = i;
			list.add(value);
		}
		// 遍历List成员的成员
		Iterator<int[]> itr = list.iterator();
		while (itr.hasNext()) {
			int[] object = itr.next();
			for (int i = 0; i < object.length; i++) {
				System.out.print(object[i]);
				System.out.print(", ");
			}
			System.out.print("/n");
		}
	}
}






我们可以拿Java的List与C++标准模板库的list做一下对比。

①下面是我从别处找来的一段代码,有兴趣的读者可以把添加对象的代码提出来,专门做成一个变量添加,然后再修改这个变量,最后打印输出,会发现C++的list是保存了push进去的对象,而不是像Java一样存的是地址。

②C++的list也允许重复添加。

#include <iostream>
#include <list>
#include <string>
#include <algorithm>
using namespace std;
class employee {
private:
	long ID; //雇员编号
	string name; //雇员名称
	float salary; //雇员薪水
public:
	// 构造函数
	employee(long ID, string name, float salary) :ID(ID), name(name), salary(salary) {}
	// 成员变量的set(),get()方法
	long getID() const {
		return ID;
	}
	string getName() const {
		return name;
	}
	float getSalary() const {
		return salary;
	}
	void setID(long id) {
		(*this).ID = id;
	}
	void setName(string name){
		(*this).name = name;
	}
};
// 薪水比较
bool lessThan(const employee &a) {
	return (a.getSalary() <= 8000);
}
bool greaterThan(const employee &a) {
	return (a.getSalary() > 8000);
}
typedef list<employee> EMPLOYEE_LIST;
typedef list<employee>::iterator EMPLOYEE_IT;
typedef list<employee>::reverse_iterator REVERSE_EMPLOYEE_IT;
//output_list函数正向输出list容器对象内的所有元素
void output_list(EMPLOYEE_LIST employ) {
	EMPLOYEE_IT employit;
	for (employit = employ.begin(); employit != employ.end(); employit++) {
		cout << (*employit).getID() << '/t' << (*employit).getName() << '/t'
				<< (*employit).getSalary() << endl;
	}
}
//reverse_output_list函数逆向输出list 容器对象内的所有元素
void reverse_output_list(EMPLOYEE_LIST employ) {
	REVERSE_EMPLOYEE_IT employit;
	for (employit = employ.rbegin(); employit != employ.rend(); employit++) {
		cout << (*employit).getID() << '/t' << (*employit).getName() << '/t'
				<< (*employit).getSalary() << endl;
	}
}
int main(int argc, char* argv[]) {
	EMPLOYEE_LIST employ; //Create A Empty Queue
	EMPLOYEE_IT employit; //Create A Random Access Iterator
	//下面的四条语句分别构造一个employee对象,并插入到list容器对象
	employ.push_back(employee(100, "张三", 10000));
	employ.push_back(employee(101, "李四", 8000));
	employ.push_back(employee(102, "王五", 8800));
	employ.push_front(employee(108, "大郎", 5000));
	//正向输出list容器对象的所有元素
	output_list(employ);
	cout << "-----------------------------------------" << endl;
	//按compare谓词删除容器中的元素
	employ.remove_if(lessThan);
	output_list(employ);
	cout << "-----------------------------------------" << endl;
	//逆向输出list容器对象的所有元素
	reverse_output_list(employ);
	//统计并输出工资高于8000的员工人数
	int i = count_if(employ.begin(), employ.end(), greaterThan);
	cout << "工资高于8000元的有" << i << "位!" << endl;
	return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: