// 연관 Container( Associative Container )
// 1. set: 순서를 가지고 값을 저장한다. 중복을 허용하지 않는다.
void main()
{
// 트리!! 완전이진트리, AVL, 레드블랙!!
// 들어올때마다 sort를 하기 때문에 빠르다.
// 두번째 인자로 단위전략을 쓰게되면 정렬방식을 바꿀 수 있다.
set<int, greater<int> > s;
s.insert(10);
s.insert(20);
s.insert(15);
s.insert(100);
SET::iterator p = s.begin();
*p = 100; // 성공은 하지만 정렬관계가 망가진다. 하지말자.
//pair<set<int, greater<int> >::iterator, bool> ret = s.insert(15);
pair<SET::iterator, bool> ret = s.insert(15);
// insert가 실패 하면 ret에 fasle가 리턴된다. 중복된 것이므로 실패
if ( ret.second == false )
{
cout << "실패" << endl;
}
copy( s.begin(), s.end(), ostream_iterator<int>(cout, " ") );
}
// 2. pair 에대해서
// 서로 다른 type 2개를 보관 할 수 있는 자료구조.
pair<int, double> foo()
{
return pair<int, double>(3, 3.4);
}
template<typename T1, typename T2> struct pair
{
T1 first;
T2 second;
//- zero initialize
//T b = T(); // built in type이면으로
// // User type이면default 생성자호출
pair( T1 a = T1(), T2 b = T2() ) : first(a), second(b) {}
};
void main()
{
pair<int, pair<int, double> > p2; // 3개의 인자를 받고 싶을때!!
// BOOST 안에는 pair를 개선한 tuple이 있습니다.
//tuple<int> one;
//tuple<int, int> two;
//tuple<int, int, int> three;
pair<int, double> p;
cout << sizeof(p) << endl;
}
// 3. map : pair를 저장하는 set. key를 가지고 data를 저장.
void main()
{
map<string, string> m;
// map에 요소를 넣는방법1. insert 사용
pair<string, string> p("mon", "월요일");
m.insert(p);
// 2. [] 연산자 사용
m["tue"] = "화요일";
cout << m["mon"] << endl; // 월요일
cout << m["web"] << endl;
}
// 4. stringstream
#include <sstream>
#include <fstream>
int main()
{
int n = 10;
ostringstream oss;
// n을 문자열로 바꾸고 싶다.
oss << "n = " << n; // C라면 sprintf( buf, "n = %d", n );
string s = oss.str(); // oss가 가진 string을 꺼낸다.
cout << s << endl;
char buf[256];
//cin >> buf;
//ifstream f( "a.cpp" );
//f >> buf;
// C의 strtok()를 STL style로 구현한 기법.
istringstream iss( "I am a boy" );
while( iss >> buf ) cout << buf << endl;
}