vectors and iterators?

This is a discussion on vectors and iterators? within the C++ Programming forums, part of the General Programming Boards category; In Java I could use an arraylist and this wouldn't be a problem but I'm not sure how to do ...

  1. #1
    Registered User
    Join Date
    Jul 2007
    Posts
    186

    vectors and iterators?

    In Java I could use an arraylist and this wouldn't be a problem but I'm not sure how to do this correctly in C++. I'm trying to simulate a draft and so I need to add and remove people from a list.

    Code:
    vector<Player> PlayerIO::draft()
    {
      string ans = "y" ;
      int i = 0;
      vector<Player> myPlayers;
      vector<Player>::iterator it = allPlayers.begin();
      cout << "Enter 'y' for yes, 'n' for no, 't' for taken, 'b' for beginning of list, 'q' for quit" << endl;
      while(allPlayers.empty() == false | ans.compare("q")!=0 )
      {
    	  cout << "Pick " << allPlayers[i].getName() << " ?";
    	  cin >> ans;
    	  if(ans.compare("y")==0)
    	  {
    		  myPlayers.push_back(allPlayers[i]);
    		  allPlayers.erase(it);
    	  }
    	  else if(ans.compare("t")==0)
    	  {
    		  allPlayers.erase(it);
    	  }
    	  else if(ans.compare("n")==0)
    	  {
    		  i++;
    		  it++;
    	  }
              else if(ans.compare("b")==0)
              {
                     i=0;
                    it = allPlayers.begin();
              }
      }
    
    return myPlayers;
    }

  2. #2
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,801
    Code:
    while(allPlayers.empty() == false | ans.compare("q")!=0 )
    The | is bitwise-or and not the boolean/logical-or || like I think you want. The comparison of ans, here and elsewhere in the code, is easily done using ans != "q".

    Code:
    if(ans.compare("y")==0)
    {
        myPlayers.push_back(allPlayers[i]);
        allPlayers.erase(it);
    }
    else if(ans.compare("t")==0)
    {
        allPlayers.erase(it);
    }
    Erasing an item from a vector will invalidate the it iterator. You must reassign the return value of the erase function call to it:
    Code:
    if(ans.compare("y")==0)
    {
        myPlayers.push_back(allPlayers[i]);
        it = allPlayers.erase(it);
    }
    else if(ans.compare("t")==0)
    {
        it = allPlayers.erase(it);
    }
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  3. #3
    Registered User
    Join Date
    Jul 2007
    Posts
    186
    That makes sense. How do I check if a string contains something? I'm trying str.contains("something")!=0 and I'm getting error 'contains': is nto a member of 'std::basic_string<_Elem,_Traits,_Ax>'

  4. #4
    Registered User
    Join Date
    Jan 2005
    Posts
    7,319
    Make sure to check your favorite reference book or website to find the right function. In this case, the function you want is find.

  5. #5
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,801
    And an example to go along with that:
    Code:
    string str("Hello");
    string::size_type index;
    if( (index = str.find("ll")) != string::npos )
        cout << "Found match at index: " << index << endl;
    else
        cout << "No match found." << endl;
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Vectors and Iterators
    By Trev614 in forum C++ Programming
    Replies: 3
    Last Post: 06-30-2008, 10:30 AM
  2. vectors iterator
    By manzoor in forum C++ Programming
    Replies: 6
    Last Post: 05-11-2008, 04:02 PM
  3. C++ STL: Why use member iterators
    By Vandrian in forum C++ Programming
    Replies: 4
    Last Post: 04-03-2008, 11:52 AM
  4. Vectors, iterators and loops
    By Heavens in forum C++ Programming
    Replies: 2
    Last Post: 05-09-2007, 06:05 AM
  5. vectors with same iterators
    By strickey in forum C++ Programming
    Replies: 3
    Last Post: 02-10-2005, 04:34 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21