C++ left Manipulator

Beginner
5 min read
Updated: Nov 2026
3 examples
iomanip

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.

C++
#include <iomanip>

std::cout << std::left << std::setw(8) << "ab" << '|';   // ab      |
CallWhat 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
C++
ios_base& left(ios_base& str);

Parameters

ParameterDescription
strThe stream. It is passed in for you when you write cout << left. You never write this argument yourself.

Return Value

Return ValueDescription
ios_base&A reference to the same stream, so you can keep chaining << or >> after left.

C++ Examples

Example 1 — Left-Align Text in a Field

The bar | shows where the field ends.

C++
#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

  1. By default the field is right-aligned, so six spaces come first and then ab.
  2. std::left moves the padding to the right side of the text.
  3. 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.

C++
#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

  1. setfill('.') chooses dots as padding. Both settings stay on for the next lines.
  2. setw(10) makes Total fill ten places. The five dots are added after it, and 25 follows with no width.
  3. 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.

C++
#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

  1. left works for numbers in the same way as for text.
  2. 1 fills a field of six places, so five spaces follow it. 22 gets four spaces.
  3. The bar | comes right after the second field and shows that both fields are six characters wide.

Frequently Asked Questions

It aligns padded output to the left of its field, so padding spaces are added after the value.
There is no field width. Use setw(n) before the value, because the width is what creates the padding.
Right. Without left, values are pushed to the right edge of their field.
Yes. left stays on, but setw applies to just the next value.
Put std::right into the stream.

Next: noboolalpha

Go back to printing bool values as 1 and 0.

noboolalpha tutorial →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

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