• std::string

    • constructor
      • string(const char*s)
      • string(int n, char c) c를 n개 반복
      • string(const string& str) copy constructor
      • string() empty string
      • string(const char*s, int n) s를 n자리까지만 자름
      • template<class Iter> string(Iter begin, Iter end) Iter의 begin~end 전까지 이어붙임
      • string(const string& str, int pos, int n) string의 pos에서 n개만 자름
      • string(string && str) noexcept move constructor: const로 취급하지 않음.
    • compare
      • s1<s2, s1==s2, s1> s2 가능 더 길거나 알파벳이 크면 크다.
    • size
      • str.size(), str.length()
  • Smart Pointer

    • constructor tamplate<class Type> explicit auto_ptr(Type* ptr);
    • no implicit type conversion allowed shared_ptr<double> pd; double *p = new double; pd = p; //error pd = shared_ptr<double>(p);
    • delete가 필요 없다.
    • *, -> 사용 가능
    • 레퍼런스를 받으면 안됨; 레퍼런스를 delete 하기 때문
      • auto_ptr only one pointer to one object assignment: transfer ownership ***no more use

      • unique_ptr only one pointer to one object NO assignment allowed

        • make assignment operator private

        • Allow: only when rvalue is temporary object(바로 파괴): 전달됨

          Untitled

      • shared_ptr many pointers point one object delete when counter=0

    • making smart pointers
      • constructor, destructor
      • overload =, *, ->
      • deep copy needed
    • with arrays use delete : delete[]아님!
      • NOT ALLOWED: shared_ptr<int> ptr(new int[n]);
      • ALLOWED: shared_ptr<int[]> ptr(new int [n]); (c++17)
  • <vector>

    • vector<int> a(n,0);
    • a.size()
    • a.begin(), a.end()
    • a.push_back()
    • a.erase()
    • a.insert()
    • #include <algorithm> sort(a.begin(), a.end()); random_shuffle(a.begin(), a.end()); for_each(a.begin(), a.end(), func); //func에 (*it) 하나씩 들어감
      • 하나씩 가져오는 for_each for_each(a.begin(), a.end(), []type& n){~}
    • range based for loop for(int x:a) cout<<x; //하나씩 출력 for(int &x:a) x = 2; //전부 2가 됨
  • iterators generalization of pointer *로 접근(dereferencing) ++로 이동 가능(incrementing)

    • vector<int>::iterator it; it = a.begin(); *it = 3; //a[0]=3; 과 동일 it++; //다음 원소 가리킴

    • linked list와 같은 자료형에서와 똑같이 동작하게 구현할 수 있다 generic programming의 일환

    • kind of iterators

      • InputIterator 프로그램으로의 입력을 뜻한다. container에서 값을 읽을 수 있어야 한다. 값을 바꾸지는 못한다. ++을 지원한다.
        • find() 등
      • OutputIterator 프로그램에서의 출력을 뜻한다. container에서 값을 바꿀 수 있어야 한다. 값을 읽지는 못한다.
        • copy() 등
      • ForwardIterator 입출력을 전부 지원한다. ++만 사용한다.
        • replace() 등
      • BidirectionalIterator ForwardIterator에서 --연산을 추가한 버전
        • reverse() 등
        • list<int>::iterator
      • RandomAccessIterator bidirectional에서 [] 연산자, 관계 연산자
        • sort()등
        • vector<int>::iterator

      Untitled

  • Container objects are owned by container objects: copy constructable, assignable

    • Sequence containers vector, array, stack, queue, deque, priority_queue, list
    • Associative containers key-values set, map, multiset, multimap
    • Unordered Associative containers(C++11) unordered key-values unordered_set, unordered_map, unordered_multiset, unordered_multimap
  • Functor(펑터) = Function objects 함수처럼 ()과 함께 쓸 수 있는 것

    • 함수 이름: func()
    • 함수 포인터: (*func)()
    • ()오버로드된 객체: myclass()
  • STL의 Functor #include <functional>

    Untitled

    ex) sort(a.begin(), a.end(), greater)

  • <initializer_list> 초기화 리스트이다. {}로 묶여 있다. vector(initializer_list<T> init, const Allocator&alloc = Allocator());

    • vector<int> v1 {10}; //10 한개 //initiallizer_list를 생성하고 이를 인수로 받는 생성자가 호출된다.
    • int func(initializer_list<int> ini){~} func({1,2,3});