Created my own string class but having couple of problems

OS debian 6.0.2
Compiler gcc 4.4.5

made my string using vector<char>
c_str errors when -> . return only 1 letter
String errors at -> .
but both work fine with (*it).c_str()
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
const char* vstring::c_str() const
{
	string temp;
	for(const_iterator it = begin(); it != end(); it++)
		temp.push_back(*it);
	return temp.c_str();
}

string vstring::String() const
{
	string temp;
	for(const_iterator it = begin(); it != end(); it++)
		temp.push_back(*it);
	return temp;
}


header
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
/* Headers
#include <vector>
#include <string>
#include <sstream> 
 */
#ifndef VSTRING_HPP
#define VSTRING_HPP

class vstring;
typedef std::vector<vstring> vstringlist;

class vstring
{	
	std::vector<char> vstr;
	
	void number(std::stringstream &vstream);
	
public:
	typedef std::vector<char>::iterator iterator;
	typedef std::vector<char>::const_iterator const_iterator;
	typedef std::vector<char>::reverse_iterator reverse_iterator;
	typedef std::vector<char>::const_reverse_iterator const_reverse_iterator;
	
	std::vector<char>::iterator begin() { return vstr.begin(); }
	std::vector<char>::const_iterator begin() const { return vstr.begin(); }
	std::vector<char>::iterator end() { return vstr.end(); }
	std::vector<char>::const_iterator end() const { return vstr.end(); }
	std::vector<char>::reverse_iterator rbegin() { return vstr.rbegin(); }
	std::vector<char>::const_reverse_iterator rbegin() const { return vstr.rbegin(); }
	std::vector<char>::reverse_iterator rend() { return vstr.rend(); }
	std::vector<char>::const_reverse_iterator rend() const { return vstr.rend(); }
	
	vstring() { }
	vstring(const std::string &str);
	vstring(const char c);
	vstring(const char* c);
	vstring(const short n);
	vstring(const unsigned short n);
	vstring(const int n);
	vstring(const unsigned int n);
	vstring(const long n);
	vstring(const unsigned long n);
	vstring(const float n);
	vstring(const double n);
	
	vstring& append(const std::string &str);
	vstring& append(const char c);
	vstring& append(const char* c);
	vstring& append(const short n);
	vstring& append(const unsigned short n);
	vstring& append(const int n);
	vstring& append(const unsigned int n);
	vstring& append(const long n);
	vstring& append(const unsigned long n);
	vstring& append(const float n);
	vstring& append(const double n);
	
	// operator[]
	
	vstringlist split(const vstring &sept=' ', unsigned int limit=0) const;
	vstringlist split_around(const vstring &sept=' ', unsigned int limit=0) const;
	vstringlist split_before(const vstring &sept=' ', unsigned int limit=0) const;
	vstringlist split_after(const vstring &sept=' ', unsigned int limit=0) const;
	vstringlist split_by(const unsigned &by=1, unsigned int limit=0) const;
	
	vstring& join(const vstring &sept, const vstringlist &other, bool aclear=false);
	
	// find return 0 on failure , return pos + 1
	unsigned int find(const vstring &other, unsigned int npos=0) const;
	unsigned int find(const char c, unsigned int npos=0) const;
	
	unsigned int rfind(const vstring &other, unsigned int npos=0) const;
	unsigned int rfind(const char c, unsigned int npos=0) const;
	
	const char* c_str() const;
	std::string String() const;
	
	vstring slice(unsigned int pos) const;
	vstring slice(unsigned int pos,unsigned int npos) const;
	
	unsigned int count(const vstring &str) const;
	
	void clear() { vstr.clear(); }
	unsigned int size() const { return vstr.size(); }
	
	// compare
	
	vstring& replace(const vstring &delim, const vstring &with="");
	
	vstring& insert(unsigned int pos, const vstring &sert);
	vstring& insert(unsigned int pos, const char c);
	
	void erase(unsigned int pos);
	void erase(unsigned int pos,unsigned int npos);
	
	vstring& lower(unsigned int pos=0); // from pos to end
	vstring& lower(unsigned int pos,unsigned int npos);
	vstring& upper(unsigned int pos=0);
	vstring& upper(unsigned int pos,unsigned int npos);
	
	bool islower() const;
	bool isupper() const;
	bool isint() const;
	bool isfloat() const;
	bool isalpha() const;
};

#endif 

Last edited on
string temp;
for(const_iterator it = begin(); it != end(); it++)
temp.push_back(*it);
return temp.c_str();
The pointer returned by std::string::c_str() is only valid as long as the std::string instance doesn't get destructed or modified. temp goes out of scope before the called can even get the pointer.

By the way, what exactly is the point of this class if you're using std::string inside one of the functions?
Last edited on
1. I have std::string in certain areas because.
-a. i have not wrote it out..
-b. using for testing code.
2. people say derive from std::string can cause memory leaks.
3. want something more flexible the std::string

can you give me any ideas for writing my own const char*

1
2
3
4
5
6
7
8
9
class vstring 
{
    vector<char> vstr;
    char* link;
...
};

char** vstring::linkchar() const
   { return &link; } 


handle link to = &vstr[0];
i would have to handle ending char '\0'
Last edited on
As helios put it, it is quite confusing that you use std::string inside your function when you are trying to create your own string class. The use of vector is the same. Using char* or char array in your implementation would be more appropriate.
Last edited on
with vectors i don't have to delete them and can have any non fix size.
i took care of it with these methods

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
void vstring::print() const
{
	for(const_iterator it = begin(); it != end(); it++)
			cout << *it;
}

// must delete[] 
char* vstring::get_char() const
{
	char* c;
	c = new char[size()];
	for(const_iterator it = begin(); it != end(); it++)
		c[it - begin()] = *it;
	c[size()] = '\0';
	return c;
}
Topic archived. No new replies allowed.