• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 #include <iostream>
2 #include <nlohmann/json.hpp>
3 
4 // simple output function
5 template<typename Map>
output(const char * prefix,const Map & m)6 void output(const char* prefix, const Map& m)
7 {
8     std::cout << prefix << " = { ";
9     for (auto& element : m)
10     {
11         std::cout << element.first << ":" << element.second << ' ';
12     }
13     std::cout << "}" << std::endl;
14 }
15 
main()16 int main()
17 {
18     // create and fill two maps
19     nlohmann::ordered_map<std::string, std::string> m_ordered;
20     m_ordered["one"] = "eins";
21     m_ordered["two"] = "zwei";
22     m_ordered["three"] = "drei";
23 
24     std::map<std::string, std::string> m_std;
25     m_std["one"] = "eins";
26     m_std["two"] = "zwei";
27     m_std["three"] = "drei";
28 
29     // output: m_ordered is ordered by insertion order, m_std is ordered by key
30     output("m_ordered", m_ordered);
31     output("m_std", m_std);
32 
33     // erase and re-add "one" key
34     m_ordered.erase("one");
35     m_ordered["one"] = "eins";
36 
37     m_std.erase("one");
38     m_std["one"] = "eins";
39 
40     // output: m_ordered shows newly added key at the end; m_std is again ordered by key
41     output("m_ordered", m_ordered);
42     output("m_std", m_std);
43 }
44