U E D R , A S I H C RSS

STL/set


1. set

  • 집합을 κ΅¬ν˜„ν•œ 자료ꡬ쑰(STLμ—μ„œ containter)이닀.
  • μ§‘ν•©λ‚΄μ—λŠ” 같은 μ›μ†Œκ°€ 있으면 μ•ˆλœλ‹€.
  • 보톡 정렬이 λ˜μ–΄μžˆλ‹€. STL의 map도 정렬이 λ˜μ–΄ μžˆλ‹€.
  • include : set
    ~cpp 
    #include <set> 
    

1.1. μ„ μ–Έ


~cpp 
set<int> s;
set<int>::iterator i;

1.2. 데이터 λ„£κΈ°


~cpp 
// 3κ°œκ°€ λ™μΌν•œ 데이터 이λ€λ‘œ 5 ν•˜λ‚˜λ§Œ λ“€μ–΄μžˆλ‹€.
s.insert(5);
s.insert(5);
s.insert(5);	

1.3. 순회

  • STL의 container 듀은 λͺ¨λ‘ λΉ„μŠ·ν•œ λͺ¨μ–‘μ˜ 순회λ₯Ό ν•œλ‹€.
  • set 의 νŠΉμ§•μ€ μΈμžλ“€μ΄ μ •λ ¬λ˜μ–΄ μžˆλ‹€λŠ” 점
    ~cpp 
    set<int>::iterator i;
    
    // for μ—μ„œ 반볡자 이용 순회
    for(set<int>::iterator i = s.begin() ; i != s.end() ; ++i) { 
    	cout << *i << endl;	// μ •λ ¬λ˜μ–΄ μžˆλ‹€.
    } 
    
  • 1.4. ν”„λ‘œκ·Έλž¨μ˜ 예


    ~cpp 
    #include <iostream> 
    #include <set> 
    using namespace std; 
    
    int main() { 
    	int data[]={5,5,5,5,63,3,3,3,6,5};
    	set<int> s(&data[0], &data[10]);
    	
    	for(set<int>::iterator i = s.begin() ; i != s.end() ; ++i) { 
    		cout << *i << endl;	// μ •λ ¬λ˜μ–΄ μžˆλ‹€.
    	} 
    	
    	return 0; 
    }
    
    κ²°κ³Ό
    ~cpp 
    3
    5
    6
    63
    
    ----
    STL
    Valid XHTML 1.0! Valid CSS! powered by MoniWiki
    last modified 2021-02-07 05:27:58
    Processing time 0.0107 sec