Skip to content

Instantly share code, notes, and snippets.

@minjang
Last active December 23, 2015 16:36
Show Gist options
  • Save minjang/b69380c0773dafe9cbe5 to your computer and use it in GitHub Desktop.
Save minjang/b69380c0773dafe9cbe5 to your computer and use it in GitHub Desktop.
Python's enumerate implementation in C++
// 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';
// 백터 원소를 복사해서 값으로 받음. A는 영향 없음.
for (pair<size_t, string> p : enumerate(A))
cout << p.first << ": " << (p.second += p.second) << '\n';
// auto로도 받을 수 있음: p의 타입은 pair<size_t, string&>
for (auto p : enumerate(A))
cout << p.first << ": " << p.second << '\n';
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment