c++ 中的集合類

心鑫發表於2014-03-22
概述


list


/*
 * array.cpp
 *
 *  Created on: 2014-3-21
 *      Author: Administrator
 */
#include <iostream>
#include <vector>
#include <deque>
#include <list>
#include <cstdlib>
using namespace std;
// compare only integral part:
bool mycomparison(double first, double second) {
	return (int(first) > int(second));
}

void displayList(std::list<double> first) {
	std::cout << "displayList:";
	for (std::list<double>::iterator it = first.begin(); it != first.end();
			++it)
		std::cout << ' ' << *it;
	std::cout << '\n';
}
int merge() {
	std::list<double> first, second;

	first.push_back(3.1);
	first.push_back(2.2);
	first.push_back(2.9);
	second.push_back(3.7);
	second.push_back(7.1);
	second.push_back(1.4);
	displayList(first);
	displayList(second);

	first.sort();
	second.sort();
	displayList(first);
	displayList(second);

	first.merge(second);
	displayList(first);
	displayList(second);
	// (second is now empty)

	second.push_back(2.1);
	first.merge(second,mycomparison); //2.1 1.4 2.2 2.9 3.1 3.7 7.1
	displayList(first);
	//引數要求明明是個模板類為什麼傳方法也可以呢
	first.sort(mycomparison);
	displayList(first);

	return 0;
//	displayList: 3.1 2.2 2.9
//	displayList: 3.7 7.1 1.4
//	displayList: 2.2 2.9 3.1
//	displayList: 1.4 3.7 7.1
//	displayList: 1.4 2.2 2.9 3.1 3.7 7.1   結果是排序的
//	displayList:
//	displayList: 1.4 2.2 2.9 2.1 3.1 3.7 7.1 結果是沒排序的

}

void push() {
	list<int> list1(2, 1);
	cout << list1.size() << endl;
	list1.insert(list1.begin(), 10);
	list1.push_back(2);
	list1.push_back(3);
	list1.push_back(4);
	for (list<int>::iterator iter = list1.begin(); iter != list1.end();
			iter++) {
		cout << *iter << endl;
	}
	cout << list1.size() << endl;
	cout << list1.max_size() << endl;
}
int assign() //分配新的內容到列表容器,取代它的當前內容,並相應地修改其大小。
{
	std::list<int> first;
	std::list<int> second;

	first.assign(7, 100); // 7 ints with value 100

	second.assign(first.begin(), first.end()); // a copy of first

	int myints[] = { 1776, 7, 4 };
	first.assign(myints, myints + 3); // assigning from array

	std::cout << "Size of first: " << int(first.size()) << '\n';
	std::cout << "Size of second: " << int(second.size()) << '\n';
	return 0;
}

int splice ()// 拼接;
{
  std::list<int> mylist1, mylist2;
  std::list<int>::iterator it;

  // set some initial values:
  for (int i=1; i<=4; ++i)
     mylist1.push_back(i);      // mylist1: 1 2 3 4

  for (int i=1; i<=3; ++i)
     mylist2.push_back(i*10);   // mylist2: 10 20 30

  it = mylist1.begin();
  ++it;                         // points to 2

  mylist1.splice (it, mylist2); // mylist1: 1 10 20 30 2 3 4
                                // mylist2 (empty)
                                // "it" still points to 2 (the 5th element)

  mylist2.splice (mylist2.begin(),mylist1, it);
                                // mylist1: 1 10 20 30 3 4
                                // mylist2: 2
                                // "it" is now invalid.
  it = mylist1.begin();
  std::advance(it,3);           // "it" points now to 30

  mylist1.splice ( mylist1.begin(), mylist1, it, mylist1.end());
                                // mylist1: 30 3 4 1 10 20

  std::cout << "mylist1 contains:";
  for (it=mylist1.begin(); it!=mylist1.end(); ++it)
    std::cout << ' ' << *it;
  std::cout << '\n';

  std::cout << "mylist2 contains:";
  for (it=mylist2.begin(); it!=mylist2.end(); ++it)
    std::cout << ' ' << *it;
  std::cout << '\n';

  return 0;
}
int main_list() {
//	push();
//	merge();
//	assign();
	splice();
	return 0;
}



