Keyboard shortcuts

Press or to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

std::to_string

Use case

Build strings from numbers for logging.

Explanation

std::to_string converts numbers to std::string.

Code

#include <cstdint>
#include <iostream>
#include <string>

int main() {
  int segments = 4;
  size_t symbols = 127;

  std::string msg = "Found " + std::to_string(segments) + " segments, " +
                    std::to_string(symbols) + " symbols.";

  std::cout << msg << "\n";

  return 0;
}

View on GitHub.

Output

$ ./src/c++11/build/std-to_string
Found 4 segments, 127 symbols.