The objective of this project is to enable C++ to use python-like constructs. Examples:
- Operator
in
if ("h" in "hola")
cout << " h is in hola" << endl;
if ("q" not_in "hola")
cout << " q is not in hola" << endl;
if (2 in vector<int>{1,2,3})
cout << " 2 is in {1,2,3}" << endl;
if (5 not_in L({1,2,3}))
cout << " 5 is not in {1,2,3}" << endl;
- Vector comprehension (like list comprehensions)
std::vector<int> vals = {6,7,8,9,10};
std::vector<int> v1 = VECTCOMP(2*x, int x, vals);
std::vector<int> v2 = VECTCOMP(2*x, int x, {1,2,3,4,5});
std::vector<int> v3 = VECTCOMPIF(x, int x, vals, if (x%2 == 0));
- Simple split / strip of strings
strip(" has spaces ")
split(" there are some words ")
This project is maintained by Patricio Loncomilla. The code is under MIT license
The codes in this repository require at least C++11 to compile
Each of the .h files, which implement the functionalities, are self contained.
Vector comprehensions are also compatible with range( ) from https://github.com/whoshuu/cpp_range
Use of vector slices is not implemented here, but it is (partially) implemented at https://github.com/ploncomi/expr_vector
Notes:
- Use of
in
andnot_in
operators require using defines for being used:
#define in <in_operator::in>
#define not_in <in_operator::not_in>
// Code using in and not_in
// Note that preexistent variables named "in" and "not_in" will be shadowed by the defines
#undef in
#undef not_in
- Support of
in
andnot_in
operators for new classes requires only to define a function:
bool contains__(const Container& container, const Element& element)
split()
andstrip()
equivalent versions do exist in the Boost libraries, but use of Boost requires more verbose code. Also,split()
in boost can only handle individual characters for splitting strings.