Introduction
Searching is where data-structure choice matters. This contact book keeps contacts in a std::map, which makes prefix search a fast lower_bound plus a short walk, and maintains a secondary std::multimap index by city. The tricky part — keeping the index correct when a contact moves city or is deleted — is exactly what real databases do.
Problem statement
+91-99000-11223 are normalised), an e-mail address and a city. Users can add (rejecting duplicates by case-insensitive name and invalid data), edit phone, e-mail or city, delete, search by name prefix, list by city, find by part of a phone number, list everything alphabetically and see counts per city.Objectives
- Use std::map::lower_bound for prefix search
- Maintain a secondary index with std::multimap and equal_range
- Normalise and validate user input
- Keep derived data consistent during updates
Features
- Case-insensitive unique names
- Phone normalisation and e-mail validation
- Prefix, city and phone-digit searches
- Index kept consistent on edit and delete
Concepts demonstrated
- Ordered maps
- Secondary indexes
- lower_bound / equal_range
- Input normalisation
- Data consistency
System architecture
ContactBook stores contacts_ keyed by the lower-cased name (so iteration is alphabetical) and by_city_, a multimap from lower-cased city to name key. starting_with jumps to lower_bound(prefix) and walks forward while keys still start with the prefix. in_city uses equal_range. Every operation that changes a city or deletes a contact first calls unindex to remove the old index entry. main splits command arguments on | so names and cities may contain spaces.
Folder structure
Complete source code
Makefile
What each file does
- src/main.cpp
- Validation helpers, the
ContactBookclass with its two indexes, result printing and the command loop. - Makefile
- Build automation:
makebuilds./app,make testruns the sample session and compares it with the expected output,make cleanremoves build files.
How to run it
Build
Run
Test against the expected output
Expected output
Generated by running the project with tests/sample-input.txt. Your output should match tests/expected-output.txt exactly.
add Asha Rao | 98450 12345 | asha@example.com | Bengaluru add Ravi Kumar | +91-99000-11223 | ravi.k@example.org | Chennai add Arjun Mehta | 9123456789 | arjun@example.net | Bengaluru add Meera Iyer | 12345 | meera@example.com | Mumbai add Meera Iyer | 9988776655 | meera@example | Mumbai add Meera Iyer | 9988776655 | meera@example.com | Mumbai add asha rao | 9000000000 | other@example.com | Pune add Anil Shah | (080) 4123 4567 | anil@example.co.in | Bengaluru list find a find ar city bengaluru phone 6655 edit Arjun Mehta | city | Pune edit Ravi Kumar | email | ravi@ edit Ravi Kumar | phone | 98765-43210 city Bengaluru city Pune delete Anil Shah delete Nobody stats list quit
Testing
- Compare
./app < tests/sample-input.txtwithtests/expected-output.txt. asha raois rejected as a duplicate ofAsha Rao.- After Arjun moves to Pune,
city Bengalurulists only Asha andcity Punelists Arjun. phone 6655finds Meera from the last four digits.
Common errors
A contact still appears under the old city after editing
Remove the old (city, key) pair from the multimap before inserting the new one.
Prefix search misses capitalised names
Normalise both the stored keys and the search prefix to lower case.
Possible improvements
- Save to and load from a CSV file
- Add birthdays and an upcoming-birthdays command
- Support several phone numbers per contact
Additional challenges
- 1Add fuzzy search that tolerates one typo.
- 2Add an index by e-mail domain.
- 3Merge two contacts that turn out to be the same person.
Built and tested it yourself? Mark it to update your dashboard.