Skip to content

Instantly share code, notes, and snippets.

@takashi1975
Created July 16, 2015 05:10
Show Gist options
  • Select an option

  • Save takashi1975/25f31b8fadde167a7762 to your computer and use it in GitHub Desktop.

Select an option

Save takashi1975/25f31b8fadde167a7762 to your computer and use it in GitHub Desktop.
C++ イテレーター 添字の値を取得するには ... std::distance を使用
//TEST [3] を見つけることを期待
std::vector<bool> array = {false, false, false, true, false};
for (std::vector<bool>::iterator it = array.begin(); it != array.end(); it++)
{
if ((*it) == true)
{
auto index = std::distance(array.begin(), it);
CCLOG("%s iterator:%d", __PRETTY_FUNCTION__, index);
break;
}
}
for (std::vector<bool>::reverse_iterator it = array.rbegin(); it != array.rend(); it++)
{
if ((*it) == true)
{
//配列の先頭からの添字
auto index = std::distance(it, array.rend()) - 1;
auto last = std::distance(array.rbegin(), it);
CCLOG("%s reverse_iterator test:%d, last:%d", __PRETTY_FUNCTION__, index, last);
break;
}
}
@takashi1975

Copy link
Copy Markdown
Author

出力結果

cocos2d: iterator test:3
cocos2d: reverse_iterator test:3, last:1

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment