去重之后还剩几个
运行下面这段程序:
#include <algorithm>
#include <iostream>
#include <map>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <vector>
using namespace std;
int main() {
vector<string> words = {"苹果", "香蕉", "苹果", "梨", "香蕉", "苹果"};
unordered_set<string> seen;
vector<string> out;
for (const string& w : words) {
if (!seen.count(w)) {
seen.insert(w);
out.push_back(w);
}
}
cout << out.size() << endl;
}
(本题用 g++ -std=c++17 -O0 编译。)
全部评论