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 7043427
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T02:17:17+00:00 2026-05-28T02:17:17+00:00

Say, I have a string and a vector of bool s. Based on the

  • 0

Say, I have a string and a vector of bools. Based on the characters in the string, I want to set the corresponding vector indices to true.

std::vector<bool> is_present(256, false);
for (int i = 0; i < str.size(); ++i)
{
    is_present[str[i]] = true;
}

From what I understand, the standard does not define the signed-ness of a char. Depending on the platform, it may be signed or unsigned. On most platforms, signed char will be an 8-bit two’s complement number (-128 to 127), and unsigned char will be an 8-bit unsigned integer (0 to 255).

If that is the case, is there a possibility that str[i] will return a negative number and cause a memory fault in is_present[str[i]]? or is the char getting typecast to vector<bool>::size_type which is unsigned and hence no problems can occur?

Also, is it better to use vector<bool> is_present(pow(2, CHAR_BIT)), false) instead of hardcoding it to 256?

  • 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-05-28T02:17:18+00:00Added an answer on May 28, 2026 at 2:17 am

    Always cast a char to an unsigned char if you want to be definite about the values.

    You can say 1u << CHAR_BIT to get the desired size.

    std::vector<bool> is_present(1u << CHAR_BIT, false);
    for (int i = 0; i < str.size(); ++i)
    {
        is_present[static_cast<unsigned char>(str[i])] = true;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Let's say I have #include <string> #include <vector> using namespace std; struct Student {
Lets say I have a std::vector of const std::string s. std::vector<const std::string> strs; Now
Say I have a string of 16 numeric characters (i.e. 0123456789012345) what is the
Say you have a string 3.4564. A sample set in the database has these
Say I have two containers storing pointers to the same objects: std::list<Foo*> fooList; std::vector<Foo*>
so let's say i have a vector, wich contains 4 elemens [string elements]. I
Let's just say i have a Vector of Strings, and I want to output
I have following requirement, std::vector< std::vector < std::string > > segments; There are unknown
I have a large Set<String> that contains many words, say: aaa, cCc, dDD, AAA,
I have an std:map defined as follows: std:map<std::string country, std::vector<SomeClass> events>; Holds some events

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.