0

Vector data: Mary Daryl Cherry

Mary vector position[0]

Daryl vector position[1]

Cherry vector position[2]

Vector size: 3

Vector name: data


No need for Mary [ if vector[0], then display vector[1] and vector[2])

Scene 0: Daryl is on Scene_0 Cherry is on Scene_0


No need for Daryl ( if vector[1], then display vector[0] and vector[2])

Scene 1: Mary is on Scene_1 Cherry is on Scene_1


No need for cherry( if vector[2], then display vector[0] and vector[1])

Scene 2:

Mary is on Scene_2 Daryl is on Scene_2


How do i display at such above? It seems kinda hard to display

data.erase(data.begin());

for(int i=0; i<data.size(); i++)
{

    cout<<data[i]<<is on Scene_[i];

}

Thanks in advance!

user1745860
  • 207
  • 1
  • 5
  • 11

2 Answers2

2
cout << data[i] << " is on Scene_" << i;
Jason C
  • 38,729
  • 14
  • 126
  • 182
  • How do i do that it only display selective data? Like if its data[0] then display data[1] and data[2] – user1745860 Aug 13 '13 at 04:55
  • I don't understand what you are asking, but you can use `if` statements to do things based on conditions. Check out http://www.cplusplus.com/doc/tutorial/control/ :) – Jason C Aug 13 '13 at 04:57
  • i re-edit the question, it should be clearer now. i try using if loop. but got stuck with it – user1745860 Aug 13 '13 at 05:03
  • 1
    Well, one way is to have a second inner loop that goes through the data as well, and displays all the items that aren't the current item, e.g. `for (int i = 0; i < data.size(); ++ i) { for (int j = 0; j < data.size(); ++ j) { if (i != j) { /* display data */ } } }` – Jason C Aug 13 '13 at 05:10
2

For you problem, I think an inner loop can do easily the problem :

unsigned int size = data.size();
for( unsigned int i = 0; i < size; i++ )
{
    for ( unsigned int j = 0; j < size; j++ )
    {
        if ( i != j )
        {
            cout << data[j] << " is on Scene_" << i;
        }
    }
}

You can see it working here : http://ideone.com/oYEIHY.

Maybe you should look at http://www.cplusplus.com/doc/tutorial/control/, because you seem not very familiar with the structures. For example, the if statement is a loop...

Pierre Fourgeaud
  • 14,290
  • 1
  • 38
  • 62