Namespaces
Variants
Views
Actions

Difference between revisions of "cpp/io/manip/left"

From cppreference.com
< cpp‎ | io‎ | manip
m (Text replace - "/sidebar" to "/navbar")
m (fmt, @@, capitalized 1st letter, {{c}}, ., headers sorted, langlinks)
 
(13 intermediate revisions by 8 users not shown)
Line 1: Line 1:
 
{{cpp/title|left|right|internal}}
 
{{cpp/title|left|right|internal}}
 
{{cpp/io/manip/navbar}}
 
{{cpp/io/manip/navbar}}
{{ddcl list begin}}
+
{{dcl begin}}
{{ddcl list header|ios}}
+
{{dcl header|ios}}
{{ddcl list item | num = 1|
+
{{dcl|num=1|
 
std::ios_base& left( std::ios_base& str );
 
std::ios_base& left( std::ios_base& str );
 
}}
 
}}
{{ddcl list item | num = 2|
+
{{dcl|num=2|
 
std::ios_base& right( std::ios_base& str );
 
std::ios_base& right( std::ios_base& str );
 
}}
 
}}
{{ddcl list item | num = 3|
+
{{dcl|num=3|
 
std::ios_base& internal( std::ios_base& str );
 
std::ios_base& internal( std::ios_base& str );
 
}}
 
}}
{{ddcl list end}}
+
{{dcl end}}
  
Modifies the default positioning of the fill characters. {{tt|left}} and {{tt|right}} apply to any output, {{tt|internal}} applies to integer, floating-point, and monetary output. Has no effect on input.
+
Modifies the positioning of the fill characters in an output stream. {{tt|left}} and {{tt|right}} apply to any type being output, {{tt|internal}} applies to integer, floating-point, and monetary output. Has no effect on input.
  
1) sets the {{tt|adjustfield}} of the stream {{tt|str}} to {{tt|left}} as if by calling {{c|str.setf(std::ios_base::left, std::ios_base::adjustfield)}}
+
@1@ Sets the {{tt|adjustfield}} of the stream {{c|str}} to {{tt|left}} as if by calling {{c|str.setf(std::ios_base::left, std::ios_base::adjustfield)}}.
  
2) sets the {{tt|adjustfield}} of the stream {{tt|str}} to {{tt|right}} as if by calling {{c|str.setf(std::ios_base::right, std::ios_base::adjustfield)}}
+
@2@ Sets the {{tt|adjustfield}} of the stream {{c|str}} to {{tt|right}} as if by calling {{c|str.setf(std::ios_base::right, std::ios_base::adjustfield)}}.
  
3) sets the {{tt|adjustfield}} of the stream {{tt|str}} to {{tt|internal}} as if by calling {{c|str.setf(std::ios_base::internal, std::ios_base::adjustfield)}}
+
@3@ Sets the {{tt|adjustfield}} of the stream {{c|str}} to {{tt|internal}} as if by calling {{c|str.setf(std::ios_base::internal, std::ios_base::adjustfield)}}.
  
This is an I/O manipulator, it may be called with an expression such as {{c|out << std::left}} for any {{tt|out}} of type {{c|std::basic_ostream}} or with an expression such as {{c|in >> std::left}} for any {{tt|in}} of type {{c|std::basic_istream}}.
+
The initial default for standard streams is equivalent to {{tt|right}}.
 +
 
 +
This is an I/O manipulator. It may be called with an expression such as {{c|out << std::left}} for any {{tt|out}} of type {{lc|std::basic_ostream}} or with an expression such as {{c|in >> std::left}} for any {{tt|in}} of type {{lc|std::basic_istream}}.
  
 
===Parameters===
 
===Parameters===
{{param list begin}}
+
{{par begin}}
{{param list item | str | reference to I/O stream }}
+
{{par|str|reference to I/O stream}}
{{param list end}}
+
{{par end}}
  
 
===Return value===
 
===Return value===
 
+
{{c|str}} (reference to the stream after manipulation).
{{tt|str}} (reference to the stream after manipulation)
+
  
 
===Example===
 
===Example===
 
