2008. 1. 9. 17:24
vector, list, hash_map 사용하기 삽질예방/STL 교과서2008. 1. 9. 17:24
필수적인 변수들.
vector<int> newVector;
vector<int>::iterator vectorIt;
list<int> newList;
list<int>::iterator listIt;
hash_map<int, char*> newHash;
hash_map<int, char*> hashIt;
vector<int>::iterator vectorIt;
list<int> newList;
list<int>::iterator listIt;
hash_map<int, char*> newHash;
hash_map<int, char*> hashIt;
값의 대입
newVector.push_back(3);
newList.push_back(3);
newHash[3] = "fuct you";
newList.push_back(3);
newHash[3] = "fuct you";
이터레이터의 이용
for (vectorIt = newVector.begin(); vectorIt != newVector.end(); vectorIt++)
{
DoWhatYouWant();
cout << (*vectorIt) << endl;
}
{
DoWhatYouWant();
cout << (*vectorIt) << endl;
}
for (hashIt = newHash.begin(); hashIt!= newHash.end(); hashIt++)
{
DoWhatYouWant();
cout << "value: " << hashIt->first << ", key: " << hashIt->second << endl;
}
{
DoWhatYouWant();
cout << "value: " << hashIt->first << ", key: " << hashIt->second << endl;
}
vectorIt는 이터레이터 이자 , (*vectorIt)라 하면 현재 iterator가 가리키고 있는 원소가 리턴된다.
hashIt또한 이터레이터인데, hashIt->first는 키를 리턴, hashIt->second는 밸류를 리턴한다.
참고로 hashIt는 표준이 아니라고 한다. 그러나 별 문제없이 아직 잘쓰고 있으니 ^^;
원소찾기
newVector.find(3)
리턴값은 newVector::iterator형이다.
만약 찾지 못한다면 newVector.end()와 동등한 값을 리턴한다.