STL学习(二)set、map、list、deque简单Demo

几个Demo,《C++标准程序库》上的例子,书里面的STL貌似是SGI版本的,在vc上有的好多跑不动,在suse10上可以跑起来。

set的简单应用

#include <iostream>
#include <set>
using namespace std;

int main(int, char *[])
{
	set<int, greater<int> > c;
	for (int i=1; i<7; i++)
	{
		c.insert(i);
	}

	//use lower_bound upper_bound equal_range
	cout<<"lower_baund(3): "<<*c.lower_bound(3)<<endl;
	cout<<"upper_bound(3): "<<*c.upper_bound(3)<<endl;
	cout<<"equal_range(3): "<<*c.equal_range(3).first<<" "<<*c.equal_range(3).second<<endl;

	//define variable for return value of insert()
	pair<set<int, greater<int> >::iterator, bool> status;
	int value = 7;
	status = c.insert(value);

	//process return value
	if(status.second)
	{
		cout<<value<<" inserted as element"<<endl;
	}
	else
	{
		cout<<value<<" already exits as element"<<endl;
	}
	
	//iterate over all elements and print them
	set<int, greater<int> >::iterator pos;
	for (pos = c.begin(); pos != c.end(); ++pos)
	{
		cout<< *pos<<" ";
	}
	cout<<endl;

	
	set<int, greater<int> > c2(c);
	//set<int> c2(c.begin(), c.end());


	//erase all elements with passed value
	int num;
	num = c.erase(9);
	cout<<num<<" elements removed"<<endl;


	//empty multiset 
	multiset<int> multi_c;

	//insert element
	multi_c.insert(1);
	multi_c.insert(1);

	//remove first value with passed value
	multiset<int>::iterator iter;
	iter = multi_c.find(1);
	if(iter != multi_c.end())
	{
		multi_c.erase(iter);
	}

	//print element of multi_c
	copy(multi_c.begin(), multi_c.end(), ostream_iterator<int>(cout, " "));
	cout<<endl;
	

	return 0;
}


multiset的简单Demo

#include <iostream>
#include <set>
using namespace std;

int main(int, char *[])
{
	typedef multiset<int, greater<int> > IntSet;

	IntSet coll;

	//insert elements in random order
	coll.insert(4);
	coll.insert(3);
	coll.insert(5);
	coll.insert(1);
	coll.insert(6);
	coll.insert(2);
	coll.insert(5);

	//iterate over all elements and print them
	IntSet::iterator pos;
	for (pos = coll.begin(); pos != coll.end(); ++pos)
	{
		cout<< *pos <<' ';
	}
	cout<<endl;

	//insert 4 again and process return value
	IntSet::iterator ipos = coll.insert(4);
	cout<< "4 inserted as element "<<distance(coll.begin(), ipos) + 1<<endl;

	//print all elements of the copy
	copy(coll.begin(), coll.end(), ostream_iterator<int>(cout, " "));
	cout<<endl;

	//assign elements to another multiset with ascending order
	//multiset<int> coll2(coll.begin(), coll.end());

	//remove all elements with value 4
	int num;
	num = coll.erase(4);
	cout<<num<<" elements(s) removed"<<endl;

	//print all elements of the copy
	copy(coll.begin(), coll.end(), ostream_iterator<int>(cout, " "));
	cout<<endl;

	return 0;
}


multimap的Demo

#include <iostream>
#include <map>
#include <string>
#include <iomanip>
using namespace std;

int main(int, char *[])
{
	// define multimap type as string/string dictionary
	typedef multimap<string, string> StrStrMMap;

	//create empty dictionary
	StrStrMMap dict;

	//insert some elements in random order
	dict.insert(make_pair(string("day"), string("Tay")));
	dict.insert(make_pair(string("strange"), string("fremd")));
	dict.insert(make_pair(string("car"), string("Auto")));
	dict.insert(make_pair(string("smart"), string("elegant")));
	dict.insert(make_pair(string("tarit"), string("Merkmal")));
	dict.insert(make_pair(string("strange"), string("seltsam")));
	dict.insert(make_pair(string("smart"), string("klug")));
	dict.insert(make_pair(string("clever"), string("raffiniert")));

	//print all elements
	StrStrMMap::iterator pos;
	cout.setf(ios::left, ios::adjustfield);
	cout<<' '<<setw(10)<<"english "<<"german "<<endl;
	cout<<setfill('-')<<setw(20)<<""<<setfill(' ')<<endl;
	for (pos = dict.begin(); pos != dict.end(); ++pos)
	{
		cout<<' '<<setw(10)<<pos->first.c_str()<<pos->second<<endl;
	}
	cout<<endl;

	// print all values for key "smart"
	string word("smart");
	cout<<word<<": "<<endl;
	for(pos = dict.lower_bound(word); pos != dict.upper_bound(word); ++pos)
	{
		cout<<"  "<<pos->second<<endl;
	}

	//print all keys for value "raffiniert"
	word = ("raffiniert");
	cout<<word<<": "<<endl;
	for (pos = dict.begin(); pos != dict.end(); ++pos)
	{
		if(pos->second == word)
		{
			cout<<"  "<<pos->first<<endl;
		}
	}

	//search an element with key "smart"
	//logarithmic complexity
	pos = dict.find(string("smart"));
	if (pos != dict.end())
	{
		cout<<pos->first<<" "<<pos->second<<endl;
	}


	return 0;
}


list 的Demo

#include <iostream>
#include <list>
#include <algorithm>
using namespace std;

void printLists(const list<int>& l1, const list<int>& l2)
{
	cout<<"list1 : ";
	copy(l1.begin(), l1.end(), ostream_iterator<int>(cout, " "));
	cout<<endl<<"list2 : ";
	copy(l2.begin(), l2.end(), ostream_iterator<int>(cout, " "));
	cout<<endl<<endl;
}


int main(int, char *[])
{
	//create two empty lists
	list<int> list1;
	list<int> list2;

	//fill both lists with elements
	for (int i=0; i<6; ++i)
	{
		list1.push_back(i);
		list2.push_front(i);
	}
	printLists(list1, list2);

	//insert all elements of list1 before the first element with value 3 of list 2
	list2.splice(find(list2.begin(), list2.end(), 3), list1);
	printLists(list1, list2);

	//move first element to the end
	list2.splice(list2.end(), list2, list2.begin());
	printLists(list1, list2);

	//sort second list, assign to list1 and remove duplicates
	list2.sort();
	list1 = list2;
	list2.unique();
	printLists(list1, list2);

	//merge both sorted lists into the first list
	list1.merge(list2);
	printLists(list1, list2);

	return 0;
};


deque 的Demo

#include <iostream>
#include <deque>
#include <string>
#include <algorithm>
using namespace std;

int main(int, char *[])
{
	//create empty deque of strings
	deque<string> coll;

	//insert several elements
	coll.assign(3, string("string"));
	coll.push_back("last string");
	coll.push_front("first string");

	//print elements separated by newlines
	copy(coll.begin(), coll.end(),
		ostream_iterator<string>(cout, "\n"));
	cout<<endl;

	//remove first and last element
	coll.pop_front();
	coll.pop_back();

	//insert "another" into every element but the first
	for(int i=1; i<coll.size(); ++i)
	{
		coll[i] = "another " + coll[i]; 
	}

	//change size to four elements
	coll.resize(4, "resized string");

	//print elements separated by new lines
	copy(coll.begin(), coll.end(),
		ostream_iterator<string>(cout, "\n"));

	return 0;
}


 

你可能感兴趣的:(STL学习(二)set、map、list、deque简单Demo)