C++ how to right justify multiple pieces of data

人盡茶涼 提交于 2019-12-12 10:45:33

问题


So I have to send to std::cout a column of data where I have to also display some characters around the data like:

    Plot Points
   (0,0), (2,3) 
(1,10), (12,14)

where I have to right justify the last right parenthesis under the letter "s" in "Points" in the column header.

I am putting the data out like:

cout << right << setw(12) << "(" << x1 << "," << y1 << "), (" << x2 << "," << y2 << ")";

But all the examples I have seen seem to show that the right and setw only seem to affect the next piece of data I send to cout, so in this case the "(" only.

Is there any way to group all those characters and variables together to have them all justified together in the column of output?

I'm just learning C++ so expect there is some simple solution I haven't learned yet?


回答1:


Is there any way to group all those characters and variables together to have them all justified together in the column of output?

Yes, you can use a little helper function to build a string:

std::string parentized_pair(int x, int y) {
    std::ostringstream oss;
    oss << "(" << x "," << y << ")";
    return oss.str();
}

and use that one in the final output:

cout << right << setw(12) << parentized_pair(x1,y1) 
     << right << setw(12) << parentized_pair(x2,y2);



回答2:


One possibility is to format the items that make up a field using a stringstream, then write out that string right justified.




回答3:


In regard to adjustfield you can use it as follows.

cout.setf(ios::right, ios::adjustfield);

In regard to width or setw unfortunately you will have to defined that before each output.

Refer to the following example:

#include <iostream>

using namespace std;

int main () {
    cout<<"Current fill character: " << cout.fill() <<", code: " <<(int)cout.fill()<<endl;
    cout<<"Current field width : " << cout.width() <<endl;
    cout<<1<<2<<3<<endl;
    // changing width;
    cout.width(3);
    cout<<"Field width after change : " << cout.width() <<endl;
    cout<<1<<2<<3<<endl;
    cout<<"Width after output : " << cout.width() <<endl;
    // changing width and fill
    cout.width(3);
    cout.fill('_');
    cout<<"Current fill character: " << cout.fill() <<", code: " <<(int)cout.fill()<<endl;
    cout<<1<<2<<3<<endl<<endl;

    cout<<"Setting width before each write:\n";
    cout<<"adjustfield - not set/default:\n";
    for(unsigned i=1; i <= 3; ++i) {
        cout.width(3);
        cout<<i;
    }
    cout<<endl;

    cout<<"adjustfield - left:\n";
    cout.setf(ios::left, ios::adjustfield);
    for(unsigned i=1; i <= 3; ++i) {
        cout.width(3);
        cout<<i;
    }
    cout<<endl;

    cout<<"adjustfield - internal:\n";
    cout.setf(ios::internal, ios::adjustfield);
    for(unsigned i=1; i <= 3; ++i) {
        cout.width(3);
        cout<<i;
    }
    cout<<endl;

    cout<<"adjustfield - right:\n";
    cout.setf(ios::right, ios::adjustfield);
    for(unsigned i=1; i <= 3; ++i) {
        cout.width(3);
        cout<<i;
    }
    cout<<endl;
    return 0;
}
/*
Console output:

Current fill character:  , code: 32
Current field width : 0
123
Field width after change : 3
123
Width after output : 0
Current fill character: _, code: 95
123

Setting width before each write:
adjustfield - not set/default:
__1__2__3
adjustfield - left:
1__2__3__
adjustfield - internal:
__1__2__3
adjustfield - right:
__1__2__3
*/

If you can provide more information about the data structure you use, or if you could maybe redesign your data structure, you might use the idea in the answer of πάντα ῥεῖ, e.g. you define a Point class/structure and overwrite the << method to do better formatting.




回答4:


C++ how to right justify multiple pieces of data?

Quick answer: sequentially and separately for each and every piece.


A good start would be to acquaint yourself with I/O manipulators and their properties. In your case when you apply setw, it sets a format flag and the value of this flag is not persistent ("sticky"), but it is reset to its initial value. For more more in depth analysis on why setw seem to reset after use, see here.

Is there any way to group all those characters and variables together to have them all justified together in the column of output?

Now, for your specific problem, you can try inserting all your data into a std::stringstream object and then apply the wanted property only once, as pointed out in the other answers.

I'm just learning C++ so expect there is some simple solution I haven't learned yet?

The simplest solution would be to write a helper function that perfomrs the wanted task, there is no simpler known (to me) C++ language facility to perform what you want.




来源:https://stackoverflow.com/questions/40706786/c-how-to-right-justify-multiple-pieces-of-data

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!