给定vector<int>对象,删除其中的重复元素,同时保持元素本来的顺序。
#include <iostream> #include <vector> #include <algorithm> using namespace std; int main() { int b,i; int a[10]; i=0; while (cin >>b) { a[i]=b; i++; if(cin.get()=='\n') break; } vector<int> v(a,a+i); vector<int> v0; vector<int>::iterator it1,it2; for(it1=v.begin();it1!=v.end();it1++){ it2=find(v.begin(),it1,*it1); if(it2==it1){ v0.push_back(*it1); } } for (vector<int>::iterator it = v0.begin(); it != v0.end(); it++) { cout << *it<<" "; } }
# include <bits/stdc++.h> using namespace std; int main() { int num; unordered_set<int> hash; vector<int> nums; while(cin >> num) if(hash.find(num) == hash.end()) { hash.insert(num); nums.push_back(num); } for(int tmp : nums) cout << tmp << " "; return 0; }
import java.util.*; public class Main { public static void main(String[] args) { LinkedHashSet<Integer> set = new LinkedHashSet<>(); Scanner in = new Scanner(System.in); while (in.hasNextInt()) set.add(in.nextInt()); for (int value : set) System.out.print(value+" "); } }