34. iter = theList.begin();
while ( iter != theList.end() )
cout << *iter+
+ << ' ';
*iter+
+ ๋ฌธ๋ฒ์ ํฌ์ธํฐ์ ๊ฐ๋ค.
4.4.2 Data Insertion
์๋์ LISTFILL ์์ ๋ฅผ ํตํด ์ฝํ ์ด๋์ ์๋ฆฌ๋จผํธ๊ฐ ์ด๋ฏธ ์กด์ฌํ๋ ๊ณณ์ ๋ฐ์ดํฐ๋ฅผ ์ฝ์ ํ๋
๊ณผ์ ์ ์ดํด๋ณด์.
// listfill.cpp
// uses iterator to fill list with data
#include <iostream>
#include <list>
using namespace std;
int main()
{
list<int> iList(5); // empty list holds 5 ints
list<int>::iterator it; // iterator
int data = 0;
// fill list with data
for (it=iList.begin(); it != iList.end(); it+
+)
*it = data +
= 2;
// display the list
for (it=iList.begin(); it != iList.end(); it+
+)
cout << *it << ' ';
cout << endl;
return 0;
}
์ฒซ๋ฒ์งธ for ๋ฃจํ๋ ์ฝํ ์ด๋์ int ๊ฐ์ 2, 4, 6, 8, 10์ผ๋ก ์ฑ์ด๋ค. ๊ทธ๋ฆฌ๊ณ ๋๋ฒ์งธ for
๋ฃจํ๋ ์ด ๊ฐ๋ค์ ๋ณด์ฌ์ค๋ค.
35. 4.4.3 Algorithms and Iterators
์ฐ๋ฆฌ๊ฐ ์๊ธฐํ๋ ์๊ณ ๋ฆฌ์ฆ๋ค์ ์ธ์๋ก ์ดํฐ๋ ์ดํฐ๋ฅผ ์ด์ฉํ๋ค. ๊ทธ๋ฆฌ๊ณ ๋๋ก ๊ฐ์ ๋ฆฌํดํ๊ธฐ๋
ํ๋ค. ITERFIND ์์ ๋ find() ์๊ณ ๋ฆฌ์ฆ์ด ๋ฆฌ์คํธ์ ์ ์ฉ๋๋ ๊ฑธ ๋ณด์ฌ์ค๋ค. (์ด๋ฏธ ์๊ณ ์๊ฒ ์ง๋ง
find()์๊ณ ๋ฆฌ์ฆ์ ์ค์ง input ์ดํฐ๋ ์ดํฐ๋ง ํ์ํ๊ธฐ๋๋ฌธ์ list์ ํจ๊ป ์ฐ์ผ ์ ์๋ค.)
// ITERFIND.cpp
// find() returns a list iterator
#include <iostream>
#include <list>
#include <algorithm>
using namespace std;
int main()
{
list<int> iList (5); // empty list holds 5 ints
list<int>::iterator it; // iterator
int data = 0;
// fill list with data
for (it=iList.begin(); it != iList.end(); it+
+)
*it = data + 2; // 2, 4, 6, 8, 10
it = find (iList.begin(), iList.end(), 8);
if (it != iList.end())
cout << "nFound 8.n";
else
cout << "nDid not find 8.n";
return 0;
}
find() ์๊ณ ๋ฆฌ์ฆ์ 3๊ฐ์ ์ธ์๋ฅผ ์ทจํ๋ค. ์ฒซ๋ฒ์งธ์ ๋๋ฒ์งธ ์ธ์๋ ๊ฒ์๋ ๋ฒ์๋ฅผ ๋ํ๋ด๋
์ดํฐ๋ ์ดํฐ์ด๊ณ , ์ธ๋ฒ์งธ ์ธ์๋ ์ฐพ์ ๊ฐ์ด๋ค. find()๊ฐ iList.end()๋ฅผ ๋ฆฌํดํ๋ค๋ฉด ๊ฐ์ ์ฐพ์ง ๋ชปํ๊ณ
๋์ ๋๋ฌํ ๊ฒ์ด๊ณ ๊ทธ๋ ์ง ์๋ค๋ฉด ๊ทธ ๊ฐ์ด ์๋ ๊ณณ์ ๊ฐ๋ฆฌํค๊ณ ์์ ๊ฒ์ด๋ค. ์์ ๊ฒฐ๊ณผ๋
์๋์ ๊ฐ๋ค.
Found 8.
56. // multiset์ person ์ค๋ธ์ ํธ๋ฅผ ์ถ๊ฐํ๋ค.
persSet.insert (pers1); persSet.insert (pers2);
persSet.insert (pers3); persSet.insert (pers4);
persSet.insert (pers5); persSet.insert (pers6);
persSet.insert (pers7); persSet.insert (pers8);
cout << "nNumber of entries = " << persSet.size();
// multiset์ content๋ฅผ ์ถ๋ ฅํ๋ค.
iter = persSet.begin();
while (iter != persSet.end())
(*iter+
+).display();
// lastName๊ณผ firstName์ ์ ๋ ฅ๋ฐ๋๋ค.
string searchLastName, searchFirstName;
cout << "nnEnter last name of person to search for: ";
cin >> searchLastName;
cout << "Enter first name: ";
cin >> searchFirstName;
// ์ ๋ ฅ๋ฐ์ ์ด๋ฆ์ผ๋ก searchPerson ์ค๋ธ์ ํธ๋ฅผ ์์ฑํ๋ค.
person searchPerson (searchLastName, searchFirstName, 0);
// ํด๋น ์ด๋ฆ์ ๊ฐ์ง ์ฌ๋์ด ๋ช๋ช ์ธ๊ฐ ์ธ์ด๋ณธ๋ค.
int cntPersons = persSet.count (searchPerson);
cout << "Number of persons with this name = " << cntPersons;
// ํด๋น ์ด๋ฆ์ ๊ฐ์ง ์ฌ๋๋ค์ ๋ชจ๋ ์ถ๋ ฅํ๋ค.
iter = persSet.lower_bound (searchPerson);
while ( iter != persSet.upper_bound (searchPerson) )
(*iter+
+).display ();
cout << endl;
return 0;
}
57. person ํด๋์ค๊ฐ STL ์ฝํ ์ด๋๋ก ์๋๋๊ธฐ ์ํด์๋ ๋ช๊ฐ์ง ๊ณตํต์ผ๋ก ์ฐ์ด๋ ๋ฉค๋ฒํจ์๊ฐ
ํ์ํ๋ค. ๋ฐ๋ก ๊ธฐ๋ณธ์์ฑ์, < ์คํผ๋ ์ดํฐ, == ์คํผ๋ ์ดํฐ๋ค์ด ํ์ํ๋ค. ์ด ๋ฉค๋ฒํจ์๋ค์
๋ฆฌ์คํธ ํด๋์ค์ ์ฌ๋ฌ ์๊ณ ๋ฆฌ์ฆ์์ ์ ์ฉํ๊ฒ ์ฌ์ฉ๋๋ค. ๋ํ ํน์ํ ๊ฒฝ์ฐ๋ฅผ ์ํด
ํ์ํ ๋ฉค๋ฒํจ์๋ฅผ ์ ์ํ ์๋ ์๋ค. ์คํผ๋ ์ดํฐ < ์ == ๋ const ์ธ์์ด์ด์ผ ํ๊ณ
friend๋ก ํ๋๊ฒ ์ข๋ค (๋ฉค๋ฒํจ์๋ก ํด๋ ์๊ด์ ์๋ค).
์ค๋ฒ๋ก๋๋ < ์คํผ๋ ์ดํฐ๋ set์ ์ ์ฅ๋ ๋ ์ ๋ ฌ๋ฐฉ์์ ๋ํ๋ธ๋ค. ์์ setpers์์๋
last name์ ์ ๋ ฌํ๊ธฐ์ํด ์ฌ์ฉ๋์๊ณ , last name์ด ๋์ผํ๋ฉด first name์ผ๋ก ์์๋ฅผ ์ ํ๊ฒ
๋ง๋ค์๋ค. ์ ํ๋ก๊ทธ๋จ์ ๊ฒฐ๊ณผ๋ ์๋์ ๊ฐ๋ค.
Number of entries = 8
Bartoski, Peter phone: 6946473
Deauville, William phone: 8435150
Fredericks, Roger phone: 7049982
KuangThu, Bruce phone: 4157300
McDonald, Amanda phone: 8435150
McDonald, Stacey phone: 3327536
McDonald, Stacey phone: 7764987
Wellington, Jhon phone: 9207400
Enter last name of person to search for: McDonald
Enter first name: Stacey
Number of persons with name = 2
McDonald, Stacey phone: 3327536
McDonald, Stacey phone: 7764987
(K:VC++6.0์์ ์์ ์์ค๋ friendํจ์๊ฐ ํด๋์ค์ ๋ฉค๋ฒ๋ณ์๋ฅผ ์ฌ์ฉํ ์ ์๋ค๋ ์๋ฌ๊ฐ
๋ฐ์ํ๋๋์ ์์๋ณด๋๊น VC+
+์ ๋ฒ๊ทธ๋ผ๊ณ ํฉ๋๋ค. ํด๊ฒฐ๋ฐฉ๋ฒ์ ์๋น์คํฉ 5๋ฅผ ๊น์๊ฑฐ๋
.NET์ ์ฌ์ฉํ์๋ฉด ๋ฉ๋๋ค. SP5๋ 2003/4 ํ์ฌ ์๋ ์ฃผ์๋ก๋ถํฐ ๋ฐ์ผ์ค ์ ์์ต๋๋ค.
http://www.microsoft.com/downloads/details.aspx?FamilyID=9066d31d -ba23-4e8a-b7c8-
b95f5e54f896&DisplayLang=ko#filelist)
58. 6.2 A List of person Objects
์์ SETPERS ์์ ์์, set์ด๋ multimap์ผ๋ก ์ฃผ์ด์ง ์ด๋ฆ์ ๊ฒ์ํ๋ ๊ฒ์ ๋งค์ฐ ๋น ๋ฅด๋ค.
๊ทธ๋ ์ง๋ง, person ์ค๋ธ์ ํธ๋ฅผ ๋น ๋ฅด๊ฒ ์ฝ์ ํ๊ณ ์ญ์ ํ๊ธฐ๋ฅผ ๊ณ ๋ คํ๋ค๋ฉด list์ ์ฌ์ฉ์ ๊ณ ๋ คํด์ผํ๋ค.
// listpers.cpp // uses a list to hold person objects
#include <iostream>
#include <list>
#include <algorithm>
#include <string>
using namespace std;
class person
{
private:
string lastName;
string firstName;
long phoneNumber;
public:
person () : lastName ("blank"),
firstName ("blank"), phoneNumber (0L)
{ }
person (string lana, string fina, long pho) :
lastName (lana), firstName (fina), phoneNumber (pho)
{ }
friend bool operator < (const person&, const person&);
friend bool operator == (const person&, const person&);
friend bool operator != (const person&, const person&);
friend bool operator > (const person&, const person&);
void display () const { // display person's data
cout << endl << lastName << ",t" << firstName
<< "ttphone: " << phoneNumber;
}
long get_phone () const {
return phoneNumber;
}
};
59. bool operator < (const person& p1, const person& p2)
{
if (p1.lastName == p2.lastName)
return (p1.firstName < p2.firstName) ? true : false;
return (p1.lastName < p2.lastName) ? true : false;
}
bool operator == (const person& p1, const person& p2)
{
if (p1.lastName == p2.lastName && p1.firstName == p2.firstName)
return true;
return false;
}
bool operator != (const person& p1, const person& p2)
{
return !(p1 == p2);
}
bool operator > (const person& p1, const person& p2)
{
return !(p1 < p2) && !(p1 == p2);
}
int main ()
{
list <person> persList; // list of person
list <person>::iterator iter1; // iterator to a list of persons
// put persons in list
persList.push_back (person("Deauville", "William", 8435150));
persList.push_back (person("McDonald", "Stacey", 3327536));
persList.push_back (person("Bartoski", "Peter", 6946473));
persList.push_back (person("KuangThu", "Bruce", 4157300));
persList.push_back (person("Wellington", "Jhon", 9207400));
persList.push_back (person("McDonald", "Amanda", 8435150));
persList.push_back (person("Fredericks", "Roger", 7049982));
60. persList.push_back (person("McDonald", "Stacey", 7764987));
cout << "nNumber of entries = " << persList.size();
iter1 = persList.begin (); // display contents of list
while ( iter1 != persList.end() )
(*iter1++).display();
// find person or person with specified name (last and first)
string searchLastName, searchFirstName;
cout << "nnEnter last name of person to search for: ";
cin >> searchLastName;
cout << "Enter first name: ";
cin >> searchFirstName;
// make a person with that name
person searchPerson (searchLastName, searchFirstName, 0L);
// search for first match of names
iter1 = find (persList.begin(), persList.end(), searchPerson);
// find additional matches
if ( iter1 != persList.end() ) {
cout << "Person(s) with that name is(are)";
do {
(*iter1).display (); // display match
+
+iter1; // search again, one past match
iter1 = find (iter1, persList.end(), searchPerson);
} while (iter1 != persList.end());
}
else
cout << "There is no person with that name.";
// find person or persons with specified phone number
cout << "nnEnter phone number (format 12345678): ";
long sNumber; // get search number
cin >> sNumber;