for(int i = 0; i < objects.size(); i++)
{
GameObject g = objects[i];
}
//my question now is, what is the list version for this??
you might say, that why don't i use vectors instead, but the point is, list have a function called "remove", and vectors don't. "remove" is what i need.
The easiest way it to use iterators to iterate through the list. Did you try finding and reading any documentation for std::list. Most of the documentation shows how to traverse and display a list.
Here is a quick example:
1 2 3 4 5
std::list<std::string> words {"the", "frogurt", "is", "also", "cursed"};
std::cout << "The contents of words are: ";
for(auto& itr : words)
std::cout << itr << " ";
std::cout << '\n';
void Handler::removeObject(GameObject* tempObject)
{
object.remove(/*remove is not available for vector*/);
// i want a simple method that would tell me how to do this.
i want a simple answer that has all of these functions
}
#include <iostream>
#include <algorithm>
#include <vector>
int main()
{
std::vector<int> ints{5, 9, 7, 2, 4};
std::cout << "Before: ";
for (int i : ints)
{
std::cout << i << ' ';
}
std::cout << '\n';
int n;
std::cout << "Which number do you want to remove? ";
std::cin >> n;
std::vector<int>::iterator it = std::find(ints.begin(), ints.end(), n);
if (it != ints.end())
{
ints.erase(it);
std::cout << "The number has been removed!\n";
}
else
{
std::cout << "The number was not found!\n";
}
std::cout << "After: ";
for (int i : ints)
{
std::cout << i << ' ';
}
std::cout << '\n';
}
Test run #1
Before: 5 9 7 2 4
Which number do you want to remove? 2
The number has been removed!
After: 5 9 7 4
Test run #2
Before: 5 9 7 2 4
Which number do you want to remove? 100
The number was not found!
After: 5 9 7 2 4
If you already know the index that you want to remove you don't have to search for it. Instead you can just add the index to the begin iterator and pass that to erase.