The First String
Much of the complexity of the STL stems from the requirements imposed by ISO, which include support for two character types:
char and
wchar_t, and
locales. To qualify as ISO compliant, a Standard Library implementation must support no less than eight console I/O objects: the famous
cout and
cin, as well as
cerr and
clog (frankly, who uses this one anyway?). If that's not enough, each of these four fabs has
wchar_t counterpart:
wcout, wcin, wcerr, and
wclog. Sharov eliminated much of this bloat. The result is a compact and manageable library, at the expense of ISO compliance. Take strings for example. In ISO STL,
std::string is actually a typedef that stands for:
basic_string<char, char_traits<char>, allocator<char> >
If you want to use a
wchar_t string, you need another specialization (i.e., class) called
std::wstring. However, uSTL has only one string class called
ustl::string. It's similar to
std::string but not identical.
ustl::string assumes all your strings are UTF8-encoded, and provides some additional functionality:
- The format() member function formats strings much like sprintf().
- An implicit const char * conversion operator.
Double Standards
Being able to compile the following code is every mobile developer's dream:
int main ()
{
vector<int> v;
v.resize (40);
for (size_t i = 0; i < v.size(); ++ i)
v[i] = i;
v.push_back (87);
v.insert (v.begin() + 20, 555);
v.erase (v.begin() + 3);
}
With uSTL, this dream becomes reality. All it takes is a tiny macro that switches between "standard STL" so to speak, and uSTL:
#if USING_USTL
#include <ustl.h>
using namespace ustl;
#else
#include <vector>
using namespace std;
#endif
There should be no observable difference between the uSTL and ISO STL versions of this program except size: the former uses less code under the hood, so for each vector specialization used, you save about 1 kilobyte of memory. 1 kilobyte doesn't seem like much, but notice that you get it for every specialization:
vector<double>, vector<string>, and so on. More importantly, when using uSTL, you don't need to link your app against the bloated shared libraries/CRTs of ISO STL. Just to give you a clue: libstdc++ loads 918,502 bytes to the main memory while libustl loads only 196,784. That's about 80 percent less!
uSTL supports other STL containers such as list, stack, queue, map, multimap, multiset, and even some TR1 features, e.g., tuple. Another noteworthy feature is the ustl::matrix class which doesn't exist in ISO STL.