Extending the string class

Jul 23, 2011 at 4:10pm
What would i have to do to expand on append operator+= . where i can add int,long, and etc. so i don't overwrite string append method.

This is what i have
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
#include <vector>
#include <string>
#include <sstream>

#ifndef ESTRING_HPP
#define ESTRING_HPP

class str;
typedef std::vector<str> svector;
typedef unsigned int uint;

class str : public std::string {
	
public:
	// string constructor
	str() : std::string() { }
	str( const std::string& s ) : std::string( s ) { }
	str( const std::string& s, std::size_t n ) : std::string( s,n ) { }
	str( const char *s, std::size_t n ) : std::string( s,n ) { }
	str( const char *s ) : std::string( s ) { }
	str( std::size_t n, char c ) : std::string ( n,c ) { }
	template <class InputIterator>
	str( InputIterator first, InputIterator last ) : std::string( first,last ) { }

	// extended methods
	svector split(str split=" ", uint limit=0);
	void join(str sept, svector &list); // make new string 
	void ajoin(str sept, svector &list);
	uint count(const str s) const;
	
	str lower();
	str upper();
	
	bool islower() const;
	bool isupper() const;
	bool isint() const;
	bool isfloat() const;
	bool isalpha() const;
	
	// convert str into a number
	template <typename T>
	T convert() { 
		T result = 0;
		if(isint() or isfloat()) {
			std::stringstream stream;
			stream << *this;
			stream >> result;
		}
		return result;
	}
};

#endif 


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
#include <iostream>

#include "estring.hpp"

using namespace std;

// take a line and split it into a vector
svector str::split(str split, uint limit) {
	svector list;
	size_t end,pos = 0;
	
	while(true) {
		end = find(split,pos);
		list.push_back(substr(pos,end-pos));
		pos = end + split.size();
		if(end == string::npos) break;
		if(limit != 0) {
			if(list.size() >= limit) {
				list.push_back(substr(pos));
				break;
			}
		}	
	}
	
	return list;
}

// combine a svector and replace string
void str::join(str sept, svector &list) {
	assign(list[0]);
	for(uint i = 1; i < list.size(); i++) {
		append(sept);
		append(list[i]);
	}
}

// append a svector to the string
void str::ajoin(str sept, svector &list) {
	for(uint i = 0; i < list.size(); i++) {
		append(sept);
		append(list[i]);
	}
}

// counts how many times it appears
uint str::count(const str s) const {
	uint total = 0;
	size_t end,pos = 0;
	
	while(true) {
		end = find(s,pos);
		if(end == string::npos)
			break;
		pos = end + s.size();
		total++;
	}
	return total;
}

bool str::islower() const {
	bool Bool = true;
	if (length() == 0)
		Bool = false;
	else {
		for(const_iterator i = begin(); i != end(); i++) {
			if(*i >= 'A' && *i <= 'Z') {
				Bool = false;
				break;
			}
		}
	}
	return Bool;
}

bool str::isupper() const {
	bool Bool = true;
	if (length() == 0)
		Bool = false;
	else {
		for(const_iterator i = begin(); i != end(); i++) {
			if(*i >= 'a' && *i <= 'z') {
				Bool = false;
				break;
			}
		}
	}
	return Bool;
}

bool str::isint() const {
	bool Bool = true;
	if (length() == 0)
		Bool = false;
	else {
		for(const_iterator i = begin(); i != end(); i++) {
			if(*i < '0' || *i > '9') {
				Bool = false;
				break;
			}
		}
	}
	return Bool;
}

bool str::isfloat() const {
	bool Bool = true;
	if (length() == 0)
		Bool = false;
	else if( count(".") != 1 )
		Bool = false;
	else {
		for(const_iterator i = begin(); i != end(); i++) {
			if((*i < '0' || *i > '9') && *i != '.') {
				Bool = false;
				break;
			}
		}
	}
	return Bool;
}

bool str::isalpha() const {
	bool Bool = true;
	if (length() == 0)
		Bool = false;
	else {
		for(const_iterator i = begin(); i != end(); i++) {
			if((*i < 'a' || *i > 'z') && (*i < 'A' || *i > 'Z') && *i != ' ') {
				Bool = false;
				break;
			}
		}
	}
	return Bool;
}

str str::lower() {
	for(iterator i = begin(); i != end(); i++) {
		if(*i >= 'A' && *i <= 'Z') 
			*i += ('a' - 'A');
	}
	return *this;
}

str str::upper() {
	for(iterator i = begin(); i != end(); i++) {
		if(*i >= 'a' && *i <= 'z') 
			*i -= ('a' - 'A');
	}
	return *this;
}
	
int main() {
	str combine("81.1");
	str test("split this line");
	svector slist;
	float nfloat;
	int nint;
	
	// convert into number
	nfloat = combine.convert<float>(); 
	cout << nfloat << endl;
	// convert into number
	nint = combine.convert<int>();
	cout << nint << endl;
	
	// count how many times it appears in string
	cout << test.count("li") << endl; 
	cout << test.count("e") << endl;
	cout << test.count("I") << endl;
	
	// seeing if its all char
	if(test.isalpha())
		cout << "True" << endl;
	else
		cout << "False" << endl;
	
		
	cout << test << endl;
	// split on a str
	slist = test.split(" ",1);
	for(uint i = 0; i < slist.size(); i++)
		cout << slist[i] << endl;
	
	// join it back to a string
	combine.join(" ", slist);
	cout << combine << endl;
	// appending , and join to end of string
	combine.ajoin(",",slist);
	cout << combine << endl;
	
	// change all letters to uppercase
	cout << test.upper() << endl;
	// change all letters to lowercase
	cout << test.lower() << endl;
	
	return 0;
}
Jul 26, 2011 at 6:52am
I don't understand. Do you want to add str& operator += (int number); to your str class ? What is stopping you ?
Jul 26, 2011 at 10:04am
NEVER DERIVE FROM STD::STRING!!!
Generally, it is a very bad idea to derive from standard class, except thoses designed to derive from(exemple std::exception).
Why? because you don't know if the destructor is virtual(it is generally not) and therefore expose your program to memory leaks.
Here, you should use std::ostringstream if you want to add int, floats... to string, this is the standard way of doing:
http://www.cplusplus.com/reference/iostream/ostringstream/
Topic archived. No new replies allowed.