Awesome Hacks!

プログラミング初心者なので地道に勉強していきます。分からない人の立場から整理していきます。

STL_list

【プログラム】

#include <iostream>
#include <list>
#include <functional>	// graterを使うためにinclude
using namespace std;

int main()
{
	std::list<int> testList;  // int型の双方向リスト

	testList.push_back(34);		cout << "34を末尾に入れました" << endl;
	testList.push_back(222);	cout << "222を末尾に入れました" << endl;
	testList.push_back(674);	cout << "674を末尾に入れました" << endl;
	testList.pop_back();		cout << "末尾を削除しました" << endl;
	testList.push_front(145);	cout << "145を先頭に入れました\n" << endl;

	for(list<int>::iterator it = testList.begin(); it != testList.end(); ++it){
		cout << *it << endl;
	}

	cout << "\n" << endl;

	std::list<int> sortList;  // int型の双方向リスト
	
	sortList.push_back(5);	cout << "5を末尾に入れました" << endl;
	sortList.push_back(2);	cout << "2を末尾に入れました" << endl;
	sortList.push_back(6);	cout << "6を末尾に入れました" << endl;
	sortList.push_back(8);	cout << "8を末尾に入れました" << endl;
	sortList.push_front(1);	cout << "1を先頭に入れました" << endl;
	sortList.push_back(4);	cout << "4を末尾に入れました" << endl;
	
	cout << "\n昇順に並べ替えます" << endl;
	sortList.sort();
	for(list<int>::iterator it = sortList.begin(); it != sortList.end(); ++it){
		cout << *it << " ";
	}
	cout << "\n" << endl;
	cout << "降順に並べ替えます" << endl;
	sortList.sort(greater<int>());
	for(list<int>::iterator it = sortList.begin(); it != sortList.end(); ++it){
		cout << *it << " ";
	}
	cout << "\n" << endl;

	return 0;
}


【実行結果】

$ testList
34を末尾に入れました
222を末尾に入れました
674を末尾に入れました
末尾を削除しました
145を先頭に入れました

145
34
222


5を末尾に入れました
2を末尾に入れました
6を末尾に入れました
8を末尾に入れました
1を先頭に入れました
4を末尾に入れました

昇順に並べ替えます
1 2 4 5 6 8 

降順に並べ替えます
8 6 5 4 2 1 

$