-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathInsertion.cpp
More file actions
58 lines (45 loc) · 1.67 KB
/
Insertion.cpp
File metadata and controls
58 lines (45 loc) · 1.67 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
/**
* \file Insertion.cpp
* \brief
*/
#include <StdStream/StdStream.h>
#include <StdTest/StdTest.h>
#include <Stl.h>
//-------------------------------------------------------------------------------------------------
int main(int, char **)
{
// Creating an empty unordered_map
std::unordered_map<std::string, int> wordMap;
// Inserting elements through an initializer_list
wordMap.insert( {{"First", 1}, {"Second", 2}, {"Third", 3}} );
using UOMIterator = std::unordered_map<std::string, int>::iterator;
// Pair of Map Iterator and bool value
std::pair<UOMIterator, bool> result;
// Inserting an element through pair
result = wordMap.insert( std::make_pair<std::string, int>("Second", 6) );
if (result.second == false) {
std::cout << "Element 'Second' not inserted again" <<std::endl;
}
// Inserting an element through value_type
result = wordMap.insert( {"Fourth", 4} );
if (result.second == false) {
std::cout << "Element 'Fourth' not inserted again"<<std::endl;
} else {
// Element inserted sucessfully, so first value in pair
// is the iterator of newly inserted element
std::cout << "Element Inserted : ";
std::cout << result.first->first << "::" << result.first->second << std::endl;
}
std::cout << STD_TRACE_VAR(wordMap) << std::endl;
return EXIT_SUCCESS;
}
//-------------------------------------------------------------------------------------------------
#if OUTPUT
Element 'Second' not inserted again
Element Inserted : Fourth::4
wordMap: std::unordered_map (size=4): {
std::pair: {Third,3}
std::pair: {Second,2}
std::pair: {Fourth,4}
std::pair: {First,1}}
#endif