Home C++ Iostream Functions left C++ left Manipulator Definition In C++, left sets the alignment of a stream to the left. When a value is shorter than the field width from setw, the padding is added on the right, so the text stays at the left edge.
Text is shown left-aligned only after you use left. Without it, numbers and text are aligned to the right inside their field. It comes from <ios>, and setw comes from <iomanip>.
This is a manipulator . You do not call it on its own. You send it into a stream with << or >>, and it changes how that stream behaves from then on (or does one small job, like ending a line).
Syntax Put std::left into the stream and combine it with std::setw.
#include <iomanip>
std::cout << std::left << std::setw(8) << "ab" << '|'; // ab | Call What it does std::cout << std::leftLeft alignment from now on. std::cout << std::left << std::setw(8) << "ab"Prints ab followed by six spaces. std::cout << std::rightGoes back to right alignment.
left has no effect without a width. setw sets the field, and left chooses the side of the padding.
Library declarations ios_base& left(ios_base& str); Parameters Parameter Description strThe stream. It is passed in for you when you write cout << left. You never write this argument yourself.
Return Value Return Value Description ios_base&A reference to the same stream, so you can keep chaining << or >> after left.
Important
left stays on until you call right, but setw applies to only one item. So you set left once and write setw before every item. The width is the smallest size, so a value that is longer than the field is never cut.
C++ Examples 1. Basic 2. With a fill character 3. Numbers Example 1 — Left-Align Text in a Field The bar | shows where the field ends.
#include <iomanip>
#include <iostream>
int main() {
std::cout << std::setw(8) << "ab" << '|' << '\n';
std::cout << std::left << std::setw(8) << "ab" << '|' << '\n';
return 0;
} How It Works By default the field is right-aligned, so six spaces come first and then ab. std::left moves the padding to the right side of the text.The bar | makes the empty space easy to see. Both fields are eight characters wide. Example 2 — left with setfill A dotted line between a label and a value.
#include <iomanip>
#include <iostream>
int main() {
std::cout << std::left << std::setfill('.');
std::cout << std::setw(10) << "Total" << 25 << '\n';
std::cout << std::setw(10) << "Tax" << 5 << '\n';
return 0;
} How It Works setfill('.') chooses dots as padding. Both settings stay on for the next lines.setw(10) makes Total fill ten places. The five dots are added after it, and 25 follows with no width.setw is written again for the next line, because it applies only once. Tax gets seven dots.Example 3 — Left-Aligned Numbers Numbers can be aligned to the left too.
#include <iomanip>
#include <iostream>
int main() {
std::cout << std::left;
std::cout << std::setw(6) << 1 << std::setw(6) << 22 << '|' << '\n';
return 0;
} How It Works left works for numbers in the same way as for text.1 fills a field of six places, so five spaces follow it. 22 gets four spaces.The bar | comes right after the second field and shows that both fields are six characters wide. Frequently Asked Questions What does left do in C++? It aligns padded output to the left of its field, so padding spaces are added after the value.
Why does left do nothing for me? There is no field width. Use setw(n) before the value, because the width is what creates the padding.
Is the default alignment left or right? Right. Without left, values are pushed to the right edge of their field.
Do I need setw for every value after left? Yes. left stays on, but setw applies to just the next value.
How do I go back to right alignment? Put std::right into the stream.
About the author Developer, cloud engineer, and technical writer
I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.
7 people found this page helpful
Helpful Share Copy link Suggestion