主页
C++
C++ 迭代器
demo_iterator_loop2
运行 ❯
×
更改方向
保存代码
更改主题
#include
#include
using namespace std; int main() { // Create a vector called cars that will store strings vector
cars = {"Volvo", "BMW", "Ford", "Mazda"}; // Loop through vector elements for (auto it = cars.begin(); it != cars.end(); ) { if (*it == "BMW") { it = cars.erase(it); // Remove an element if it matches "BMW" } else { ++it; } } // Print vector elements for (const string& car : cars) { cout << car << "\n"; } return 0; }
Volvo
Ford
Mazda