A lightweight, header‑only collection of helpful utilities for C++ projects.
- Features
- Printing Utilities
- Input Utilities
- Math Utilities
- String Utilities
- Random Utilities
- How to Download
- Usage Example
print()– prints without newlineprintln()– prints with newlinefprint()– formatted multi-argument printfprintln()– formatted multi-argument print with newlineprintContainer(container)– prints container elements line-by-lineprintContainer(container, true)– prints container inline
Scan(variable)– reads formatted inputScan(message, variable)– prints message then reads inputScanln(string)– reads full lineScanln(message, string)– prints message then reads full line
PITAUEPHISQRT2LN2LN10
sum(a, b)- returns the sum of two numberssubtract(a, b)- returns the difference between two numbersmultiply(a, b)- returns the product of two numbersdivide(a, b)- returns the result of dividing one number by anotherpower(base, exponent)- raises a base number to a given exponentsquareRoot(value)- returns the square root of a numbern_Root(root, number)- computes the n-th root of a number using iterative approximationabsoluteValue(value)- returns the absolute (non-negative) value of a number
sine(value)- returns the sine of a value (in radians)cosine(value)- returns the cosine of a value (in radians)tangent(value)- returns the tangent of a value (in radians)FromDegreesToRadians(deg)- converts degrees to radiansFromRadiansToDegrees(rad)- converts radians to degrees
Logarithm(base, value)- computes the logarithm of a value with a custom base
IsEven(number)- checks if a number is even (rounded to the nearest integer)IsPalindrome(int)- checks if an integer reads the same forward and backward
Length
FromFeetToMeters()- converts feet to metersFromMetersToFeet()- converts meters to feet
Temperature
FromCelsiusToF()- converts Celsius to FahrenheitFromFahrenheitToC()- converts Fahrenheit to CelsiusFromCelsiusToK()- converts Celsius to KelvinFromKelvinToC()- converts Kelvin to CelsiusFromFahrenheitToK()- converts Fahrenheit to KelvinFromKelvinToF()- converts Kelvin to Fahrenheit
Encapsulated inside the String class.
ToString(value)- converts a value to stringToUpper(str)- returns a converted string to uppercaseToLower(str)- returns a converted string to lowercase
Join(str1, str2, ...)- concatenates two or more strings togetherClear(str)- clears a string (makes it an empty string)SearchFirstSubString(str, subStr)- returns the starting index of the substring or -1 if not found.SearchSubString(str, subStr)- returns a pointer to an array containing the first and last index of a substring (both endpoints included)
IsPalindrome(str)- checks if a string is a palindromeIsAnagram(str1, str2)- checks if two strings could be considered anagrams of eachothers
String::Empty- returns an empy stringString::Lorem- prints out dummy text
RandFloat(min, max)- generates a random floating-point number within the specified range [min, max] (both endpoints included)RandInt(min, max)- generates a random integer within the specified range [min, max] (both endpoints included)RandomBool()- generates a random boolean value (true or false)Random_Num()- generates a random floating-point number in the range [0,1] (both endpoints included)Throw_Dice()- simulates a dice roll and returns a random integer in the range [1,6] (both endpoints included)
You can add utils.h to your project in any of these ways:
- Click Here to go to the page of the header file
- Click Download Raw File
- Save it as utils.h inside your project’s include/ or source folder
git clone https://github.com/usersolvesgits/util.h.gitSimply copy the contents of utils.h into your own header file.
To see how everything works in practice, check the main.cpp file included in the 'code' folder, or by clicking here.
It contains simple, clear examples demonstrating each category of utilities.
Click to expand
// Printing utilities
println("Hello, world!");
fprintln("Sum of 3 + 7 = ", sum(3, 7));
// Container printing
std::vector<int> nums = {1, 2, 3, 4, 5};
printContainer(nums, true);
// Math utilities
println("PI = ", PI);
println("Square root of 49 = ", squareRoot(49));
println("Is 42 even? ", IsEven(42));
println("Log base 2 of 32 = ", Logarithm(2, 32));
// String utilities
println(String::ToUpper("hello"));
println("Is 'racecar' a palindrome? ", String::IsPalindrome("racecar"));
println("Are 'listen' and 'silent' anagrams? ", String::IsAnagram("listen", "silent"));
// Random utilities
println("Random float [0,1]: ", RandFloat());
println("Random int [1,10]: ", RandInt(1, 10));
println("Random bool: ", RandomBool());Sample Output
Hello, world!
Sum of 3 + 7 = 10
1 2 3 4 5
PI = 3.14159
Square root of 49 = 7
Is 42 even? 1
Log base 2 of 32 = 5
HELLO
Is 'racecar' a palindrome? 1
Are 'listen' and 'silent' anagrams? 1
Random float [0,1]: 0.48291
Random int [1,10]: 7
Random bool: 0