#include <map>
#include <set>
std::set<int> mySet;
mySet.insert({7, 2, 4});
mySet
mySet = std::set<int>();
mySet.insert({7, 4, 2});
mySet
#include <iostream>
for (auto i = mySet.begin(); i != mySet.end(); ++i) {
std::cout << *i << ' ';
}
#include <vector>
std::vector<int> v {6, 2, 4, 8, 1, 5, 3, 7, 10, 9};
std::set <int> s (v.begin(), v.end());
v.assign (s.begin(), s.end());
v
std::map<int,std::vector<int>> factors;
// operator[] creates!
factors[1]
factors[1].push_back(1)
factors
factors.insert({2, std::vector<int>{1, 2}})
factors
factors.emplace(3, std::vector<int>{1, 3})
factors
// f is a read-only "view" of factors
auto const& f = factors;
// we can still modify factors
factors.emplace(4, std::vector<int>{1, 2, 4})
f
f[2]
f.at(2)
factors.insert({5, {1, 5}})
factors
factors.insert(std::pair<int,std::vector<int>>(6, std::vector<int>{1, 2, 3, 6}));
factors
factors.insert(std::make_pair(7, std::vector<int>{1, 7}))
factors
#include <string>
struct Person {
std::string first;
std::string last;
std::string dob;
friend bool operator< (Person const& p1, Person const& p2) {
int compare = p1.last.compare(p2.last);
if (compare == 0) {
compare = p1.first.compare(p2.first);
if (compare == 0) {
compare = p1.dob.compare(p2.dob);
}
}
return compare < 0;
}
friend std::ostream& operator<<(std::ostream& os, Person const& p) {
return (os << "haha no way");
}
};
std::map<Person, float> students; // for GPA
Person killian {"William", "Killian", "you wish"};
students.emplace(killian, 3.38)
students
factors
auto i = factors.begin();
(i = factors.erase(i))->first
(i = factors.erase(i))->first
(i = factors.erase(i))->first
(i = factors.erase(i))->first
(i = factors.erase(i))->first
(i = factors.erase(i))->first
factors.size()
factors
i = factors.erase(i)
factors.size()
factors.begin() == factors.end()
i == factors.end()
factors
// range-based erase
for (auto i = factors.begin(); i != factors.end(); ) {
i = factors.erase (i);
}
factors.size() == 0
std::vector<int> values{1, 2, 3, 4, 5, 6, 7, 8};
std::set<int> svalues;
// range-based insert
auto first = values.begin();
auto last = values.end();
while (first != last) {
svalues.insert(*first++);
}
svalues