tags:

views:

214

answers:

2

The basic_string class was apparently designed as a general purpose container, as I cannot find any text-specific function in its specification except for the c_str() function. Just out of curiosity, have you ever used the std::basic_string container class for anything else than storing human-readable character data?

The reason I ask this is because one often has to choose between making something general or specific. The designers chose to make the std::basic_string class general, but I doubt it is ever used that way.

+1  A: 

yes - I've implemented state machine for 'unsigned int'. To store/compare states basic_string has been used

Dewfy
gosh, that must have been a really complex state machine
anon
@Dewfy: What was the advantage of `std::basic_string` over, say, `std::vector` in this case?
sbi
@sbi benefit is semantical only - using concatentaion over +=
Dewfy
@Neil Butterworth - not complex - we just replaced string literals with them unique ID. Benchmark has shown that operation with unsigned int is faster than unsigned short. That is why this basic_string<unsigned> has been used.
Dewfy
+3  A: 

It was designed as a string class (hence, for example, length() and all those dozens of find functions), but after the introduction of the STL into the std lib it was outfitted to be an STL container, too (hence size() and the iterators, with <algorithm> making all the find functions redundant).

It's main purpose is to store characters, though. Using anything than PODs isn't guaranteed to work (and doesn't work, for example, when using Dinkumware's std lib). Also, the necessary std::char_traits isn't required to be available for anything else than char and wchar_t (although many implementations come with a reasonable implementation of the base template).

In the original standard, the class wasn't required to store its data in a contiguous piece of memory, but this has changed with C++03.

In short, it's mostly useful as a container of characters (a.k.a. "string"), where "character" has a fairly wide definition.

The "wildest" I have used it for is for storing differently encoded strings by using different character types. That way, strings of different encodings are incompatible even if they use the same character size (ASCII and UTF-8) and, e.g., assignment causes compile-time errors.

sbi
The change happened in C++03.
ltcmelo
@ltcmelo: Thanks!
sbi