set

/*
 * set.cpp
 *
 *  Created on: 2014-3-22
 *      Author: Administrator
 */
// set::insert (C++98)
#include <iostream>
#include <set>
void displaySet(std::set<int> myset) {
	std::cout << "myset contains:";
	for (std::set<int>::iterator it = myset.begin(); it != myset.end(); ++it)
		std::cout << ' ' << *it;
	std::cout << '\n';
}
int get_allocator() {
	std::set<int> myset;
	int * p;
	unsigned int i;

	// allocate an array of 5 elements using myset's allocator:
	//get_allocator 實際返回的是 class allocator
	p = myset.get_allocator().allocate(5);

	// assign some values to array
	for (i = 0; i < 5; i++)
		p[i] = (i + 1) * 10;

	std::cout << "The allocated array contains:";
	for (i = 0; i < 5; i++)
		std::cout << ' ' << p[i];
	std::cout << '\n';

	displaySet(myset);
	myset.get_allocator().deallocate(p, 5);
	displaySet(myset);
	return 0;
}

void insert() {
	std::set<int> myset;
	std::set<int>::iterator it;
	std::pair<std::set<int>::iterator, bool> ret;
	// set some initial values:
	for (int i = 1; i <= 5; ++i)
		myset.insert(i * 10); // set: 10 20 30 40 50
	ret = myset.insert(20); // no new element inserted
	if (ret.second == false)
		it = ret.first; // "it" now points to element 20

	myset.insert(it, 25); // max efficiency inserting
	myset.insert(it, 24); // max efficiency inserting
	myset.insert(it, 26); // no max efficiency inserting
	int myints[] = { 5, 10, 15 }; // 10 already in set, not inserted
	myset.insert(myints, myints + 3);
	displaySet(myset);
}
int key_comp() {
	std::set<int> myset;
	int highest;

	std::set<int>::key_compare mycomp = myset.key_comp();

	for (int i = 0; i <= 5; i++)
		myset.insert(i * 10);

	std::cout << "myset contains:";

	highest = *myset.rbegin();
	std::cout << highest << std::endl;
	std::set<int>::iterator it = myset.begin();
	do {
		std::cout << ' ' << *it;
	} while (mycomp(*(++it), highest));

	//myset.key_comp() 通過的是_Compare,_Compare是 struct less 的別名,less過載了()操作符
	//方法內部因沒有訪問內部的私有成員,所有不必用友元的方式,
	//總之就是比較大小,引數1<引數2返回真

//  typedef _Compare key_compare;

//  typename _Compare = std::less<_Key>,

//  struct less : public binary_function<_Tp, _Tp, bool>
//    {
//      bool
//      operator()(const _Tp& __x, const _Tp& __y) const
//      { return __x < __y; }
//    };

	std::cout << '\n';

	return 0;
}
int main_set() {
//	insert();
//	key_comp();
	get_allocator();
	return 0;
}





map


/*
 * map.cpp
 *
 *  Created on: 2014-3-22
 *      Author: Administrator
 */
#include <iostream>
#include <map>

bool fncomp (char lhs, char rhs) {return lhs<rhs;}

struct classcomp {
  bool operator() (const char& lhs, const char& rhs) const
  {return lhs<rhs;}
};

void map1() {
	std::map<char, int> first;
	first['a'] = 10;
	first['b'] = 30;
	first['c'] = 50;
	first['d'] = 70;
	std::map<char, int> second(first.begin(), first.end());
	std::map<char, int> third(second);
	std::map<char, int, classcomp> fourth; // class as Compare
	bool (*fn_pt)(char, char) = fncomp;
	std::map<char, int, bool (*)(char, char)> fifth(fn_pt); // function pointer as Compare
}

int main ()
 {
	map1();

  return 0;
}











相關文章