Show Menu
Cheatography

C++ String Tips Cheat Sheet (DRAFT) by

A few tips and tricks for strings manipulation in C++

This is a draft cheat sheet. It is a work in progress and is not finished yet.

<io­man­ip>

setios­flags (ios_b­ase­::f­mtflags mask)
Sets the format flags specified by parameter mask. Basically, the input is a bunch of format flags binary or'd together.
std::cout << std::s­eti­osflags (std::­ios­::s­howbase | std::i­os:­:up­per­case);
reseti­osf­lag­s(i­os_­bas­e::­fmt­flags mask)
Unsets the format flags specified by parameter mask.
setpre­cision (int n)
Sets the decimal precision to be used to format floati­ng-­point values on output operat­ions.
setw(int n)
Behaves as if member width were called with n as argument on the stream on which it is insert­ed/­ext­racted as a manipu­lator.
setfill (char_type c)
Sets c as the stream's fill character.
get_money (money­T& mon, bool intl = false)
Extracts characters from the input stream it is applied to, and interprets them as a monetary expres­sion, which is stored as the value of mon.
put_money (const moneyT­& mon, bool intl = false)
Inserts the repres­ent­ation of mon as a monetary value into the output stream it is applied to.
get_time (struct tm tmb, const charT fmt)
Extracts characters from the input stream it is applied to, and interprets them as time and date inform­ation as specified in argument fmt. The obtained data is stored at the struct tm object pointed by tmb.
put_time (const struct tm tmb, const charT fmt)
Inserts the repres­ent­ation of the time and date inform­ation pointed by tmb, formatting it as specified by argument fmt.
The return value for each of the above is unspec­ified, and they should only be used as stream manipu­lators.
 

Money Manipu­lators

#include <iostream>     // std::cin, std::cout
#include <iomanip>      // std::get_money

int main ()
{
  long double price;
  std::cout << "Please, enter the price: ";
  std::cin >> std::get_money(price);

  if (std::cin.fail()) std::cout << "Error reading price\n";
  else std::cout << "The price entered is: " << price << '\n';

  return 0;
}
The