Click here to Skip to main content
15,886,840 members
Articles / Programming Languages / C++

A C++ StringBuilder Class

Rate me:
Please Sign up or sign in to vote.
4.22/5 (5 votes)
14 Sep 20074 min read 74.5K   600   11  
An atricle introducing a StringBuilder class written in C++
#ifndef _STRING_ELEMENT_H
#define _STRING_ELEMENT_H

#include <string>
#include <sstream>

class StringElement {

private:
	template<typename T> std::string ToString(T &val) {

		std::stringstream ss;
		ss << val;
		std::string retVal;
		ss >> retVal;

		return retVal;
	};

	std::string _value;

public:
	StringElement(int i) : _value(ToString<int>(i)) {
	};
	
	StringElement(double d) : _value(ToString<double>(d)) {
	};
	
	StringElement(char c) : _value(ToString<char>(c)) {
	};
	
	StringElement(char *cp) : _value(cp) {
	};

	StringElement(const std::string s) : _value(s) {
	};
	
	StringElement(long l) : _value(ToString<long>(l)) {
	};
	
	StringElement(float f) : _value(ToString<float>(f)) {
	};
	
	StringElement(short s) : _value(ToString<short>(s)) {
	};
	
	StringElement(bool b) : _value(ToString<bool>(b)) {
	};

	operator std::string() const {

		return _value;
	};
};

#endif _STRING_ELEMENT_H

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


Written By
Web Developer
Israel Israel
I am a software engineer, as well as a research student at The Hebrew University. I love coding, math, computer theory, reading, tennis, guitar, swimming, animals, and - most importantly - my wife and family.

Comments and Discussions