C ++ how to align multiple pieces of data correctly

So I need to send a data column to std::cout , where I also need to display some characters around data such as:

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

where I must correctly substantiate the last right bracket under the letter "s" in "Points" in the column heading.

I put the data as:

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

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

Is there a way to group all of these characters and variables together so that they are all grounded together in the output column?

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

+6
source share
4 answers

Is there a way to group all of these characters and variables together so that they are all grounded together in the output column?

Yes, you can use a small 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 it in the final release:

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

One possibility is to format the elements that make up the field using a string stream, and then write this line correctly.

+2
source

Regarding the adjustment field, you can use it as follows.

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

Regarding width or setw , unfortunately, you have to determine what before each exit.

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 additional information about the data structure you are using, or perhaps you can reverse engineer the data structure, you can use this idea in your answer to πάντα ῥεῖ, for example. you define the class / structure of Point and overwrite the << method for better formatting.

+2
source

C ++ how to properly align multiple pieces of data?

Quick answer: sequentially and separately for each part.


A good start would be to get to know the I / O manipulators and their properties. In your case, when you use setw , it sets the format flag, and the value of this flag is not constant (“sticky”), but it is reset to its initial value. For a more detailed analysis of the reasons for setw after reset see here.

Is there a way to group all of these characters and variables together so that they are all grounded together in the output column?

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

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

The simplest solution would be to write a helper function that performs the requested task, there is no simpler (to me) C ++ language to accomplish what you want.


+1
source

Source: https://habr.com/ru/post/1012505/


All Articles