This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 6. 초기화 리스트 | |
cout << "[TEST 6] initializer list\n"; | |
for (auto &&p : enumerate({"foo", "bar", "baz"})) | |
cout << p.first << ": " << p.second << '\n'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 4. 앞서 구현한 range 사용 예 | |
cout << "[TEST 4] range\n"; | |
auto &&D = range(100, 103); | |
// decltype(p) == pair<size_t, int>&& | |
for (auto &&p : enumerate(D)) | |
cout << p.first << ": " << p.second << '\n'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 2. 일반 배열 예 | |
cout << "[TEST 2] array\n"; | |
string C[] = {"foo", "bar", "baz"}; | |
// auto&&로 받는 것이 범위 기반 for 문에서 일반적이고 효율적인 방법 | |
// p 타입: pair<size_t, string&>&&, 원소 타입이 string&로 추론 | |
for (auto &&p : enumerate(C, 100)) | |
cout << p.first << ": " << (p.second += p.second) << '\n'; | |
for (auto &&p : enumerate(C, 100)) | |
cout << p.first << ": " << p.second << '\n'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <iostream> | |
template<typename T> | |
class range_iterator { | |
T cur_; | |
const T step_ = 1; | |
public: | |
range_iterator(T init) : cur_{init} {} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 5. 변수를 거치지 않고 직접 사용 | |
cout << "[TEST 5] in-place through rvalue reference\n"; | |
for (auto &&p : enumerate(range(100, 103))) | |
cout << p.first << ": " << p.second << '\n'; | |
// decltype(p) == pair<size_t, string&>&& | |
for (auto &&p : enumerate(vector<string>{"foo", "bar", "baz"})) | |
cout << p.first << ": " << (p.second += p.second) << '\n'; | |
// 함수 반환값 바로 사용 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 3. const 예제 | |
cout << "[TEST 3] const\n"; | |
const string E[] = {"foo", "bar", "baz"}; | |
// decltype(p) == pair<size_t, string const&>&& | |
// p 자체는 상수가 아니므로 인덱스 값은 변경 가능, 배열 값은 수정 불가. | |
for (auto &&p : enumerate(E)) | |
cout << (p.first += 1) << ": " << p.second << '\n'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 1. Forward iterator를 얻을 수 있는 STL 컨테이너 | |
cout << "[TEST 1] vector<string>\n"; | |
vector<string> A = {"foo", "bar", "baz"}; | |
// 참조자로 직접 A 내용 수정. | |
for (pair<size_t, string&> p : enumerate(A)) | |
cout << p.first << ": " << (p.second += p.second) << '\n'; | |
// 수정 내역 확인: 벡터 원소를 상수 참조자로 받음. | |
for (pair<size_t, const string&> p : enumerate(A)) | |
cout << p.first << ": " << p.second << '\n'; |
NewerOlder