Home

C++ Standard Libraries

You can find libraries at https://cplusplus.com/reference/<library>

The standard libraries are any library that uses namespace std.

iostream

   
cout << "string"; Output to console
  Floats don’t output .0. 99 not 99.0
cout << endl; Outputs new line. Same as \n
cin >> var User input from console
  Inputs are separated by white spaces
  If user enters an invalid input it returns false
  Keeps new lines in the buffer
cin >> var1 >> var2 Multiple inputs on one line
.ignore() Ignores the next character
.get(ch) Gets the next character in the buffer including new
.clear() Clears the flags include end of file(eof) flag

string

   
string str = "str"; If not initialized it will be an empty string
getline(cin, str); Gets all remaining text in the current buffer
  Gets up to the next new line
  Doesn’t include the new line in str
  Removes the ending new line in the buffer
str.size()  
str.at(index) returns char
str.replace(index, length, str2) Replaces starting at index
str.find(str2) Gets the starting index of the string
  Returns string::npos if nothing was found
std::string(1, char) Convert char to a string one 1
Converting to other types  
stoi String to int
stoul String to unsigned int
stod String to double
to_string Converts an int or float to string

String Stream

#include <sstream>

Used to treat a string as a stream. The underlying string doesn’t change.

   
ostringstream Output string stream. Used for <<s
istringstream Input string stream. Used for >>s.
stringstream Input and Output string stream
stringstream ss(string) Opens string stream
ss << "string"; Concatenates to the string stream, but not the constructor string.
ss >> str; Puts the next word into str. Words are separated by spaces.

cmath

   
pow(base, exponent)  
sqrt(x)  
fabs(x) absolute value
ceil(x) Round up
floor(x) Round down
rand() Random int from 0 and RAND_MAX
M_PI  

ctime

cstdlib

   
abs()  
atoi() Converts string to int

iomanip

   
cout << fixed setprecision counts after decimal point
cout << setprecision(num) Limits float digits
  Continually active
  Rounds the output
cout << setw(10) Sets width of output
  Only valid for the value directly after <<
  Aligns output to the right side
  Value larger than setw then it ignored
cout << right; Justify right
cout << left; Justify left

typeinfo

returned Type
d double
c char
i integer
P pointer
K constant

fstream

   
ifstream Reading file
ofstream Writing file. Opens a new one if not found.
fstream Reading and writing
fstream file("name.txt"); Open file
file.open("name.txt"); Also opens the file
file.close(); Closes a file
file.is_open() and file Checks if it opened

optional

Optional is used when there is the possiility that you cannot get any code back?

optional<string> openFile(string fileName) {
	fstream file(fileName);
	if (!file) return {} // No data

	string contents;
	stringstream contents_stream;
	contents_stream << file.rdbuf();
	contents = contents_stream.str();
	file.close();

	return contents;
}

int main() {
	optional<fstream> file = openFile("test.cpp");
	if (!file){
		cout << "Error opening test.cpp" << endl;
		return 1;
	}
}

characters functions

#include <cctype>

Function Description
std::toupper Convert to upper case
std::tolower Convert to lower case
std::isxdigit Is hexadecimal numeric character
std::isupper Is upper case character
std::isspace Is a whitespace character. Spaces, tabs, and new lines
std::ispunct Is a punctuation/special character
std::islower Is lower case character
std::isdigit Is a digit
std::isblank Is a space or tab
std::isalpha Is lower or upper case letter
std::isalnum Is lower, upper, or number