1

if I have a set for example, with the following content: 0,1,2,3,8,13,56,532,

how would I do a for(auto it = xxxxx; y ? z; ++it) from 3 to 532 to 0 to 3 ?

Like:

magic.start(3);
for(auto i: magic)
    std::cout << i << " ";

That would print:

3 8 13 56 532 0 1 2

Edit: Might anyone be interested in the end result (thanks for all the answers):

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);
}
Gizmo
  • 1,990
  • 1
  • 24
  • 50
  • why can't you use two loops – Ashwani Nov 18 '14 at 07:09
  • well I suppose that's okay if there is no other way :) however I'm having problems when doing this in reverse with `for (auto it = PlayersOnline.end(); it != PlayersOnline.find(3); --it)` as this crashes. – Gizmo Nov 18 '14 at 07:11
  • If you need to save the result sequence, you could use [`rotate_copy`](http://en.cppreference.com/w/cpp/algorithm/rotate_copy) with additional container. But if you just want to iterate over the set, this solution may be inefficient. – awesoon Nov 18 '14 at 07:15

3 Answers3

1

You could do it like this (with two loops).

The code uses set::find() to track down the element you wish to start from. Then it will print from the iterator returned from find() till the end of the set. Notice that I didn't check what find() returned, for simplicity.

In the second loop it will print from the start until it meets the element the first loop started with.

#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;
}

Output:

myset contains: 3 8 13 56 532
 0 1 2

In order to do this backwards, you could do something like in this answer.

Community
  • 1
  • 1
gsamaras
  • 71,951
  • 46
  • 188
  • 305
  • Yes @Gizmo, you just need to start from `.end() - 1`. Do you want me to show you an example? Or you can handle it from here? ;) – gsamaras Nov 18 '14 at 07:26
1

I guess what you mean is like this:

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;
   } 
}
SHR
  • 7,940
  • 9
  • 38
  • 57
  • your code is nice, works too, thoug it's introducing an additional variable, and requires the person reading my code to analyze this piec, so +1 for the nice answer but I'll go with the above :# – Gizmo Nov 18 '14 at 07:27
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);
}
Tony Delroy
  • 102,968
  • 15
  • 177
  • 252