📄 10_3.cpp
字号:
// 10_3.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include <iostream>
#include <conio.h>
#include <vector> // 注意,不是vector.h
#include <algorithm>
#include <iterator>
using namespace std; // 所有STL模板都定义在名字空间std中
int main(int argc, char* argv[])
{
vector<int> vecInt;
// 输入10个整数
cout << "please input 10 integer here:" << endl;
for(int i = 0; i < 10; i++){
int val;
cin >> val;
vecInt.push_back(val);
}
// 利用迭代器iterator遍历, 删除20
vector<int>::iterator it = vecInt.begin();
while(it != vecInt.end()){
if(*it == 20)
it = vecInt.erase(it); // erase删除, 返回值it指向被删除的下一个元素
else
it++; // 迭代,指向下一个元素
}
// 利用泛型算法删除20
// remove(vecInt.begin(), vecInt.end(), 20);
// 利用下标遍历输出删除后的数
cout << "remove all value less than 20:" << endl;
for(i = 0; i < vecInt.size(); i++){
cout << vecInt[i] << " "; // 利用下标操作
}
cout << endl;
getch();
return 0;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -