【发布时间】:2015-01-15 07:05:13
【问题描述】:
如果我有一套例如,内容如下:0,1,2,3,8,13,56,532,
我将如何处理从 3 到 532 到 0 到 3 的 for(auto it = xxxxx; y ? z; ++it)?
喜欢:
magic.start(3);
for(auto i: magic)
std::cout << i << " ";
那会打印出来:
3 8 13 56 532 0 1 2
编辑:可能有人对最终结果感兴趣(感谢所有答案):
bool SpectateNextPlayer(int playerid)
{
if (PlayerCurrentlySpectating[playerid] == INVALID_PLAYER_ID)
return false;
auto current = PlayersOnline.find(PlayerCurrentlySpectating[playerid]);
for (auto it = current; it != PlayersOnline.end(); ++it)
if (PlayerSpactatable(*it) && (*it) != PlayerCurrentlySpectating[playerid])
if (PlayerSpectateOtherPlayer(playerid, *it))//check here if playerid != *it
return true;
for (auto it = PlayersOnline.begin(); it != current; ++it)
if (PlayerSpactatable(*it) && (*it) != PlayerCurrentlySpectating[playerid])
if (PlayerSpectateOtherPlayer(playerid, *it))
return true;
return !DisablePlayerSpectate(playerid);
}
bool SpectatePreviousPlayer(int playerid)
{
if (PlayerCurrentlySpectating[playerid] == INVALID_PLAYER_ID)
return false;
auto rcurrent = find(PlayersOnline.rbegin(), PlayersOnline.rend(), PlayerCurrentlySpectating[playerid]);
for (auto it = rcurrent; it != PlayersOnline.rend(); ++it)
if (PlayerSpactatable(*it) && (*it) != PlayerCurrentlySpectating[playerid])
if (PlayerSpectateOtherPlayer(playerid, *it))
return true;
for (auto it = PlayersOnline.rbegin(); it != rcurrent; ++it)
if (PlayerSpactatable(*it) && (*it) != PlayerCurrentlySpectating[playerid])
if (PlayerSpectateOtherPlayer(playerid, *it))
return true;
return !DisablePlayerSpectate(playerid);
}
【问题讨论】:
-
为什么不能使用两个循环
-
好吧,我想如果没有其他方法也没关系 :) 但是我在使用
for (auto it = PlayersOnline.end(); it != PlayersOnline.find(3); --it)反向执行此操作时遇到问题,因为这会崩溃。 -
如果您需要保存结果序列,您可以使用
rotate_copy和附加容器。但是如果你只是想遍历集合,这个解决方案可能效率低下。