#include <set>
#include <map>
#include <unordered_set>
#include <unordered_map>
#include <algorithm>
#include <iostream>
#include <vector>
#include <functional>
#include <iterator>
using namespace std;
set<int> s{1,2,3,4,5,8,20};
s
{ 1, 2, 3, 4, 5, 8, 20 }
set<int> s2{1,20,3,4,5,8,3};
s2
{ 1, 3, 4, 5, 8, 20 }
set<int> s3{1,1,1,3,3,2,9,15};
s3
{ 1, 2, 3, 9, 15 }
multiset<int> m{1,1,1,3,3,2,9,15};
m
{ 1, 1, 1, 2, 3, 3, 9, 15 }
multiset<int> m{1,15,1,3,2,2,9,3,1};
m
{ 1, 1, 1, 2, 2, 3, 3, 9, 15 }
set<int, greater<int> > s2{1,2,3,4,5,8,20};
s2
{ 20, 8, 5, 4, 3, 2, 1 }
// declaring set
set<int> st;
// declaring iterators
auto it = st.begin();
set<int>::iterator it1, it2;
// declaring pair for return value of set containing
// set iterator and bool
pair<set<int>::iterator, bool> ptr;
// using insert() to insert single element
// inserting 20
ptr = st.insert(20);
// checking if the element was already present or newly inserted
if (ptr.second)
cout << "The element was newly inserted" << endl;
else
cout << "The element was already present" << endl;
The element was newly inserted
// using insert() to insert single element
// inserting 20 again ...
ptr = st.insert(20);
// checking if the element was already present or newly inserted
if (ptr.second)
cout << "The element was newly inserted" << endl;
else
cout << "The element was already present" << endl;
The element was already present
// printing set elements after insertion
cout << "\nThe set elements after 1st insertion are : ";
for (it1 = st.begin(); it1 != st.end(); ++it1)
cout << *it1 << " ";
The set elements after 1st insertion are : 20
// inserting set elements using hint
st.insert(it, 24);
// printing set elements after insertion
cout << "\nThe set elements after 2nd insertion are : ";
for (it1 = st.begin(); it1 != st.end(); ++it1)
cout << *it1 << " ";
The set elements after 2nd insertion are : 20 24
// inserting array elements in set
// 24 is not inserted again
int arr[3] = {25, 24, 26};
st.insert(arr, arr + 3);
// printing set elements after insertion
cout << "\nThe set elements after 3rd insertion are : ";
for (it1 = st.begin(); it1 != st.end(); ++it1)
cout << *it1 << " ";
cout << endl;
The set elements after 3rd insertion are : 20 24 25 26
// using insert() to insert single element
// inserting 20 again
ptr = st.insert(20);
// checking if the element was already present or newly inserted
if (ptr.second)
cout << "The element was newly inserted" << endl;
else
cout << "The element was already present" << endl;
The element was already present
// returns 1 if exists, 0 otherwise
st.count(20)
1
st.count(30)
0
// returns iterator if found, end() if not found
auto it = st.find(20);
it
@0x7f53ae1ee1b8
// true if found, false otherwise
it != st.end()
input_line_35:3:4: note: use '|=' to turn this inequality comparison into an or-assignment it != st.end() ^~ |=
true
// true if found, false otherwise
st.find(30) != st.end()
false
st.erase(20);
st
{ 24, 25, 26 }
multiset<int> m5{1,1,2,3};
m5
{ 1, 1, 2, 3 }
m5.erase(1);
m5
{ 2, 3 }
multiset<int> m6{1,1,2,3};
m6.erase(m6.begin());
m6
{ 1, 2, 3 }
// not sorted!
std::unordered_set<int> us({1,11,4,2,7});
us
{ 7, 2, 4, 11, 1 }
// a different order now!
us.insert(3);
us
{ 3, 1, 11, 4, 2, 7 }
// same way to check for values
us.count(11)
1
// same way to check for values
us.count(30)
0
set<int> setOps1{1,4,7,11};
set<int> setOps2{1,5,9,11};
set<int> setUnion;
set_union(setOps1.begin(), setOps1.end(), setOps2.begin(), setOps2.end(), std::inserter(setUnion, setUnion.begin()));
setUnion
{ 1, 4, 5, 7, 9, 11 }
set<int> setIntersection;
set_intersection(setOps1.begin(), setOps1.end(), setOps2.begin(), setOps2.end(), std::inserter(setIntersection, setIntersection.begin()));
setIntersection
{ 1, 11 }
set<int> setDifference;
set_difference(setOps1.begin(), setOps1.end(), setOps2.begin(), setOps2.end(), std::inserter(setDifference, setDifference.begin()));
setDifference
{ 4, 7 }
// not limited to sets
vector<int> vOps1{1,4,7,11};
vector<int> vOps2{1,5,9,11};
vector<int> vUnion(10);
auto endIt = set_union(vOps1.begin(), vOps1.end(), vOps2.begin(), vOps2.end(), vUnion.begin());
vUnion
{ 1, 4, 5, 7, 9, 11, 0, 0, 0, 0 }
vector<int> vUnionTrunc(vUnion.begin(), endIt);
vUnionTrunc
{ 1, 4, 5, 7, 9, 11 }
map<std::string, int> m1({{"abc", 6}, {"def", 7}, {"ghi", 6}});
m1
{ "abc" => 6, "def" => 7, "ghi" => 6 }
m1.insert({"jkl", 8});
m1
{ "abc" => 6, "def" => 7, "ghi" => 6, "jkl" => 8 }
m1["mno"] = 11;
m1
{ "abc" => 6, "def" => 7, "ghi" => 6, "jkl" => 8, "mno" => 11 }
bool fncomp(char lhs, char rhs) { return lhs > rhs; }
struct classcomp {
bool operator()(const char &lhs, const char &rhs) const { return lhs > rhs; }
};
void printMap(map<char, int> &m) {
auto mapPtr = m.cbegin();
while (mapPtr != m.cend()) {
cout << mapPtr->first << ": " << mapPtr->second << endl;
mapPtr++;
} // End of WHILE
} // End of printMap
void printMap(map<char, int, classcomp> &m) {
auto mapPtr = m.cbegin();
while (mapPtr != m.cend()) {
cout << mapPtr->first << ": " << mapPtr->second << endl;
mapPtr++;
} // End of WHILE
} // End of printMap
void printMap(map<char, int, bool (*)(char, char)> &m) {
auto mapPtr = m.cbegin();
while (mapPtr != m.cend()) {
cout << mapPtr->first << ": " << mapPtr->second << endl;
mapPtr++;
} // End of WHILE
} // End of printMap
std::map<char, int> first;
first['a'] = 10;
first['a'] = 20;
first['b'] = 50;
first['c'] = 70;
first['d'] = 30;
first
{ 'a' => 20, 'b' => 50, 'c' => 70, 'd' => 30 }
std::map<char, int> second(first.begin(), first.end());
second
{ 'a' => 20, 'b' => 50, 'c' => 70, 'd' => 30 }
std::map<char, int> third(second);
third
{ 'a' => 20, 'b' => 50, 'c' => 70, 'd' => 30 }
// class as Compare
std::map<char, int, classcomp> fourth;
fourth['a'] = 10;
fourth['b'] = 50;
fourth
{ 'b' => 50, 'a' => 10 }
bool (*fn_pt)(char, char) = fncomp;
// function pointer as Compare
std::map<char, int, bool (*)(char, char)> fifth(fn_pt);
fifth['a'] = 10;
fifth['b'] = 50;
fifth['c'] = 70;
fifth['d'] = 30;
fifth
{ 'd' => 30, 'c' => 70, 'b' => 50, 'a' => 10 }
std::map<string, int> sixth;
sixth["abc"] = 4;
sixth["def"] = 5;
sixth["ghi"] = 6;
sixth
{ "abc" => 4, "def" => 5, "ghi" => 6 }
sixth["def"]
5
sixth["blah"]
0
sixth.at("blah")
0
sixth.insert({"jkl", 22});
sixth
{ "abc" => 4, "blah" => 0, "def" => 5, "ghi" => 6, "jkl" => 22 }
sixth.erase("ghi");
sixth
{ "abc" => 4, "blah" => 0, "def" => 5, "jkl" => 22 }
unordered_map<string, int> um({{"abc", 6}, {"def", 7}, {"ghi", 6}});
um
{ "ghi" => 6, "def" => 7, "abc" => 6 }
um.at("ghi")
6
um.at("jkl")
Standard Exception: _Map_base::at
um["jkl"]
0
// returns an iterator to a pair
auto uit = um.find("def");
uit->second
7
multimap<string, int> mm({{"a", 1}, {"a", 3}, {"b", 5}, {"c", 7}, {"c", 8}});
mm
{ "a" => 1, "a" => 3, "b" => 5, "c" => 7, "c" => 8 }
// subscript doesn't work
mm["a"]
input_line_75:2:4: error: no viable overloaded operator[] for type 'multimap<std::string, int>' (aka 'multimap<basic_string<char>, int>') mm["a"] ~~^~~~
Interpreter Error:
// use equal_range to find all values
auto range_its = mm.equal_range("a");
for (auto it = range_its.first; it != range_its.second; ++it) {
cout << "value: " << it->second << endl;
}
value: 1 value: 3
std::string paragraph = R"""(
It was the best of times
it was the worst of times
it was the age of wisdom
it was the age of foolishness
it was the epoch of belief
it was the epoch of incredulity
it was the season of Light
it was the season of Darkness
it was the spring of hope
it was the winter of despair
we had everything before us
we had nothing before us
we were all going direct to Heaven
we were all going direct the other way
in short
the period was so far like the present period
that some of its noisiest authorities insisted on its being received
for good or for evil
in the superlative degree of comparison only
)""";
std::stringstream stream(paragraph);
// count the number of times each word occurs in the input
std::map<string, size_t> word_count; // empty map from string to size_t
std::string word;
while (stream >> word) {
++word_count[word];
}
for (const auto &w : word_count)
cout << w.first << " occurs " << w.second << " times" << endl;
// same thing
for (auto map_it = word_count.cbegin(); map_it != word_count.cend(); map_it++)
cout << map_it->first << " occurs " << map_it->second << " times" << endl;
Darkness occurs 1 times Heaven occurs 1 times It occurs 1 times Light occurs 1 times age occurs 2 times all occurs 2 times authorities occurs 1 times before occurs 2 times being occurs 1 times belief occurs 1 times best occurs 1 times comparison occurs 1 times degree occurs 1 times despair occurs 1 times direct occurs 2 times epoch occurs 2 times everything occurs 1 times evil occurs 1 times far occurs 1 times foolishness occurs 1 times for occurs 2 times going occurs 2 times good occurs 1 times had occurs 2 times hope occurs 1 times in occurs 2 times incredulity occurs 1 times insisted occurs 1 times it occurs 9 times its occurs 2 times like occurs 1 times noisiest occurs 1 times nothing occurs 1 times of occurs 12 times on occurs 1 times only occurs 1 times or occurs 1 times other occurs 1 times period occurs 2 times present occurs 1 times received occurs 1 times season occurs 2 times short occurs 1 times so occurs 1 times some occurs 1 times spring occurs 1 times superlative occurs 1 times that occurs 1 times the occurs 14 times times occurs 2 times to occurs 1 times us occurs 2 times was occurs 11 times way occurs 1 times we occurs 4 times were occurs 2 times winter occurs 1 times wisdom occurs 1 times worst occurs 1 times Darkness occurs 1 times Heaven occurs 1 times It occurs 1 times Light occurs 1 times age occurs 2 times all occurs 2 times authorities occurs 1 times before occurs 2 times being occurs 1 times belief occurs 1 times best occurs 1 times comparison occurs 1 times degree occurs 1 times despair occurs 1 times direct occurs 2 times epoch occurs 2 times everything occurs 1 times evil occurs 1 times far occurs 1 times foolishness occurs 1 times for occurs 2 times going occurs 2 times good occurs 1 times had occurs 2 times hope occurs 1 times in occurs 2 times incredulity occurs 1 times insisted occurs 1 times it occurs 9 times its occurs 2 times like occurs 1 times noisiest occurs 1 times nothing occurs 1 times of occurs 12 times on occurs 1 times only occurs 1 times or occurs 1 times other occurs 1 times period occurs 2 times present occurs 1 times received occurs 1 times season occurs 2 times short occurs 1 times so occurs 1 times some occurs 1 times spring occurs 1 times superlative occurs 1 times that occurs 1 times the occurs 14 times times occurs 2 times to occurs 1 times us occurs 2 times was occurs 11 times way occurs 1 times we occurs 4 times were occurs 2 times winter occurs 1 times wisdom occurs 1 times worst occurs 1 times