{{example
 
{{example
|
+
|code=
| code=
+
#include <iostream>
+
 
#include <iomanip>
 
#include <iomanip>
 +
#include <iostream>
 
#include <locale>
 
#include <locale>
 +
 
int main()
 
int main()
 
{
 
{
 
     std::cout.imbue(std::locale("en_US.utf8"));
 
     std::cout.imbue(std::locale("en_US.utf8"));
     std::cout << "Left fill:\n" << std::left << std::setfill('*')
+
 
 +
     std::cout << "Default positioning:\n" << std::setfill('*')
 
               << std::setw(12) << -1.23  << '\n'
 
               << std::setw(12) << -1.23  << '\n'
 
               << std::setw(12) << std::hex << std::showbase << 42 << '\n'
 
               << std::setw(12) << std::hex << std::showbase << 42 << '\n'
 +
              << std::setw(12) << std::put_money(123, true) << "\n\n";
 +
             
 +
    std::cout << "Left positioning:\n" << std::left
 +
              << std::setw(12) << -1.23  << '\n'
 +
              << std::setw(12) << 42 << '\n'
 
               << std::setw(12) << std::put_money(123, true) << "\n\n";
 
               << std::setw(12) << std::put_money(123, true) << "\n\n";
  
     std::cout << "Internal fill:\n" << std::internal
+
     std::cout << "Internal positioning:\n" << std::internal
 
               << std::setw(12) << -1.23  << '\n'
 
               << std::setw(12) << -1.23  << '\n'
 
               << std::setw(12) << 42 << '\n'
 
               << std::setw(12) << 42 << '\n'
 
               << std::setw(12) << std::put_money(123, true) << "\n\n";
 
               << std::setw(12) << std::put_money(123, true) << "\n\n";
  
     std::cout << "Right fill:\n" << std::right
+
     std::cout << "Right positioning:\n" << std::right
 
               << std::setw(12) << -1.23  << '\n'
 
               << std::setw(12) << -1.23  << '\n'
 
               << std::setw(12) << 42 << '\n'
 
               << std::setw(12) << 42 << '\n'
 
               << std::setw(12) << std::put_money(123, true) << '\n';
 
               << std::setw(12) << std::put_money(123, true) << '\n';
 
}
 
}
| output=
+
|output=
Left fill:
+
Default positioning:
 +
*******-1.23
 +
********0x2a
 +
***USD *1.23
 +
 
 +
Left positioning:
 
-1.23*******
 
-1.23*******
 
0x2a********
 
0x2a********
 
USD *1.23***
 
USD *1.23***
  
Internal fill:
+
Internal positioning:
 
-*******1.23
 
-*******1.23
 
0x********2a
 
0x********2a
 
USD ****1.23
 
USD ****1.23
  
Right fill:
+
Right positioning:
 
*******-1.23
 
*******-1.23
 
********0x2a
 
********0x2a
Line 76: Line 88:
  
 
===See also===
 
===See also===
{{dcl list begin}}
+
{{dsc begin}}
{{dcl list template | cpp/io/manip/dcl list setw}}
+
{{dsc inc|cpp/io/manip/dsc setw}}
{{dcl list template | cpp/io/manip/dcl list setfill}}
+
{{dsc inc|cpp/io/manip/dsc setfill}}
{{dcl list end}}
+
{{dsc inc|cpp/io/manip/dsc showbase}}
 +
{{dsc end}}
 +
 
 +
{{langlinks|de|es|fr|it|ja|pt|ru|zh}}

Latest revision as of 22:36, 15 September 2023

 
 
 
Input/output manipulators
Floating-point formatting
Integer formatting
Boolean formatting
Field width and fill control
internalleftright
Other formatting
Whitespace processing
Output flushing
(C++20)  

Status flags manipulation
Time and money I/O
(C++11)
(C++11)
(C++11)
(C++11)
Quoted manipulator
(C++14)
 
Defined in header <ios>
(1)
(2)
std::ios_base& internal( std::ios_base& str );
(3)

Modifies the positioning of the fill characters in an output stream. left and right apply to any type being output, internal applies to integer, floating-point, and monetary output. Has no effect on input.

1) Sets the adjustfield of the stream str to left as if by calling str.setf(std::ios_base::left, std::ios_base::adjustfield).
2) Sets the adjustfield of the stream str to right as if by calling str.setf(std::ios_base::right, std::ios_base::adjustfield).
3) Sets the adjustfield of the stream str to internal as if by calling str.setf(std::ios_base::internal, std::ios_base::adjustfield).

The initial default for standard streams is equivalent to right.

This is an I/O manipulator. It may be called with an expression such as out << std::left for any out of type std::basic_ostream or with an expression such as in >> std::left for any in of type std::basic_istream.

Contents

[edit] Parameters

str - reference to I/O stream

[edit] Return value

str (reference to the stream after manipulation).

[edit] Example

#include <iomanip>
#include <iostream>
#include <locale>
 
int main()
{
    std::cout.imbue(std::locale("en_US.utf8"));
 
    std::cout << "Default positioning:\n" << std::setfill('*')
              << std::setw(12) << -1.23  << '\n'
              << std::setw(12) << std::hex << std::showbase << 42 << '\n'
              << std::setw(12) << std::put_money(123, true) << "\n\n";
 
    std::cout << "Left positioning:\n" << std::left
              << std::setw(12) << -1.23  << '\n'
              << std::setw(12) << 42 << '\n'
              << std::setw(12) << std::put_money(123, true) << "\n\n";
 
    std::cout << "Internal positioning:\n" << std::internal
              << std::setw(12) << -1.23  << '\n'
              << std::setw(12) << 42 << '\n'
              << std::setw(12) << std::put_money(123, true) << "\n\n";
 
    std::cout << "Right positioning:\n" << std::right
              << std::setw(12) << -1.23  << '\n'
              << std::setw(12) << 42 << '\n'
              << std::setw(12) << std::put_money(123, true) << '\n';
}

Output:

Default positioning:
*******-1.23
********0x2a
***USD *1.23
 
Left positioning:
-1.23*******
0x2a********
USD *1.23***
 
Internal positioning:
-*******1.23
0x********2a
USD ****1.23
 
Right positioning:
*******-1.23
********0x2a
***USD *1.23

[edit] See also

changes the width of the next input/output field
(function) [edit]
changes the fill character
(function template) [edit]
controls whether prefix is used to indicate numeric base
(function) [edit]