// merge()の使用例
#include < iostream>
#include < list>
using namespace std;
int main()
{
list< int> lst1, lst2;
int i;
for(i=0; i<10; i+=2)
lst1.push_back(i);
for(i=1; i<11; i+=2)
lst2.push_back(i);
cout << "Original size of lst1: ";
cout << lst1.size() << "\n";
cout << "Original contents of lst1:\n";
list< int>::iterator p = lst1.begin();
while(p != lst1.end())
cout << *p++ << " ";
cout << "\n\n";
cout << "Original size of lst2: ";
cout << lst2.size() << "\n";
cout << "Original contents of lst2:\n";
p = lst2.begin();
while(p != lst2.end())
cout << *p++ << " ";
cout << "\n\n";
// 2つのリストを混合する
lst1.merge(lst2);
cout << "Size of lst1 after merge: ";
cout << lst1.size() << "\n";
cout << "Merged contents of lst1:\n";
p = lst1.begin();
while(p != lst1.end())
cout << *p++ << " ";
cout << "\n\n";
cout << "lst2 is now empty, its size is ";
cout << lst2.size() << "\n";
return 0;
}
// リストがソートされていないと混合は正しく行われない
#include < iostream>
#include < list>
using namespace std;
int main()
{
list< int> lst1, lst2;
int i;
lst1.push_back(2);
lst1.push_back(0);
lst1.push_back(8);
lst1.push_back(4);
lst1.push_back(6);
for(i=1; i<11; i+=2)
lst2.push_back(i);
cout << "Original contents of lst1:\n";
list< int>::iterator p = lst1.begin();
while(p != lst1.end())
cout << *p++ << " ";
cout << "\n\n";
cout << "Original contents of lst2:\n";
p = lst2.begin();
while(p != lst2.end())
cout << *p++ << " ";
cout << "\n\n";
// この操作は失敗する
lst1.merge(lst2);
cout << "Contents of lst1 after failed merge:\n";
p = lst1.begin();
while(p != lst1.end())
cout << *p++ << " ";
cout << "\n\n";
return 0;
}
// 降順に混合する
#include < iostream>
#include < list>
using namespace std;
int main()
{
list< int> lst1, lst2;
int i;
for(i=10; i>=0; i-=2)
lst1.push_back(i);
for(i=11; i>=1; i-=2)
lst2.push_back(i);
cout << "Original contents of lst1:\n";
list< int>::iterator p = lst1.begin();
while(p != lst1.end())
cout << *p++ << " ";
cout << "\n\n";
cout << "Original contents of lst2:\n";
p = lst2.begin();
while(p != lst2.end())
cout << *p++ << " ";
cout << "\n\n";
// "<"ではなくgreater()を使って混合する
lst1.merge(lst2, greater< int>());
cout << "Merged contents of lst1:\n";
p = lst1.begin();
while(p != lst1.end())
cout << *p++ << " ";
cout << "\n\n";
return 0;
}
// 結合の例
#include < iostream>
#include < list>
#include < string>
#include < algorithm>
using namespace std;
int main()
{
list< string> sentence;
list< string> phrase;
list< string>::iterator p;
string s1[] = {
"occur", "at", ""
};
string s2[] = {
"Splicing", "can", ""
};
string s3[] = {
"or", "the", "end.", ""
};
string s4[] = {
"the", "front,", "the", "middle,", ""
};
int i;
// 初期状態の文を作成する
for(i=0; s1[i]!=""; i++)
sentence.push_back(s1[i]);
// 追加する最初の句を作成する
for(i=0; s2[i]!=""; i++)
phrase.push_back(s2[i]);
cout << "Original sentence:\n";
p = sentence.begin();
while(p != sentence.end())
cout << *p++ << " ";
cout << "\n\n";
// 先頭に結合する
sentence.splice(sentence.begin(), phrase);
cout << "Sentence after splicing at the front:\n";
p = sentence.begin();
while(p != sentence.end())
cout << *p++ << " ";
cout << "\n\n";
// 次の句を作成する
for(i=0; s3[i]!=""; i++)
phrase.push_back(s3[i]);
// 末尾に結合する
sentence.splice(sentence.end(), phrase);
cout << "Sentence after splicing at the end:\n";
p = sentence.begin();
while(p != sentence.end())
cout << *p++ << " ";
cout << "\n\n";
// 最後の句を作成する
for(i=0; s4[i]!=""; i++)
phrase.push_back(s4[i]);
// "or" の前に句を結合する
p = find(sentence.begin(), sentence.end(), "or");
sentence.splice(p, phrase);
cout << "Sentence after splicing in the middle:\n";
p = sentence.begin();
while(p != sentence.end())
cout << *p++ << " ";
return 0;
}
// リストをソートする
#include < iostream>
#include < list>
#include < cstdlib>
using namespace std;
int main()
{
list< int> lst;
int i;
// ランダムな整数のリストを作成する
for(i=0; i<10; i++)
lst.push_back(rand());
cout << "Original contents:\n";
list< int>::iterator p = lst.begin();
while(p != lst.end()) {
cout << *p << " ";
p++;
}
cout << "\n\n";
// リストをソートする
lst.sort();
cout << "Sorted contents:\n";
p = lst.begin();
while(p != lst.end()) {
cout << *p << " ";
p++;
}
return 0;
}
// remove()の使用例
#include < iostream>
#include < list>
using namespace std;
int main()
{
list< int> lst;
list< int>::iterator p;
int i;
for(i=0; i<20; i++) lst.push_back(i%3);
cout << "Original list: ";
for(p=lst.begin(); p!=lst.end(); p++)
cout << *p << " ";
cout << "\n\n";
lst.remove(1); // すべての1を削除する
cout << "Modified list: ";
for(p=lst.begin(); p!=lst.end(); p++)
cout << *p << " ";
cout << "\n\n";
return 0;
}
// unique()の使用例
#include < iostream>
#include < list>
using namespace std;
int main()
{
list< int> lst;
list< int>::iterator p;
for(int i=0; i<5; i++)
for(int j=0; j<3; j++) lst.push_back(i);
cout << "Orignal list: ";
for(p=lst.begin(); p!=lst.end(); p++)
cout << *p << " ";
cout << "\n\n";
lst.unique(); // 連続する重複要素を削除する
cout << "Modified list: ";
for(p=lst.begin(); p!=lst.end(); p++)
cout << *p << " ";
cout << "\n\n";
return 0;
}
// reverse()を使った回文テスター
#include < iostream>
#include < list>
using namespace std;
char phrases[][80] = {
"Madam, I'm Adam.",
"Able was I ere I saw Elba.",
"A man, a plan, a canal: Panama!",
"This is not one.",
""
};
int main()
{
list< char> pal;
int i, j;
list< char>::iterator p;
for(i=0; *phrases[i]; i++) {
for(j=0; phrases[i][j]; j++)
pal.push_back(phrases[i][j]);
cout << "Phrase # " << i << " forward: ";
p = pal.begin();
while(p != pal.end()) cout << *p++;
cout << "\n";
// 関係のない文字を削除する
pal.remove(',');
pal.remove('.');
pal.remove('!');
pal.remove(':');
pal.remove('\'');
pal.remove(' ');
cout << "Phrase # " << i << " after deletions: ";
p = pal.begin();
while(p != pal.end()) cout << *p++;
cout << "\n";
pal.reverse(); // リストを反転する
cout << "Phrase # " << i << " backward: ";
p = pal.begin();
while(p != pal.end()) cout << *p++;
cout << "\n\n";
pal.clear(); // 次の文を試す準備
}
return 0;
}
// メールアドレスをリストに格納する
#include < iostream>
#include < list>
#include < string>
using namespace std;
class maillist {
string name;
string street;
string city;
string state;
string zip;
public:
maillist() {
name = street = city = state = zip = "";
}
maillist(string n, string s, string c,
string st, string z) {
name = n;
street = s;
city = c;
state = st;
zip = z;
}
string getname() { return name; }
string getcity() { return city; }
string getstreet() { return street; }
string getstate() { return state; }
string getzip() { return zip; }
};
// リストを名前でソートするために必要
bool operator<(maillist &a, maillist &b)
{
return a.getname() < b.getname();
}
// リストから名前を検索するために必要
bool operator==(maillist &a, maillist &b)
{
return a.getname() == b.getname();
}
// リストを画面に表示する
void display(list< maillist> &lst)
{
list< maillist>::iterator p;
for(p=lst.begin(); p!=lst.end(); p++) {
cout << p->getname() << ": ";
cout << p->getstreet() << ", ";
cout << p->getcity() << ", ";
cout << p->getstate() << " ";
cout << p->getzip() << "\n";
}
}
int main()
{
list< maillist> mlstA, mlstB;
mlstA.push_back(maillist("James, Tom",
"1102 W. Henry St",
"Mission", "TX", "78572"));
mlstA.push_back(maillist("Newton, Sid",
"55 Oscar Blvd",
"Kirksville", "MO", "63501"));
mlstA.push_back(maillist("Henson, Erick",
"908 Trunk Ave",
"Peoria", "IL", "61615"));
mlstA.push_back(maillist("Ewen, Heidi",
"43645 N. Broadway #4",
"Idaho Falls", "ID", "83401"));
mlstA.push_back(maillist("Mount, W. C.",
"78A Wothington Rd",
"Berkeley", "CA", "94710"));
mlstB.push_back(maillist("Williams, Don",
"197 NorthRidge Dr",
"Walworth", "WI", "53184"));
mlstB.push_back(maillist("Newton, Sid",
"55 Oscar Blvd",
"Kirksville", "MO", "63501"));
mlstB.push_back(maillist("Ewen, Heidi",
"43645 N. Broadway #4",
"Idaho Falls", "ID", "83401"));
mlstB.push_back(maillist("Weston, George",
"5464 Woodbury Ct",
"Baker", "LA", "70714"));
mlstA.sort();
mlstB.sort();
// メーリングリストを混合する
mlstA.merge(mlstB);
cout << "List A after sorting and merging.\n";
display(mlstA);
cout << "List A now has " << mlstA.size();
cout << " entries.\n\n";
// 重複要素を削除する
mlstA.unique();
cout << "List A after removing duplicates.\n";
display(mlstA);
cout << "List A now has " << mlstA.size();
cout << " entries.\n\n";
return 0;
}