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

  • Home
  • SEARCH
  • 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 8298127
In Process

The Archive Base Latest Questions

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

I’m trying to format output using the NTL library (a number theory library). One

  • 0

I’m trying to format output using the NTL library (a number theory library). One of the objects is the GF2X object, which is a polynomial represented as a string of coefficients. A quick example:

GF2X a = GF2X(5,1);
a++;
cout<<a;

will yield [1 0 0 0 0 1] which is the same as x^5 + 1. My question is about formatting this output using setw. I want to be able to output various length GF2X objects, prepended by a number, and appended with a string. I’d like my output to look like the following:

  1:    [x x x x x x x x]       string here
 15:    [x x x]                 string here

I would also settle for the right ] to be aligned, which is what I should probably expect if I’m using setw. However, when i use the code (variable names ommitted for simplicity):

    cout << setw(3)<< int <<": "<< setw(35) << GF2X << setw(15) << string << endl;

I get output more like this (some white space removed for compactness)

  1:            [x x x x x x x x]   string here
 15:            [x x x]   string here

In other words, the setw function seems to be treating the entire output of <<GF2X as a single character, and doesn’t seem to actually account for the size of the output string. As you can see from the output I’ve shown you, the left side of the GF2X output is aligned, but the right side isn’t, whereas typically, setw seems to align the right side of outputs.

  • 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-08T15:39:17+00:00Added an answer on June 8, 2026 at 3:39 pm

    You can do it with a modification to the output operator. I imagine it’s written something like this (I don’t know anything about this GF2X class, so this is partially psuedo-code:

    std::ostream & operator<<(std::ostream & os, const GF2X & x)
    {
        os << '[';
        for (int i=0; i<x.num_elements; ++i)
            os << x.get_element(i) << ' ';
        return os << ']';
    }
    

    The problem is that setw will only operate on that first ‘[‘, it doesn’t operate on the whole object (it doesn’t know what the whole object is). You can fix the operator by writing the whole thing to a string (using stringstream or some other means), and then outputting the string. If modifying the operator is not an option for you, then you will need to use a separate helper function to first convert the object to a string (using a stringstream), then output that string to the stream.

    Actually, boost::lexical_cast would come in really handy for you here, since it will do that last for you:

    cout << setw(35) << boost::lexical_cast<std::string>(GF2X);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to understand how to use SyndicationItem to display feed which is
I am reading a book about Javascript and jQuery and using one of the
I'm making a simple page using Google Maps API 3. My first. One marker
I'm trying to select an H1 element which is the second-child in its group
Basically, what I'm trying to create is a page of div tags, each has
I'm new to using the Perl treebuilder module for HTML parsing and can't figure
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I used javascript for loading a picture on my website depending on which small
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function

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.