Implementing Basic Autocomplete

How can I implement a basic autocomplete feature using a list of std::strings?

Implementing a basic autocomplete feature using std::string is a great way to practice string manipulation and searching. Let's create a simple autocomplete function that suggests words from a predefined list based on a user's input prefix.

Here's a step-by-step implementation:

  1. Create a list of words (our dictionary) using std::vector<std::string>.
  2. Write a function that takes a prefix and the dictionary as input.
  3. Use std::string::starts_with() to find matches (C++20 feature).
  4. Return a list of matching words.

Here's the code implementation:

#include <iostream>
#include <string>
#include <vector>
#include <algorithm>

std::vector<std::string> autocomplete(
    const std::string& prefix,
    const std::vector<std::string>& dictionary) {
  std::vector<std::string> suggestions;

  for (const auto& word : dictionary) {
    if (word.starts_with(prefix)) {  
      suggestions.push_back(word);
    }
  }

  return suggestions;
}

int main() {
  std::vector<std::string> dictionary{
    "cat", "dog", "elephant",
    "lion", "tiger", "leopard"
  };

  std::string prefix;
  std::cout << "Enter a prefix: ";
  std::cin >> prefix;

  auto suggestions =
    autocomplete(prefix, dictionary);

  std::cout << "Suggestions:\n";
  for (const auto& suggestion : suggestions) {
    std::cout << suggestion << '\n';
  }
}
Enter a prefix: l
Suggestions:
lion
leopard

Let's break down the autocomplete() function:

  1. It takes a prefix and a dictionary as input.
  2. It creates an empty vector suggestions to store matching words.
  3. It iterates through each word in the dictionary.
  4. If a word starts with the given prefix (using starts_with()), it's added to suggestions.
  5. Finally, it returns the list of suggestions.

In main(), we create a sample dictionary, get a prefix from the user, call autocomplete(), and print the suggestions.

This basic implementation can be enhanced in several ways:

  • Sort the suggestions alphabetically using std::sort().
  • Limit the number of suggestions returned.
  • Implement case-insensitive matching.
  • Use more efficient data structures like tries for larger dictionaries.

Remember, std::string::starts_with() is a C++20 feature. If you're using an older C++ version, you can replace it with:

if (word.substr(0, prefix.length()) == prefix) {
  suggestions.push_back(word);
}

This autocomplete feature demonstrates the power and flexibility of std::string in C++, showcasing methods like starts_with() and how they can be used in practical applications.

A Deeper Look at the std::string Class

A detailed guide to std::string, covering the most essential methods and operators

Questions & Answers

Answers are generated by AI models and may not have been reviewed. Be mindful when running any code on your device.

Efficient String Concatenation in Loops
How can I efficiently concatenate multiple strings in a loop without excessive memory allocation?
Converting String Case in C++
What's the best way to convert all characters in a std::string to uppercase or lowercase?
Splitting a String into a Vector
How can I split a std::string into a vector of substrings based on a delimiter?
Removing Whitespace from a String
What's the most efficient way to remove all whitespace from a std::string?
Case-Insensitive String Comparison
How can I implement a case-insensitive string comparison using std::string?
Replacing All Substrings in a String
What's the best approach to replace all occurrences of a substring within a std::string?
Reversing a String in C++
What's the most efficient way to reverse a std::string?
Validating Email Addresses with Regex
How can I check if a std::string is a valid email address using regular expressions?
Checking if a String is a Palindrome
How can I efficiently check if a std::string is a palindrome?
Calculating Levenshtein Distance
How can I efficiently calculate the Levenshtein distance between two std::strings?
Or Ask your Own Question
Get an immediate answer to your specific question using our AI assistant