libStatGen Software  1
STLUtilities Namespace Reference

This file is inspired by the poor quality of string support in STL for what should be trivial capabiltiies, for example setting or appending the ASCII representation of a floating point or integer number to a string. More...

Functions

int Tokenize (std::vector< std::string > &result, const char *input, char delimiter)
std::string & append (std::string &s, float f)
 use std streams API to do float conversion to string, then append it.
std::string & append (std::string &s, double f)
 use std streams API to do double conversion to string, then append it.
std::string & append (std::string &s, char c)
 The rest we can handle readily ourselves.
std::string & append (std::string &s, unsigned char c)
 Similar to signed char case, but this time for unsigned.
std::string & append (std::string &s, const char *rhs)
 Now append a full C style NUL terminated string to the std::string.
std::string & append (std::string &s, std::string &rhs)
 Prevent the generic template from picking up std::string.
template<typename T >
std::string & append (std::string &s, std::vector< T > v, std::string separator="")
 iterate over the provided vector, appending all elements with an optional separator
template<typename T >
std::string & append (std::string &s, T i)
 This template handles the rest of the cases for integral types.
std::string & operator<< (std::string &s, char c)
std::string & operator<< (std::string &s, unsigned char c)
std::string & operator<< (std::string &s, uint64_t i)
std::string & operator<< (std::string &s, int64_t i)
template<typename T >
std::string & operator<< (std::string &s, T val)
template<typename S >
std::string & append (std::string &s, std::vector< std::string > v, S delimeter, bool itemize=false)
template<typename T , typename S >
std::string & append (std::string &s, std::vector< T > v, S delimeter, bool itemize=false)

Detailed Description

This file is inspired by the poor quality of string support in STL for what should be trivial capabiltiies, for example setting or appending the ASCII representation of a floating point or integer number to a string.

This file uses variadic templates to implement a type safe version (subset) of C-library printf.

Therefore, -std=c++0x is a required option on g++


Function Documentation

std::string& STLUtilities::append ( std::string &  s,
char  c 
) [inline]

The rest we can handle readily ourselves.

Unlike std::string operator +, this operator treats c as a character and appends the ASCII character c.

Definition at line 75 of file STLUtilities.h.

{
    s += c;
    return s;
}
template<typename T >
std::string& STLUtilities::append ( std::string &  s,
i 
)

This template handles the rest of the cases for integral types.

Not user friendly if you pass in a type T that is for example a std::vector.

Definition at line 128 of file STLUtilities.h.

{
    char digits[20];
    char *p = digits;
    bool negative = false;

    if (i<0)
    {
        negative = true;
        i = -i;
    }

    do
    {
        *p++ = '0' + i % 10;
        i = i/10;
    }
    while (i);

    if (negative) s += '-';

    do
    {
        s += *--p;
    }
    while (p > digits);

    return s;
}
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends