Skip to content

Differences

Jeongho Nam edited this page Jun 9, 2016 · 5 revisions

Differences between C++/STL

Let's talk about which differences exist between C++/STL and TypeScript-STL.

Naming convention

TypeScript-STL follows Camel notation in class name.

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.

Map and Set containers have their own specific name following algorithm.

C++/STL TypeScript-STL
std::set std.TreeSet
std::multiset std.TreeMultiSet
std::unordered_set std.HashSet
std::unordered_multiset std.HashMultiSet
std::map std.TreeMap
std::multimap std.TreeMultiMap
std::unordered_map std.HashMap
std::unordered_multimap std.HashMultiMap

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.

However, function names are following snake notation like C++/STL.

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.

Operator Overriding

Substitute operator overriding.

C++/STL TypeScript-STL
operator< less
operator== equal_to

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==.

std.Pair follows the operator overriding protocol.
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 equal_to<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);
		}
	}
}

Using promised operator.

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).

Use promised methods
// 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.equal_to(pair2)); // true, RECOMMENDED
console.log(std.equal_to(pair2)); // true, IT'S OK

Iterator

Operators
C++/STL TypeScript-STL
operator== equal_to
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.equal_to(intArray.end()); it = it.next())
	console.log(it.value); // std::cout << *it << std::endl;
Advance Returns
C++/STL TypeScript-STL
it++; it = it.next();
next(it); it = it.next();
advance(it, 5); it = it.advance(5);

Tree Container

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());
let defaultMap = new std.Map<string, number>(std.greater);
let assignedMap = new std.Map<string, number>(defaultMap.begin(), defaultMap.end(), std.greater);

Hash Container

Clone this wiki locally