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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T09:47:45+00:00 2026-05-26T09:47:45+00:00

What is the fastest way (if there is any other) to convert a std::vector

  • 0

What is the fastest way (if there is any other) to convert a std::vector from one datatype to another (with the idea to save space)? For example:

std::vector<unsigned short> ----> std::vector<bool> 

we obviously assume that the first vector only contains 0s and 1s. Copying element by element is highly inefficient in case of a really large vector.

Conditional question:
If you think there is no way to do it faster, is there a complex datatype which actually allows fast conversion from one datatype to another?

  • 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-26T09:47:46+00:00Added an answer on May 26, 2026 at 9:47 am
    std::vector<bool> 
    

    Stop.

    A std::vector<bool> is… not. std::vector has a specialization for the use of the type bool, which causes certain changes in the vector. Namely, it stops acting like a std::vector.

    There are certain things that the standard guarantees you can do with a std::vector. And vector<bool> violates those guarantees. So you should be very careful about using them.

    Anyway, I’m going to pretend you said vector<int> instead of vector<bool>, as the latter really complicates things.

    Copying element by element is highly inefficient in case of a really large vector.

    Only if you do it wrong.

    Vector casting of the type you want needs to be done carefully to be efficient.

    If the the source T type is convertible to the destination T, then this is works just fine:

    vector<Tnew> vec_new(vec_old.begin(), vec_old.end());
    

    Decent implementations should recognize when they’ve been given random-access iterators and optimize the memory allocation and loop appropriately.

    The biggest problem for non-convertible types you’ll have for simple types is not doing this:

    std::vector<int> newVec(oldVec.size());
    

    That’s bad. That will allocate a buffer of the proper size, but it will also fill it with data. Namely, default-constructed ints (int()).

    Instead, you should do this:

    std::vector<int> newVec;
    newVec.reserve(oldVec.size());
    

    This reserves capacity equal to the original vector, but it also ensures that no default construction takes place. You can now push_back to your hearts content, knowing that you will never cause reallocation in your new vector.

    From there, you can just loop over each entry in the old vector, doing the conversion as needed.

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

Sidebar

Related Questions

What is the fastest way of transferring few thousand rows of data from one
What is the fastest way you know to convert a floating-point number to an
In a recent question I asked about the fastest way to convert a large
Possible Duplicate: Fastest way to retrieve a <title> in PHP Suppose there is a
Using ADO.NET , what is the fastest way to retrieve data from the database
What is the fastest (or most Pythonic) way to convert x = [False, False,
Fastest way to uniqify a list in Python without preserving order? I saw many
What's the fastest way to get a framebuffer and render to in software on
What is the fastest way to share data structures between Java and C#? I
I am looking the fastest way to draw thousands of individually calculated pixels directly

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.