如果我有一组例如,具有以下内容: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);
}
答案 0 :(得分:1)
你可以这样做(有两个循环)。
代码使用set::find()来跟踪您希望从中开始的元素。然后它将从从find()
返回的迭代器打印到集合的结尾。请注意,为简单起见,我没有检查find()
返回的内容。
在第二个循环中,它将从开始打印,直到它遇到第一个循环开始的元素。
#include <iostream>
#include <set>
int main ()
{
std::set<int> myset;
myset.insert (0);
myset.insert (1);
myset.insert (2);
myset.insert (3);
myset.insert (8);
myset.insert (13);
myset.insert (56);
myset.insert (532);
std::cout << "myset contains:";
std::set<int>::iterator it1;
it1=myset.find(3);
for (std::set<int>::iterator it = it1; it!=myset.end(); ++it)
std::cout << ' ' << *it;
std::cout << '\n';
for (std::set<int>::iterator it2=myset.begin(); *it2!=*it1; ++it2)
std::cout << ' ' << *it2;
std::cout << '\n';
return 0;
}
输出:
myset contains: 3 8 13 56 532
0 1 2
为了向后执行此操作,您可以执行this回答中的操作。
答案 1 :(得分:1)
我猜你的意思是这样的:
bool first=true;
auto start = magic.find(3);
for(auto it=start;it!=start || first;it++){
first = false;
if(it == magic.end()){//make it like a circled list
it = magic.begin();
}
if(it == magic.end()){
cout << *it;
}
}
答案 2 :(得分:0)
auto i = magic.find(3);
if (i != magic.end())
{
auto finito = i;
do
{
std::cout << *i << ' '; // or whatever you want to do with elements
if (++i == magic.end()) i = magic.begin();
} while (i != finito);
}