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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T18:34:14+00:00 2026-05-29T18:34:14+00:00

I want to overload ostream& operator<<(ostream& out, const myType& y) in order to output

  • 0

I want to overload

ostream& operator<<(ostream& out, const myType& y)

in order to output big unsigned integers represented in myType by a vector of unsigned short. So if the vector in question has, say, the elements 1f, a356, 13d5 I want to get the output 1fa35613d5 — right now I only need this for hex or oct output.
In particular, 1, 0, 0 should be output to 100000000. I want to acchieve this by successively outputting the elements of the vector. What I do get with this method, however, is 100, despite the fact that I set

out.width(4);
out.fill('0');
out << std::internal;
out << std::noskipws;

Of course I could first write the ushort to a string and then output that, but I’d prefer to only use formatting instructions for out cause this makes it easier to respect the hex or oct settings of out. Which formatting option am I missing here?

  • 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-29T18:34:15+00:00Added an answer on May 29, 2026 at 6:34 pm

    The following program prints fixed-width hex characters:

    #include <iostream>
    #include <iomanip>
    #include <vector>
    
    int main()
    {
        std::vector<unsigned short int> v { 10, 25, 0, 2000 };
    
        for (auto n : v)
        {
            std::cout << "0x" << std::hex << std::setfill('0')
                      << std::setw(4) << n << std::endl;
        }
    }
    

    Output:

    0x000a
    0x0019
    0x0000
    0x07d0
    

    If you’re writing a formatting function for this, you don’t have to repeat std::hex, as that’s permanent. Preserving the state of an ostream is a bit tricky, though, so perhaps you should look into something like Boost’s state-saver.

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

Sidebar

Related Questions

I want to overload the operator + in a class which has a vector
I want to overload (hijack?) ostream and basic_ostream<unsigned char> so that it stops attempting
ostream &stream; stream.write(SomeUnsignedCharStar, intSize); error C2664 cannot convert parameter 1 from const unsigned char
I want to replace the bitshift bool overload for i/ostream. The current implementation only
I want to overload a cast operator, I have the following piece of code
I want to overload the + operator for adding segments together to form a
I want to overload the operator , so that I can assign my fraction
I have a Card class and I want to overload the > operator to
I want to overload ++ operator to use pre-increment and post-increment using operator overloading
I want to overload the receiver operator so I can do this: someClass >>

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.