std::ostringstream builds a formatted string before sending it somewhere else.

output string stream An output string stream collects formatted pieces into one string value.

Stringstream Formatting

quantity
stringstream_formatting.cpp
Replay: real traced execution (multi-file project)
#include <iostream>
#include <sstream>
#include <string>

int main() {
    int quantity = 3;
    int unitPrice = 4;

    std::ostringstream builder;
    builder << "items=" << quantity;
    builder << ",total=" << quantity * unitPrice;

    std::string receipt = builder.str();

    std::cout << receipt << std::endl;
    std::cout << "length=" << receipt.size() << std::endl;
    return 0;
}
#include <iostream>
#include <sstream>
#include <string>

int main() {
    int quantity = 1;
    int unitPrice = 4;

    std::ostringstream builder;
    builder << "items=" << quantity;
    builder << ",total=" << quantity * unitPrice;

    std::string receipt = builder.str();

    std::cout << receipt << std::endl;
    std::cout << "length=" << receipt.size() << std::endl;
    return 0;
}
#include <iostream>
#include <sstream>
#include <string>

int main() {
    int quantity = 5;
    int unitPrice = 4;

    std::ostringstream builder;
    builder << "items=" << quantity;
    builder << ",total=" << quantity * unitPrice;

    std::string receipt = builder.str();

    std::cout << receipt << std::endl;
    std::cout << "length=" << receipt.size() << std::endl;
    return 0;
}
  1. quantity ← 3, unitPrice ← 4, builder ← (empty), receipt ← items=3,total=12

    5int main() {6    int quantity→ 3 = 3; //@quantity=1, 57    int unitPrice→ 4 = 4;89    std::ostringstream builder→ (empty);10    builder(empty) << "items=" << quantity3;11    builder(empty) << ",total=" << quantity3 * unitPrice4;1213    std::string receipt→ items=3,total=12 = builder(empty).str();1415    std::cout << receiptitems=3,total=12 << std::endl;16    std::cout << "length=" << receiptitems=3,total=12.size() << std::endl;17    return 0;18}
    outputitems=3,total=12
    length=16
  1. quantity ← 1, unitPrice ← 4, builder ← (empty), receipt ← items=1,total=4

    5int main() {6    int quantity→ 1 = 1;7    int unitPrice→ 4 = 4;89    std::ostringstream builder→ (empty);10    builder(empty) << "items=" << quantity1;11    builder(empty) << ",total=" << quantity1 * unitPrice4;1213    std::string receipt→ items=1,total=4 = builder(empty).str();1415    std::cout << receiptitems=1,total=4 << std::endl;16    std::cout << "length=" << receiptitems=1,total=4.size() << std::endl;17    return 0;18}
    outputitems=1,total=4
    length=15
  1. quantity ← 5, unitPrice ← 4, builder ← (empty), receipt ← items=5,total=20

    5int main() {6    int quantity→ 5 = 5;7    int unitPrice→ 4 = 4;89    std::ostringstream builder→ (empty);10    builder(empty) << "items=" << quantity5;11    builder(empty) << ",total=" << quantity5 * unitPrice4;1213    std::string receipt→ items=5,total=20 = builder(empty).str();1415    std::cout << receiptitems=5,total=20 << std::endl;16    std::cout << "length=" << receiptitems=5,total=20.size() << std::endl;17    return 0;18}
    outputitems=5,total=20
    length=16