Use this file to discover all available pages before exploring further.
The C++ Standard Library offers a rich set of facilities for text manipulation and input/output. At the core are std::string (narrow characters) and std::wstring (wide characters), backed by the basic_string class template. For non-owning string references there is std::string_view (C++17). I/O is handled through a hierarchy of stream objects — cin/cout/cerr for console I/O, ifstream/ofstream for file I/O, and stringstream for in-memory I/O. C++20 adds std::format, a type-safe, positional formatting facility that supersedes printf-style format strings.
The <string> header provides free functions to convert between strings and numeric types:
#include <string>#include <iostream>int main(){ // Numeric → string std::string si = std::to_string(42); std::string sd = std::to_string(3.14159); std::cout << si << " " << sd << "\n"; // 42 3.141590 // String → numeric int i = std::stoi("123"); long l = std::stol("456789"); double d = std::stod("3.14"); float f = std::stof("2.72"); std::cout << i << " " << l << " " << d << " " << f << "\n"; // stoi with a base int hex = std::stoi("FF", nullptr, 16); std::cout << "0xFF = " << hex << "\n"; // 255}
std::stoi and friends throw std::invalid_argument if the string cannot be converted, and std::out_of_range if the value overflows the target type. Always wrap them in a try/catch when parsing user input.
string_view (from <string_view>) is a non-owning, read-only reference to a character sequence. It avoids copies when passing string data to functions that only need to read it.
#include <string_view>#include <string>#include <iostream>// Accept any string-like object without copyingvoid print_first_word(std::string_view sv){ auto end = sv.find(' '); std::cout << sv.substr(0, end) << "\n";}int main(){ std::string s = "Hello World"; const char* cstr = "Hello C++"; std::string_view sv = "Hello string_view"; // points to string literal print_first_word(s); // works — no copy of std::string print_first_word(cstr); // works — no copy of C string print_first_word(sv); // works — already a view // string_view does not own the data — never store a view // to a temporary string that will be destroyed}
A string_view does not null-terminate its data. Never pass sv.data() to a C API that expects a null-terminated const char* unless you are certain the underlying buffer is null-terminated.
#include <fstream>#include <string>#include <iostream>#include <stdexcept>#include <vector>int main(){ // Write to a file { std::ofstream ofs("data.txt"); if (!ofs) throw std::runtime_error("Cannot open data.txt for writing"); ofs << "Line 1\n"; ofs << "Line 2\n"; ofs << "Line 3\n"; } // ofs goes out of scope — file is flushed and closed automatically // Read from a file { std::ifstream ifs("data.txt"); if (!ifs) throw std::runtime_error("Cannot open data.txt for reading"); std::string line; while (std::getline(ifs, line)) std::cout << line << "\n"; } // Append to a file { std::ofstream ofs("data.txt", std::ios::app); ofs << "Line 4 (appended)\n"; } // Binary I/O { std::vector<int> data = {1, 2, 3, 4, 5}; std::ofstream bin("data.bin", std::ios::binary); bin.write(reinterpret_cast<const char*>(data.data()), data.size() * sizeof(int)); }}
Always check that a file stream opened successfully with if (!ifs) or ifs.is_open() before performing I/O. Use RAII — let the stream destructor close the file rather than calling close() explicitly.
std::stringstream (and the read-only istringstream / write-only ostringstream) treat an in-memory string as a stream. They are ideal for parsing, formatting, and serialization.
#include <sstream>#include <string>#include <vector>#include <iostream>int main(){ // Build a formatted string using ostringstream std::ostringstream oss; oss << "Name: " << "Alice" << ", Age: " << 30 << ", Score: " << 98.5; std::string result = oss.str(); std::cout << result << "\n"; // Name: Alice, Age: 30, Score: 98.5 // Parse a CSV line using istringstream std::string csv = "10,20,30,40,50"; std::istringstream iss(csv); std::vector<int> nums; std::string token; while (std::getline(iss, token, ',')) nums.push_back(std::stoi(token)); for (int n : nums) std::cout << n << ' '; // 10 20 30 40 50 std::cout << "\n"; // Round-trip: write values into stringstream, read them back std::stringstream ss; ss << 3.14 << " " << 42 << " " << "hello"; double d; int i; std::string s; ss >> d >> i >> s; std::cout << d << " " << i << " " << s << "\n"; // 3.14 42 hello}
std::format (from <format>) provides printf-style brevity with full type safety and extensibility. The format string is a Python-like {} placeholder syntax checked at compile time (in C++20 with std::format; fully constexpr-checked in C++23).
std::format requires /std:c++20 or /std:c++latest. Visual Studio 2019 16.10+ and Visual Studio 2022 both ship a complete <format> implementation. Use std::vformat and std::make_format_args for runtime-variable format strings.
Windows applications often need to handle wide characters. The <iostream> wide counterparts (wcin, wcout, wcerr) handle wchar_t streams:
#include <iostream>#include <string>int main(){ std::wstring name = L"Ångström"; std::wcout << L"Name: " << name << L"\n"; // Read a wide line std::wstring line; std::getline(std::wcin, line);}
You cannot mix narrow (cin/cout) and wide (wcin/wcout) operations on the same stream after the orientation is set. Determine which orientation you need at the start of the program and stick with it.
Use std::string for most text. Use std::wstring when interfacing with Windows APIs that require wchar_t. Use std::string_view (C++17) for read-only access to string data without copying — particularly in function parameters.
std::format vs. sprintf vs. ostringstream
Prefer std::format (C++20) for new code: it is type-safe, locale-aware by default, and more readable than printf-family functions. Use ostringstream when targeting C++17 or earlier. Avoid sprintf/printf in C++ code — they have no type safety.
When to use stringstream vs. format?
Use std::format for building a single formatted string. Use std::stringstream for more complex parsing tasks (tokenizing, reading structured input) where the stream extraction operators (>>) are convenient.