250x250
Notice
Recent Posts
«   2024/11   »
1 2
3 4 5 6 7 8 9
10 11 12 13 14 15 16
17 18 19 20 21 22 23
24 25 26 27 28 29 30
관리 메뉴

일상 코딩

[C++/CPP] 16.02. STL iterator, 반복자 본문

C++/따배C++ 16강 STL

[C++/CPP] 16.02. STL iterator, 반복자

polarcompass 2022. 1. 4. 19:37
728x90
C++ 온라인 컴파일러 사이트
  1. https://replit.com/~
  2. https://www.onlinegdb.com/online_c++_compiler
  3. https://cpp.sh/
  4. https://www.tutorialspoint.com/compile_cpp_online.php
#include<iostream>
#include<vector>
#include<list>
#include<set>
#include<map>

using namespace std;

int main()
{
    vector<int> container;
    for (int i = 0; i < 10; ++i)
        container.push_back(i);
    
    {
        // vector<int>::iterator
        vector<int>::const_iterator itr;
        itr = container.begin();

        while (itr != container.end())
        {
            cout << *itr << " ";
            ++itr;
        }
        cout << endl;
    }

    {
        for (auto itr = container.begin(); itr != container.end(); ++itr)
        {
            cout << *itr << " ";
        }
        cout << endl;
        
    }

    {
        for(auto & e : container)
            cout << e << " ";
        cout << endl;
    }

    {
        list<int> container;
        for (int i = 0; i < 10; ++i)
            container.push_back(i);
        
        for (auto itr = container.begin(); itr != container.end(); ++itr)
            cout << *itr << " ";
        cout << endl;

        for(auto & e : container)
            cout << e << " ";
        cout << endl;
    }

    {
        set<int> container;
        for (int i = 0; i < 10; ++i)
            container.insert(i);
        
        for (auto itr = container.begin(); itr != container.end(); ++itr)
            cout << *itr << " ";
        cout << endl;

        for(auto & e : container)
            cout << e << " ";
        cout << endl;
    }

    {
        map<int, char> container;
        for (int i = 0; i < 10; ++i)
            container.insert(make_pair(i, char(i + 65)));
        
        for (auto itr = container.begin(); itr != container.end(); ++itr)
        {   
            cout << itr->first;
            cout << ":";
            cout << itr->second;
            cout << " ";
        }
        cout << endl;

        for(auto & e : container)
        {
            cout << e.first;
            cout << ":";
            cout << e.second;
            cout << " ";
        }
        cout << endl;
    }

    return 0;
}
728x90

'C++ > 따배C++ 16강 STL' 카테고리의 다른 글

[C++/CPP] 16.03. STL algorithm  (0) 2022.01.05
[C++/CPP] 16.01. Standard Template Libraries, containers  (0) 2022.01.02