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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T23:56:16+00:00 2026-06-08T23:56:16+00:00

My question is related to numeric type conversion in C++. A very common way

  • 0

My question is related to numeric type conversion in C++. A very common way to do that is to use static_cast, for example:

float a;
int b;
a = 3.14;
b = static_cast<int>(a);

Then, how about numeric vector type conversion? Could we continue to use static_cast? I have done the following experiment:

typedef vector<int> IntVector;
typedef vector<float> FloatVector;
IntVector myvector;
myvector.push_back(3);
myvector.push_back(4);
myvector.push_back(5);

// Solution 1 (successful)
FloatVector solution1 ( myvector.begin(), myvector.end() );
for(int i=0; i<solution1.size(); i++)
   cout<<solution1[i]<<endl;
// Solution 2 (failed)
FloatVector solution2;
solution2 = static_cast<FloatVector> (myvector);

It seems that for numeric vector types it is impossible to use static_cast to convert. I was wondering whether there are good solutions to this problem. Thanks!

  • 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-08T23:56:18+00:00Added an answer on June 8, 2026 at 11:56 pm

    The language directly supports conversion from one numeric type to another. You do not even need the static_cast, you could just assign. This conversion involves a logical copying of the value, as opposed to a reinterpretation of the value representation.

    The language does not directly support conversion between arrays of different types, or for that matter of std::vector of different types.

    But as you found, there is some support for copying elements, and then when each element is numeric, the built-in support for numeric type conversion kicks in for each element.

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

Sidebar

Related Questions

Question related to PHP memory-handling from someone not yet very experienced in PHP: If
Another question related to this one . I have a List<SortableObjects> that is the
Quick question related to IIR filter coefficients. Here is a very typical implementation of
As a follow up question related to my question regarding efficient way of storing
my question is kinda related to this question: Numeric Soft Keyboard on Android BUT,
I've a question related to Doctrine 2 and Zend Framework. If you use Zend
I have a question related to jquery. Basically I want to do a very
This is a question related to getting Drupal CCK fields (just in case that
A question related to Regular cast vs. static_cast vs. dynamic_cast : What cast syntax
I have a simple question related to one-line programming. First an example: function test(a)

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.