void iterate_dict(DBusMessageIter *dict, char *string, uint16_t key_int) { DBusMessageIter dict_entry, sub_dict_entry; printf("{ "); while (dbus_message_iter_get_arg_type(dict) == DBUS_TYPE_DICT_ENTRY) { dbus_message_iter_recurse(dict, &dict_entry); dbus_message_iter_get_basic(&dict_entry, &string); printf("%s=", string); dbus_message_iter_next(&dict_entry); while (dbus_message_iter_get_arg_type(&dict_entry) != DBUS_TYPE_INVALID) { dbus_message_iter_recurse(&dict_entry, &sub_dict_entry); if (dbus_message_iter_get_arg_type(&sub_dict_entry) == DBUS_TYPE_UINT16) { dbus_message_iter_get_basic(&sub_dict_entry, &key_int); printf("%d ", key_int); } else if (dbus_message_iter_get_arg_type(&sub_dict_entry) == DBUS_TYPE_STRING) { dbus_message_iter_get_basic(&sub_dict_entry, &string); printf("%s ", string); } else if (dbus_message_iter_get_arg_type(&sub_dict_entry) == DBUS_TYPE_ARRAY) { iterate_array(&sub_dict_entry); } dbus_message_iter_next(&dict_entry); } dbus_message_iter_next(dict); } printf("}"); }
/* Get dictionary info about the current service and store it */ static void extract_service_properties(DBusMessageIter *dict, struct service_data *service) { DBusMessageIter entry, value, array_item; char *key; char *key_str; uint16_t key_uint16; dbus_bool_t key_bool; while (dbus_message_iter_get_arg_type(dict) == DBUS_TYPE_DICT_ENTRY) { dbus_message_iter_recurse(dict, &entry); dbus_message_iter_get_basic(&entry, &key); printf("\n %s = ", key); if (strcmp(key, "Name") == 0 && strlen(key) < 5) service->name = key; dbus_message_iter_next(&entry); dbus_message_iter_recurse(&entry, &value); /* Check if entry is a dictionary itself */ if (strcmp(key, "Ethernet") == 0 || /* if just strcmp, the .Configuration names don't match * and they are iterated with iterate_array instead*/ strncmp(key, "IPv4", 4) == 0 || strncmp(key, "IPv6", 4) == 0 || strncmp(key, "Proxy", 5) == 0 || strcmp(key, "Provider") == 0) { dbus_message_iter_recurse(&value, &array_item); iterate_dict(&array_item, key_str, key_uint16); } else switch (dbus_message_iter_get_arg_type(&value)) { case DBUS_TYPE_ARRAY: iterate_array(&value); break; case DBUS_TYPE_BOOLEAN: dbus_message_iter_get_basic(&value, &key_bool); printf("%s", key_bool == TRUE ? "True" : "False"); break; case DBUS_TYPE_BYTE: dbus_message_iter_get_basic(&value, &key_uint16); printf("%d", key_uint16); break; case DBUS_TYPE_STRING: dbus_message_iter_get_basic(&value, &key_str); printf("%s", key_str); break; } dbus_message_iter_next(dict); } printf("\n\n"); }
/* sequential container adapter: * stack * queue * priority_queue */ static int sequential_container_op() { std::list<string> authors = {"Miltion", "Shakespeare", "Austen"}; std::vector<const char*> articles = {"a", "an", "the"}; std::list<string> list2(authors); //拷贝构造时,类型必须完全一致 //std::vector<string> articles2 = articles; std::forward_list<std::string> words(articles.cbegin(), articles.cend()); //此时类型可以不一致 std::array<int, 10> a1 = { 1, 2, 3 }; std::array<int, 10> a2 = a1; a2 = { 4, 5, 6 }; iterate_array(a1, "a1: "); iterate_array(a2, "a2: "); cout << endl; //std::swap(a1, a2); a1.swap(a2); iterate_array(a1, "a1: "); iterate_array(a2, "a2: "); cout << endl; words.assign(articles.cbegin(), articles.cend()); words.assign(initializer_list<string>{"wang", "hou", "ren"}); words.assign(3, "wo"); // for (auto &s : words) { // cout << s << " "; // } // cout << endl; string str; str.insert(str.begin(), 'b'); cout << str << endl; std::vector<std::string> svec; svec.insert(svec.end(), 2, "ha"); svec.insert(svec.begin(), { "Aa", "Bb" }); auto end_it = svec.end(); std::vector<std::string> svec2{"hello", "world", "2016"}; #if _cplusplus > 201103L auto ret = svec.insert(end_it-1, svec2.begin()+1, svec2.end()); #endif // if (ret == end_it) // cout << "insert failed" << endl; svec.emplace_back("12345"); svec.emplace(svec.begin() + 1, "67890"); iterate_vector_string(svec); string s1 = svec.back(); string s2 = *--svec.end(); if (s1 == s2) cout << "equal string: " << s1 << endl; string s3 = svec.at(0); string s4 = svec.front(); if (s3 == s4) cout << "equal front string: " << s3 << endl; auto it = svec.erase(svec.end()-1); if (it != svec.end()) cout << *it << endl; else cout << "off-the-end iterator" << endl; std::forward_list<int> f_list; auto f_it = f_list.before_begin(); for (int i = 0; i < 10; ++i) { f_it = f_list.insert_after(f_it, i); // f_list.push_front(i); } for (auto f_curit = f_list.begin(), f_it = f_list.before_begin(); f_curit != f_list.end();) { if (*f_curit % 2) f_curit = f_list.erase_after(f_it); else { f_it = f_curit; ++f_curit; } } for (auto &i : f_list) { cout << i << " "; } cout << endl; vector<int> v1; auto cap = v1.capacity(); v1.insert(v1.begin(), 0); cap = v1.capacity(); v1.reserve(10); cap = v1.capacity(); v1.shrink_to_fit(); cap = v1.capacity(); vector<int> v2{10, 20, 30, 40, 50, 60}; #if _cplusplus > 201103L auto v1_it = v1.insert(v1.begin(), v2.begin(), v2.begin() + (v2.end() - v2.begin()) / 2); //返回插入的第一个元素迭代器 cout << *v1_it << endl; //10 #endif std::forward_list<int> flist2; auto fl2_it = flist2.insert_after(flist2.before_begin(), v2.begin(), v2.begin() + (v2.end() - v2.begin()) / 2); //返回插入的最后一个元素迭代器 cout << *fl2_it << endl; //30 int fl2_v = *flist2.begin(); //移动flist.begin()之后的那个元素移动到了flist3,为什么移动的不是flist2.begin()本身?因为forward_list不能方便的操作迭代器之前的那个元素 std::forward_list<int> flist3{77, 78, 79}; flist3.splice_after(flist3.before_begin(), flist2, flist2.begin()); return 0; }