^(코딩캣)^ = @"코딩"하는 고양이;
[단막 C++ 문법] C++ 배열 반복문 사용 방법
C++ 배열 반복문 사용 방법 C++에서 배열 반복문을 사용하는 방법은 다음과 같이 정리할 수 있다. 1. 전통적인 배열 반복문 전통적인 배열 반복문은 역시 인덱스 변수를 선언하고 그 변수를 증감하면서 배열의 원소에 순차적으로 접근하는 방식이다. /* C++ source */ #include // std::array #include // std::cout using namespace std; int main(int argc, char * argv[]) { std::array integers = { 2, 4, 6, 8, 0, 9, 7, 5, 3, 1 }; for (size_t i = 0; i < integers.size(); i++) { cout
Language/C & C++
2019. 2. 7. 16:06

[단막 C++ 문법] C++ 배열 반복문 사용 방법

Language/C & C++
2019. 2. 7. 16:06

C++ 배열 반복문 사용 방법


C++에서 배열 반복문을 사용하는 방법은 다음과 같이 정리할 수 있다.

 

1. 전통적인 배열 반복문


전통적인 배열 반복문은 역시 인덱스 변수를 선언하고 그 변수를 증감하면서 배열의 원소에 순차적으로 접근하는 방식이다.

/* C++ source */
#include <array>     // std::array
#include <iostream>  // std::cout

using namespace std;

int main(int argc, char * argv[])
{
    std::array<int, 10> integers = { 2, 4, 6, 8, 0, 9, 7, 5, 3, 1 };
    
    for (size_t i = 0; i < integers.size(); i++)
    {
        cout<<integers[i]<<'\t';
    }
    return 0;
}

 

2. 반복자 사용하기


STL Container 클래스내의 반복자(iterator)를 자동변수로 선언하여 배열의 원소에 순차적으로 접근할 수도 있다. 여기에서 반복자의 자료형은 std::array<int, 10>::iterator이지만 작성의 편의를 위해 auto 키워드로 대체하여 반복자를 선언하는 것이 보통이다.

/* C++ source */
#include <array>     // std::array
#include <iostream>  // std::cout

using namespace std;

int main(int argc, char * argv[])
{
    std::array<int, 10> integers = { 2, 4, 6, 8, 0, 9, 7, 5, 3, 1 };
    
    for (std::array<int, 10>::iterator = integers.begin(); integer != integers.end(); integer++)
    {
        cout<<*integers<<'\t';
    }
    
    return 0;
}

 

3. std::for_each 사용하기


std::for_each는 앞서 작성한 반복자 선언을 좀 더 키워드화한 것으로 algorithm 헤더 파일에 다음과 같이 선언되어 있다.

/* C++ source */
template <class InputIterator, class Function>
Function for_each (InputIterator first, InputIterator last, Function fn);

 

여기서 fn 매개변수는 람다식이 될 수도 있고 함수 포인터가 될 수도 있다. 다음은 함수 포인터를 사용한 예이다.

/* C++ source */
#include <algorithm> // std::for_each
#include <array>     // std::array
#include <iostream>  // std::cout

using namespace std;

void myloop(int);

int main(int argc, char * argv[])
{
    std::array<int, 10> integers = { 2, 4, 6, 8, 0, 9, 7, 5, 3, 1 };
    
    std::for_each
    (
        integers.begin(), // InputIterator first
        integers.end(), // InputIterator last
        myloop // Function fn
    );
    
    return 0;
}

void myloop(int integer)
{
    cout<<integer<<'\t';
}

 

다음은 람다식을 사용한 예이다.

/* C++ source */
#include <algorithm> // std::for_each
#include <array>     // std::array
#include <iostream>  // std::cout

using namespace std;

int main(int argc, char * argv[])
{
    std::array<int, 10> integers = { 2, 4, 6, 8, 0, 9, 7, 5, 3, 1 };
    
    std::for_each
    (
        integers.begin(), // InputIterator first
        integers.end(), // InputIterator last
        [](int integer) { cout<<integer<<'\t'; } // Function fn
    );
    
    return 0;
}

 

4. Range based for 구문 사용하기


Range based for가 바로 파이썬, 베이직 등 타 스크립트 언어에서 지원하는 foreach 구문이다. C++에서 foreach 구문은 다음과 같은 형태를 갖는다.

/* C++ source */
#include <array>     // std::array
#include <iostream>  // std::cout

using namespace std;

int main(int argc, char * argv[])
{
    std::array<int, 10> integers = { 2, 4, 6, 8, 0, 9, 7, 5, 3, 1 };
    
    for (int & integer : integers)
    {
        cout<<integer<<'\t';
    }
    
    return 0;
}

 

카테고리 “Language/C & C++”
more...

“c++11” (1건)