In [1]:
#include <map>
#include <set>
In [2]:
std::set<int> mySet;
mySet.insert({7, 2, 4});
In [3]:
mySet
Out[3]:
{ 2, 4, 7 }
In [4]:
mySet = std::set<int>();
mySet.insert({7, 4, 2});
mySet
Out[4]:
{ 2, 4, 7 }
In [5]:
#include <iostream>
for (auto i = mySet.begin(); i != mySet.end(); ++i) {
    std::cout << *i << ' ';
}
2 4 7 
In [6]:
#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
Out[6]:
{ 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 }
In [7]:
std::map<int,std::vector<int>> factors;
In [8]:
// operator[] creates!
factors[1]
Out[8]:
{}
In [48]:
factors[1].push_back(1)
In [10]:
factors
Out[10]:
{ 1 => { 1 } }
In [49]:
factors.insert({2, std::vector<int>{1, 2}})
Out[49]:
@0x558d02183fa0
In [12]:
factors
Out[12]:
{ 1 => { 1 }, 2 => { 1, 2 } }
In [50]:
factors.emplace(3, std::vector<int>{1, 3})
Out[50]:
@0x558d02509100
In [14]:
factors
Out[14]:
{ 1 => { 1 }, 2 => { 1, 2 }, 3 => { 1, 3 } }
In [15]:
// f is a read-only "view" of factors
auto const& f = factors;
In [51]:
// we can still modify factors
factors.emplace(4, std::vector<int>{1, 2, 4})
Out[51]:
@0x558d02abd210
In [17]:
f
Out[17]:
{ 1 => { 1 }, 2 => { 1, 2 }, 3 => { 1, 3 }, 4 => { 1, 2, 4 } }
In [18]:
f[2]
input_line_39:2:3: error: no viable overloaded operator[] for type 'const std::map<int, std::vector<int, std::allocator<int> >, std::less<int>, std::allocator<std::pair<const int, std::vector<int, std::allocator<int> > > > >'
 f[2]
 ~^~
/opt/conda/envs/env/bin/../lib/gcc/x86_64-conda_cos6-linux-gnu/7.3.0/../../../../x86_64-conda_cos6-linux-gnu/include/c++/7.3.0/bits/stl_map.h:484:7: note: candidate function not viable: 'this' argument has type 'const std::map<int, std::vector<int, std::allocator<int> >, std::less<int>, std::allocator<std::pair<const int, std::vector<int, std::allocator<int> > > >
      >', but method is not marked const
      operator[](const key_type& __k)
      ^
/opt/conda/envs/env/bin/../lib/gcc/x86_64-conda_cos6-linux-gnu/7.3.0/../../../../x86_64-conda_cos6-linux-gnu/include/c++/7.3.0/bits/stl_map.h:504:7: note: candidate function not viable: 'this' argument has type 'const std::map<int, std::vector<int, std::allocator<int> >, std::less<int>, std::allocator<std::pair<const int, std::vector<int, std::allocator<int> > > >
      >', but method is not marked const
      operator[](key_type&& __k)
      ^
Interpreter Error: 
In [19]:
f.at(2)
Out[19]:
{ 1, 2 }
In [52]:
factors.insert({5, {1, 5}})
Out[52]:
@0x558d0292d7a0
In [21]:
factors
Out[21]:
{ 1 => { 1 }, 2 => { 1, 2 }, 3 => { 1, 3 }, 4 => { 1, 2, 4 }, 5 => { 1, 5 } }
In [53]:
factors.insert(std::pair<int,std::vector<int>>(6, std::vector<int>{1, 2, 3, 6}));
In [23]:
factors
Out[23]:
{ 1 => { 1 }, 2 => { 1, 2 }, 3 => { 1, 3 }, 4 => { 1, 2, 4 }, 5 => { 1, 5 }, 6 => { 1, 2, 3, 6 } }
In [54]:
factors.insert(std::make_pair(7, std::vector<int>{1, 7}))
Out[54]:
@0x558d02b26e50
In [25]:
factors
Out[25]:
{ 1 => { 1 }, 2 => { 1, 2 }, 3 => { 1, 3 }, 4 => { 1, 2, 4 }, 5 => { 1, 5 }, 6 => { 1, 2, 3, 6 }, 7 => { 1, 7 } }
In [26]:
#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");
    }
};
In [27]:
std::map<Person, float> students; // for GPA
In [28]:
Person killian {"William", "Killian", "you wish"};
In [29]:
students.emplace(killian, 3.38)
Out[29]:
@0x558cff3fdf50
In [30]:
students
Out[30]:
{ @0x558cff6e24c0 => 3.38f }
In [31]:
factors
Out[31]:
{ 1 => { 1 }, 2 => { 1, 2 }, 3 => { 1, 3 }, 4 => { 1, 2, 4 }, 5 => { 1, 5 }, 6 => { 1, 2, 3, 6 }, 7 => { 1, 7 } }
In [32]:
auto i = factors.begin();
Out[32]:
@0x558cfcde9fc0
In [36]:
(i = factors.erase(i))->first
Out[36]:
2
In [37]:
(i = factors.erase(i))->first
Out[37]:
3
In [38]:
(i = factors.erase(i))->first
Out[38]:
4
In [39]:
(i = factors.erase(i))->first
Out[39]:
5
In [40]:
(i = factors.erase(i))->first
Out[40]:
6
In [41]:
(i = factors.erase(i))->first
Out[41]:
7
In [42]:
factors.size()
Out[42]:
1
In [43]:
factors
Out[43]:
{ 7 => { 1, 7 } }
In [44]:
i = factors.erase(i)
Out[44]:
@0x7f287ffdf160
In [45]:
factors.size()
Out[45]:
0
In [46]:
factors.begin() == factors.end()
Out[46]:
true
In [47]:
i == factors.end()
input_line_93:2:4: note: use '=' to turn this equality comparison into an assignment
 i == factors.end()
   ^~
   =
Out[47]:
true
In [55]:
factors
Out[55]:
{ 1 => { 1 }, 2 => { 1, 2 }, 3 => { 1, 3 }, 4 => { 1, 2, 4 }, 5 => { 1, 5 }, 6 => { 1, 2, 3, 6 }, 7 => { 1, 7 } }
In [56]:
// range-based erase
for (auto i = factors.begin(); i != factors.end(); ) {
    i = factors.erase (i);
}
factors.size() == 0
Out[56]:
true
In [57]:
std::vector<int> values{1, 2, 3, 4, 5, 6, 7, 8};
std::set<int> svalues;
In [58]:
// range-based insert
auto first = values.begin();
auto last = values.end();

while (first != last) {
    svalues.insert(*first++);
}
svalues
Out[58]:
{ 1, 2, 3, 4, 5, 6, 7, 8 }
In [ ]: