map<Key,T>::iterator it; (*it).first; // the key value (of type Key) (*it).second; // the mapped value (of type T) (*it); // the "element value" // (of type pair<const Key,T>) // 다음과 같이 사용할 수도 있다. it->first; // (*it).first (the key value) it->second; // (*it).second (the mapped value)//
// map의 키가 되기 위해서는 아래와 같이 // operator<를 구현하고 있어야 한다. class CMyKey { public: std::string m_key; // map의 키가 되기 위해서는 비교 연산자가 필요 // const member function으로 구현 필요 bool operator<(const CMyKey& other) const { return m_key.c_str()[0] < other.m_key.c_str()[0]; }; // constructor CMyKey(const char* key){ m_key = key;}; }; //
// map의 키가 되기 위해서는 아래와 // 같이 operator<를 구현하고 있어야 한다. class CMyKey { public: std::string m_key; // map의 키가 되기 위해서는 비교 연산자가 필요 // const member function으로 구현 필요 bool operator<(const CMyKey& other) const { return m_key.c_str()[0] < other.m_key.c_str()[0]; }; // constructor CMyKey(const char* key){ m_key = key;}; }; void main() { // map 선언 std::map<CMyKey, std::string> myMap; // map에 값 넣기 myMap[ CMyKey("a")] = "a_value"; myMap[ CMyKey("b")] = "b_value"; myMap[ CMyKey("c")] = "c_value"; // check the size std::cout<<"the size of map is: " <<myMap.size()<<std::endl; // 찾기 CMyKey("b") 인 것을 찾기 std::map<CMyKey, std::string>::iterator it; it = myMap.find( CMyKey("b")); if( it == myMap.end()) { std::cout<<"could not find"<<std::endl; } else { // 찾아서 it->second를 value를 찍어 봄 std::cout<<"find the element. value is: " <<it->second<<std::endl; } // iterator를 이용한 삭제 myMap.erase(it); // 삭제 확인 it = myMap.find( CMyKey("b")); if( it == myMap.end()) std::cout<<"Right. element is removed" <<std::endl; else std::cout<<"Wrong. element exits"<<std::endl; // clear myMap.clear(); // check the size std::cout<<"the size of map after clear: " <<myMap.size()<<std::endl; } //
STL의 Container (std::list, std::vector, etc)들의 모든 elements를 변환할 때 사용한다 (의미하는 그대로). algorithm에 포함된 것 답게 unary (단항) operator나 binary (이항) operator를 parameter로 받아서 처리해준다. 즉, std::vector의 모든 elements를 1씩 증가 시켜주거나 prefix로 특정 문자를 붙이는 경우 사용할 수 있을 것이다.
template < class InputIterator, class OutputIterator, class UnaryOperator > OutputIterator transform ( InputIterator first1, InputIterator last1, OutputIterator result, UnaryOperator op );template < class InputIterator1, class InputIterator2, class OutputIterator, class BinaryOperator > OutputIterator transform ( InputIterator1 first1, InputIterator1 last1, InputIterator2 first2, OutputIterator result, BinaryOperator binary_op );
//
첫 번째 것은 단항 연산자를 이용해서 입력으로 들어오는 container의 처음과 끝 iterator를 처리해주고 결과를 output conatiner의 iterator에서 차례로 넣어 준다. 두 번째 것은 이항 연산자를 이용해서 첫 번째 container (first1 ~ last1)와 두 번째 container의 시작 (frist2) 부터를 처리해서 output containter의 iterator에 차례로 넣어 준다.