Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8053721
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T07:56:37+00:00 2026-06-05T07:56:37+00:00

This might sound like a basic question, and it’s certainly solvable but i’m looking

  • 0

This might sound like a basic question, and it’s certainly solvable but i’m looking for a quick and elegant solution.

I want to create a collection of reserved words for my program:
{"apple", "orange", "peach"}

It is constant and i want to be able during runtime to check if a string s is a reserved word (f s is part of the set).

I’ve thought about using std::set but i don’t want to add each one of my reserved words to the set manually. Besides, I don’t need the full power of set, for example I don’t need to add new elements or to remove ones.

What is an elegant way of doing it ?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-05T07:56:39+00:00Added an answer on June 5, 2026 at 7:56 am

    You can store the words in an array, then use the std::set range constructor:

    char const* raw_words[] = { "apple", "orange", "peach" };
    
    std::set<std::string> const words(std::begin(raw_words), std::end(raw_words));
    

    This makes use of the new begin and end functions in C++11, but you can also do this in C++03 using pointers to the first and one-past-the-end elements of the array.

    In C++11 you could also use an initializer list to initialize the std::set, though not all compilers support this feature yet.

    Note also that if the contents of the set of words never change, it might be better to simply use a sorted std::vector<std::string> with std::lower_bound and std::binary_search to find elements. You may find that this will perform better.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

this might sound like a stupid question, but i want to clarify a concept:
This might sound like a bit of a silly question but I been looking
This might sound like a reaaaally dumb question but... why do browsers have a
This might sound like an obvious question but I can't seem to find the
This might sound like a very stupid question to some of you, but I
This might sound like a silly question but is there a manual or a
HI, this might sound like a silly question but.. i've created several types in
This might sound like a noob question but this is the first time I'm
This might sound like a silly question, but yesterday none of our sites would
This might sound very very basic 101 type question, but I really need help

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.