65 // Iterate through the contact list |
65 // Iterate through the contact list |
66 CxMapIterator iter = cxMapIterator(contacts); |
66 CxMapIterator iter = cxMapIterator(contacts); |
67 cx_foreach(CxMapEntry *, entry, iter) { |
67 cx_foreach(CxMapEntry *, entry, iter) { |
68 cxstring name = cx_strn(entry->key->data, entry->key->len); |
68 cxstring name = cx_strn(entry->key->data, entry->key->len); |
69 const char *phone = entry->value; |
69 const char *phone = entry->value; |
70 printf("%.*s: %s\n", (int) name.length, name.ptr, phone); |
70 printf("%" CX_PRIstr ": %s\n", CX_SFMT(name), phone); |
71 } |
71 } |
72 |
72 |
73 cxMapFree(contacts); |
73 cxMapFree(contacts); |
74 |
74 |
75 return 0; |
75 return 0; |
80 |
80 |
81 In the first part we add several entries to the map. |
81 In the first part we add several entries to the map. |
82 Then the example shows retrieval, updating, and removal of information. |
82 Then the example shows retrieval, updating, and removal of information. |
83 The last part shows how to iterate over the pairs of the map and how to recover the string from the key. |
83 The last part shows how to iterate over the pairs of the map and how to recover the string from the key. |
84 |
84 |
85 In real world situations, however, it is quite unlikely that you will use a map to store string literals. |
85 In real-world situations, however, it is quite unlikely that you will use a map to store string literals. |
86 The next example shows a more realistic program, where it is necessary to store strings based on user input. |
86 The next example shows a more realistic program, where it is necessary to store strings based on user input. |
87 |
87 |
88 ```C |
88 ```C |
89 #include <stdio.h> |
89 #include <stdio.h> |
90 #include <string.h> |
90 #include <string.h> |
140 input[strcspn(input, "\n")] = '\0'; |
140 input[strcspn(input, "\n")] = '\0'; |
141 cxmutstr *phone = cxMapGet(contacts, input); |
141 cxmutstr *phone = cxMapGet(contacts, input); |
142 if (phone == NULL) { |
142 if (phone == NULL) { |
143 puts("No record."); |
143 puts("No record."); |
144 } else { |
144 } else { |
145 printf("Result: %.*s\n", |
145 printf("Result: %" CX_PRIstr "\n", |
146 (int) phone->length, phone->ptr); |
146 CX_SFMT(*phone)); |
147 } |
147 } |
148 } |
148 } |
149 } else if (input[0] == '3') { |
149 } else if (input[0] == '3') { |
150 fputs("Name > ", stdout); |
150 fputs("Name > ", stdout); |
151 if (fgets(input, buffer_size, stdin)) { |
151 if (fgets(input, buffer_size, stdin)) { |
165 cx_foreach(CxMapEntry *, entry, iter) { |
165 cx_foreach(CxMapEntry *, entry, iter) { |
166 cxstring name = cx_strn(entry->key->data, |
166 cxstring name = cx_strn(entry->key->data, |
167 entry->key->len); |
167 entry->key->len); |
168 // ... except that here we get cxmutstr* |
168 // ... except that here we get cxmutstr* |
169 cxmutstr *phone = entry->value; |
169 cxmutstr *phone = entry->value; |
170 printf("%.*s: %.*s\n", |
170 printf("%" CX_PRIstr ": %" CX_PRIstr "\n", |
171 (int) name.length, name.ptr, |
171 CX_SFMT(name), CX_SFMT(*phone)); |
172 (int) phone->length, phone->ptr); |
|
173 } |
172 } |
174 } else if (input[0] == '0') { |
173 } else if (input[0] == '0') { |
175 running = false; |
174 running = false; |
176 } else { |
175 } else { |
177 puts("Please select a menu item.\n"); |
176 puts("Please select a menu item.\n"); |