W3School TIY Editor

  • W3School 在线教程
  • 改变方向
  • 暗黑模式
​x
 
#include <iostream>
#include <vector>
using namespace std;
​
int main() {
  // 创建字符串向量 cars
  vector<string> cars = {"Volvo", "BMW", "Ford", "Tesla"};
​
  // 遍历向量元素
  for (auto it = cars.begin(); it != cars.end(); ) {
    if (*it == "BMW") {
      it = cars.erase(it); // 如果元素匹配 "BMW" 则移除
    } else {
      ++it;
    }
  }
​
  // 打印向量元素
  for (const string& car : cars) {
    cout << car << "\n";
  }
  return 0;
}