hg_git hg_git - 1 year ago 86
C++ Question

fixed size string type container

I need a fixed size string type for certain requirements. For now I've been doing a simple

array of characters to meet this requirement. However, it is lacking certain stl container-friendly properties that I require now and then. For eg a simple
methods are required quite frequently and also the
property or
method that might return this property or the ability to not manage '/0'. Not the actual size, but the size of string that fills the container.

Now I know I can reimplement all this and I might in the future when I get some time, but for now is there another way to achieve this?

Answer Source

I am assuming that you want to hold the small string in-place, avoiding allocation on heap and indirection when accessing it. In that case, have a look at small_vector from Boost.

typedef boost::container::small_vector<char> small_string;

This would work as the string you want.

UPDATE: It seems that many implementations of standard C++ library use the small string optimization (SSO) for std::string. It means that as long as your string is small enough (for example 22 characters on 64 bit architecture) it will be stored in-place. I strongly suggest you to use standard std::string then.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download