⚠️ 去重和排序,是躲不掉的
(本条路线统一用 g++ -std=c++17 -O0 -Wall 编译)
#include <iostream>
#include <map>
#include <set>
#include <string>
#include <vector>
int main() {
/* map / set 遍历一定是按键排序的;vector 是你放进去的顺序 */
std::vector<std::string> v{"pear", "apple", "fig"};
std::set<std::string> s{"pear", "apple", "fig"};
std::string vfirst = v.front();
std::string sfirst = *s.begin();
/* set 会去重,vector 不会 */
std::vector<int> vi{3, 1, 3};
std::set<int> si{3, 1, 3};
std::cout << vfirst << "/" << sfirst << "/"
<< vi.size() << "/" << si.size() << "\n";
return 0;
}
全部评论