#include <iostream>
#include <algorithm>
#include <vector>
using namespace std;
int main()
{
vector <int> a ({1,2,3,3,4,4,5,6,6,7});
a.erase(unique(a.begin(),a.end()),a.end());
for (int i = 0;a[i];i++) cout<<a[i]<<endl;//第一种
for (int x:a) cout<<a[i]<<endl;//第二种
return 0;
}
为什么两个输出会不一样?如果我只想输出无重复的序列只能用第二种方法吗?
第一种办法怎么改可以输出第二种的内容?(前提你当然不知道无重复序列的长度啊)
你的两种方法都有错误。vector已经去完重了
for (int i = 0; i < a.size(); i++) cout << a[i] << endl;//第一种
for (int x : a) cout << x << endl;//第二种
//也可以用迭代器,for_each等等方法便利