c++的list(合并两个有序数组)

实现C++标准库中list的merge函数(以 l i s t < i n t > list list<int>为例)

/**
C++的list实现  2020.05.29(未完)
Author: 豆奶
**/
  
/**

功能:将l1和l2合并到l1, 并清空l2.
输入:l1, l2是两个已经排好序的list,类型:list
**/
void sorted_merge(list<int>& l1, list<int>& l2) {
	list<int>::iterator plist, plist2; 
    if ( l1.empty() ){
		l1.clear();
		for(plist2 = l2.begin(); plist2 != l2.end(); plist2++)
			l1.push_back(*plist2);
		l2.clear();
		return;
	}
	else if(l2.empty()) {return;}
	plist = l1.begin();
	plist2 = l2.begin();
	while(plist!=l1.end()){
		if (*plist > *plist2)
			swap(*plist, *plist2);
		*plist++;
	}
	for(plist = l2.begin(); plist != l2.end(); plist++)
		l1.push_back(*plist);
	l2.clear();
	return;
}

你可能感兴趣的:(C++学习)