-
-
Notifications
You must be signed in to change notification settings - Fork 49
Differences
Let's talk about which differences exist between C++/STL and TypeScript-STL.
C++/STL | TypeScript-STL |
---|---|
std::vector | std.Vector |
std::list | std.List |
std::deque | std.Deque |
std::queue | std.Queue |
std::priority_queue | std.PriorityQueue |
std::stack | std.Stack |
Unlike ordinary STL; C++/STL, TypeScript-STL is following Camel notation on class name. The first letter begins from capital letter and spaced word also begins from capital letter with concatenation.
Those naming notation (Camel) is also adjusted in Exception classes.
In pure JavaScript, Name of Map and Set are reserved for ES6 hash-map and hash-set containers. Thus unlike linear containers like Vector and List who are following same words of C++/STL, Set and Map containers' names are extremely different with C++/STL.
Their name is following their own mapping algorithm. TreeMap is a map container mapping key elements by tree algorithm. Of course, HashMap is a map container mapping key elements by hashing algorithm.
C++/STL | TypeScript-STL |
---|---|
std::bind | std.bind |
std::find_if | std.find_if |
std::vector::push_back | std.Vector.push_back |
std::map::equal_range | std.TreeMap.equal_range |
Class name is following Camel notation, however, function name is following Snake notation which is being followed by C++/STL too.
C++/STL | TypeScript-STL |
---|---|
operator< | less |
operator== | equals |
In C++, operator overriding in a class level is possible. Unlike C++, JavScript doesn't support the operator overriding, so we cannot do such thing like C++ in JavaScript. However, we need similar function for standard comparison to use STL containers. For an example, TreeMap requires comparison method for sorting and constructing a B+ Tree. Of course, many algorithms in STL also requires it, operator overriding.
To substitute the operator overriding, we promise a protocol method. Use less instead of operator<
and use equal_to insteand of operator==
.
namespace std
{
export class Pair<First, Second>
{
public less<U1 extends T1, U2 extends T2>(pair: Pair<U1, U2>): boolean
{
if (std.equal_to(this.first, pair.first) == false)
return std.less(this.first, pair.first);
else
return std.less(this.second, pair.second);
}
public equals<U1 extends T1, U2 extends T2>(pair: Pair<U1, U2>): boolean
{
return std.equal_to(this.first, pair.first) && std.equal_to(this.second, pair.second);
}
}
}
C++/STL | TypeScript-STL Global Method | TypeScript-STL Member Method ----|--------------------------- A < B | std.less(A, B) | A.less(B) A == B | std.equal_to(A, B) | A.equal_to(B) A <= B | std.less_equal(A, B) | A.less(B) OR A.less(B) A > B | std.greater(A, B) | !A.less(B) AND !A.equal_to(B) A >= B | std.greater_equal(A, B) | !A.less(B) A != B | std.not_equal_to(A, B) | !A.equal_to(B)
We promised a method protocol for operator overriding. To keep it, we don't have to use local operator symbol like ==
to objects. Instead of the A == B
, use std.equal_to(A, B)
or A.equal_to(B)
.
// TWO PAIRS HAVE SAME VALUE
let pair1 = std.make_pair("samchon", "Jeongho Nam");
let pair2 = std.make_pair("samchon", "Jeongho Nam");
// VALIDATE
console.log(pair1 == pair2); // false, DON'T USE IT.
console.log(pair.equals(pair2)); // true, RECOMMENDED
console.log(std.equal_to(pair2)); // true, IT'S OK
C++/STL | TypeScript-STL |
---|---|
operator== | equals |
operator* | value |
operator-- | prev |
operator++ | next |
std::vector<int> int_array(5, 1);
for (auto it = int_array.begin(); it != int_array.end(); it++)
std::cout << *it << std::endl;
let intArray = new std.Vector<number>(5, 1);
// for (auto it = intArray.begin(); it != intArray.end(); it++)
for (let it = intArray.begin(); !it.equals(intArray.end()); it = it.next())
console.log(it.value); // std::cout << *it << std::endl;
C++/STL | TypeScript-STL |
---|---|
it++; |
it = it.next(); |
next(it); |
it = it.next(); |
advance(it, 5); |
it = it.advance(5); |
std::map<int, std::string, std::greater<std::string>> default_map;
std::map<int, std::string, std::greater<std::string>> assigned_map(default_map.begin(), default_map.end());
####### TypeScript, custom comparison function in tail parameter
let defaultMap = new std.TreeMap<string, number>(std.greater);
let assignedMap = new std.TreeMap<string, number>(defaultMap.begin(), defaultMap.end(), std.greater);
template <typename Key, typename T>
class Entry
{
private:
Key key;
T value;
public:
// MUST BE SPECIFIED IN TEMPLATE PARAMETER
static size_t hashCode()
{
return std::hash(key);
}
};
std::unordered_set<Entry, Entry::hash> entrySet;
class Entry<Key, T>
{
private key: Key;
private value: T;
// WHEN MEMBER FUNCTION {hash()} is defined, then be used automatically.
public hashCode(): number
{
return std.hash(key);
}
}
let entrySet = new std.HashMap<Entiry>();