The py-func
library provides a comprehensive collection of C++ implementations for common string, vector, and array manipulation tasks. This library aims to mimic some of the functionalities found in Python, making it easier to perform these operations in C++.
- String manipulation
- Vector manipulation
- Array manipulation
To use the entire py-func
library, include the master header file py-func.h
in your C++ source files:
#include "py-func.h"
If you only need specific functionalities, include the required headers separately:
#include "count.h"
#include "join.h"
#include "range.h"
#include "replace.h"
#include "slice.h"
#include "split.h"
-
range :
-
slice :
-
replace :
-
count :
-
join :
-
The "range" functionality allows you to create a sequence of numbers or extract a sequence of values from iterable data types such as arrays, template arrays, vectors, and strings.
- Allows you to generate sequences of numbers(
vector
) in C++, similar to Python'srange()
function. You can create sequences with specified start, end, and step values.
-
type
int, long int, long long int, unsigned int, unsigned long int, float, double, long double
-
return type
std::vector<type>
-
usage
// Generate sequence of integers from 0 to 9 with a step of 1 pyfunc::range<int>(0, 10, 1) -> {0, 1, 2, 3, 4, 5, 6, 7, 8, 9} // Generate sequence of unsigned integers from 0 to 8 with a step of 2 pyfunc::range<unsigned int>(0, 10, 2) -> {0, 2, 4, 6, 8} // Generate sequence of long integers from -1 to -5 with a step of -1 pyfunc::range<long int>(-1, -5, -1) -> {1, -2, -3, -4, -5} // Generate sequence of floats from 0.1 to 0.9 with a step of 0.1 pyfunc::range<float>(0, 1, 0.1) -> {0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8, 0.9}
-
example
for (int i: pyfunc::range(1, 10, 2)){ std::cout << i << std::endl; /* 1 3 5 7 9 */ }
- It allows you to extract a subsequence from a given string based on specified start, end, and step values.
- return type
std::string
-
usage
std::string string = "0123456789" pyfunc::slice(string, 0, 5, 1) -> "01234" pyfunc::slice(string, 0, 10, 2) -> "02468" pyfunc::slice(string, -1, -5, -1) -> "9876" pyfunc::slice(string, -1, -10, -2) -> "97531"
-
example
std::string str = "1,2,3,4,5,6"; std::cout << pyfunc::slice(str, 0, str.length(), 2); // 123456
- It allows you to extract a subsequence from a given vector based on specified start, end, and step values.
-
type
any type
-
return type
std::vector<type>
- usage
std::vector<int> numbers = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9} pyfunc::slice(numbers, 0, 5, 1) -> {0, 1, 2, 3, 4} pyfunc::slice(numbers, 0, 10, 2) -> {0, 2, 4, 6, 8} pyfunc::slice(numbers, -1, -5, -1) -> {9, 8, 7, 6} pyfunc::slice(numbers, -1, -10, -2) -> {9, 7, 5, 3, 1}
- It allows you to extract a subsequence from a given array based on specified start, end, and step values.
-
type
any type
-
return type
*any type
-
usage
int numbers[10] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9} pyfunc::slice(numbers, 10, 0, 5, 1) -> {0, 1, 2, 3, 4} pyfunc::slice(numbers, 10, 0, 10, 2) -> {0, 2, 4, 6, 8} pyfunc::slice(numbers, 10, -1, -5, -1) -> {9, 8, 7, 6} pyfunc::slice(numbers, 10, -1, -10, -2) -> {9, 7, 5, 3, 1}
char characters[6] = {'h', 'e', 'l', 'l', 'o', '\0'} pyfunc::slice(characters, 5, 0, 3, 1) -> {'h', 'e', 'l'} pyfunc::slice(characters, 5, -1, -5, -1) -> {'o', 'l', 'l', 'e'}
const char * characters = "hello" pyfunc::slice(characters, 5, 0, 3, 1) -> {'h', 'e', 'l'} pyfunc::slice(characters, 5, -1, -5, -1) -> {'o', 'l', 'l', 'e'}
- Allows you to generate sequences of numbers(
-
- replaces a specified value with another value in Iterable data types eg:- arrays, template arrays, vectors and strings.
-
type1
std::string, char
-
type2
char, std::string
-
return type
std::string
- usage
std::string str = "my name is Name, Name is 10 years old." pyfunc::replace(str, "Name", "john Watson") -> "john Watson is 10 years old.john Watson is 50 years old." pyfunc::replace(str, '1', "5") -> "my name is Name, Name is 50 years old."
-
type
any type
-
return type
std::vector<any type>
-
usage
std::vector<int> numbers = {1, 1, 2, 2, 3, 4, 5, 5, 6} pyfunc::replace(numbers, 1, 100) -> {100, 100, 2, 2, 3, 4, 5, 5, 6} pyfunc::replace(numbers, 5, 1) -> {1, 1, 2, 2, 3, 4, 1, 1, 6}
std::vector<std::string> strs = {"abc", "def", "ghi"} pyfunc::replace(strs, "abc", "replaced") -> {"replaced", "def", "ghi"} pyfunc::replace<std::string>(strs, "def", "replaced") -> {"abc", "def", "replaced"}
-
type
any type
-
return type
*any type
-
usage
int numbers[10] = {0, 0, 0, 1, 2, 3, 3, 4, 4, 5} pyfunc::replace(numbers,10, 0, 100) -> {100, 100, 100, 1, 2, 3, 3, 4, 4, 5}
char characters[6] = {'h', 'e', 'l', 'l', 'o', '\0'} pyfunc::replace(characters, 5, 'l', 'L') -> {'h', 'e', 'L', 'L', 'o', '\0'}
-
type
any type
-
return type
std::array<any type, size>
-
usage
std::array<int, 5> numbers= {0, 0, 0, 1, 2} pyfunc::replace(numbers, 0, 100) -> {100, 100, 100, 1, 2}
std::array<char, 5> characters= {'h', 'e', 'l', 'l', 'o'} pyfunc::replace(characters, 'l', 'L') -> {'h', 'e', 'L', 'L', 'o', '\0'}
-
- Split a std::string into a std::vector< std::string> where each word is a list item
-
type
char, std::string
-
return type
std::vector<std::string>
-
usage
std::string str = "1,2,3,4,5";` pyfunc::split(str, ",") -> {"1", "2", "3", "4", "5"}
std::string str = "1>>432>>3>>4213>>5" pyfunc::split(str, ">>") -> {"1", "432", "3", "4213", "5"} pyfunc::split(str, '>') -> {"1", "", "432", "", "3", "", "4213", "", "5"}
std::string str = "c:/users/user/appdata";` pyfunc::split(str, "/") -> {"c:", "users", "user", "appdata"}
-
- The count method returns the number of elements with the specified value in Iterable. eg:- arrays, template arrays, vectors and strings
-
type
std::string, char
-
type2
int, long int, long long int, unsigned long, unsigned long long, double, float
- usage
std::string str = "11234123" pyfunc::count<int>(str, "1") -> 3 pyfunc::count<long long int>(str, "2") -> 2 pyfunc::count<int>(str, "123") -> 2
-
type
any type
-
type2
int, long int, long long int, unsigned long, unsigned long long, double, float
-
usage
std::vector<int> numbers = {1, 1, 2, 2, 3, 4, 5, 5, 6} pyfunc::count<int, int>(numbers, 1) -> 2 pyfunc::count<int, int>(numbers, 6) -> 1
std::vector<std::string> strs = {"abc", "def", "ghi", "abc", "abc"} pyfunc::count<std::string, long long int>(strs, "abc") -> 3
-
type
any type
-
type2
int, long int, long long int, unsigned long, unsigned long long, double, float
-
return type
unsigned long long
-
usage
int numbers[10] = {0, 0, 0, 1, 2, 3, 3, 4, 4, 1} pyfunc::count<int, int>(numbers, 10, 0) -> 3
char characters[6] = {'h', 'e', 'l', 'l', 'o', '\0'} pyfunc::count<char, int>(characters, 5, 'l') -> 2
-
type
any type
-
type2
int, long int, long long int, unsigned long, unsigned long long, double, float
-
usage
std::array<int, 5> numbers= {0, 0, 0, 1, 2} pyfunc::count<int, 5, int>(numbers, 0) -> 3 pyfunc::count<int, 5, long int>(numbers, 0) -> 3
std::array<char, 5> characters= {'h', 'e', 'l', 'l', 'o'} pyfunc::count<char, 5, int>(characters, 'l') -> 3
-
- The count method returns the number of elements with the specified value in Iterable. eg:- arrays, template arrays, vectors and strings
- type
std::string, char
-
usage
std::string str = "2468"; pyfunc::join(str, ",") -> "2,4,6,8" pyfunc::join(str, "--") -> "2--4--6--8" pyfunc::join(str, '|') -> "2|4|6|8"
- type
std::string, char
-
usage
std::vector<std::string> strs = {"abc","def","ghi","jkl"} pyfunc::join(strs, "+") -> "abc+def+ghi+jkl" pyfunc::join(strs, "_____") -> "abc_____def_____ghi_____jkl" pyfunc::join(strs, '|') -> "abc|def|ghi|jkl"
-
type
std::string, char
-
return type
std::string
-
usage
std::string strs[] = {"abc","def","ghi","jkl"} pyfunc::join(strs, 4, "||||") -> "abc||||def||||ghi||||jkl" pyfunc::join(strs, 4, "-") -> "abc-def-ghi-jkl"
char characters[6] = {'h', 'e', 'l', 'l', 'o', '\0'} pyfunc::join(characters, 5, ',') -> "h,e,l,l,o"
-
type
std::string, char
-
return type
std:string
-
usage
std::array<char, 5> characters = {'0', '0', '0', '1', '2'} pyfunc::join(characters, '=') -> "0=0=0=1=2"
std::array<std::string, 4> strs = {"a","b","c","d"} pyfunc::join(strs, ">") -> "a>b>c>d"