A Deeper Look at the std::string Class

Implementing Basic Autocomplete

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

Abstract art representing computer programming

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.

Answers to questions are automatically generated and may not have been reviewed.

A computer programmer
Part of the course:

Professional C++

Comprehensive course covering advanced concepts, and how to use them on large-scale projects.

Free, unlimited access

This course includes:

  • 125 Lessons
  • 550+ Code Samples
  • 96% Positive Reviews
  • Regularly Updated
  • Help and FAQ
Free, Unlimited Access

Professional C++

Comprehensive course covering advanced concepts, and how to use them on large-scale projects.

Screenshot from Warhammer: Total War
Screenshot from Tomb Raider
Screenshot from Jedi: Fallen Order
Contact|Privacy Policy|Terms of Use
Copyright © 2024 - All Rights Reserved