473,473 Members | 1,614 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

istream >> (my own string class) - low-level solution?

I'm doing a chapter 12 exercise from Accelerated C++ ... writing a
string-like class which stores its data in a low-level way. My class,
called Str, uses a char array and length variable. I've gotten
everything working that I want working so far, except for

std::istream& operator>>(std::istream&, Str&)

The way my Str class manages itself of course requires that the size
of the char array to store is known when it is allocated. The problem
is I don't know how many characters the word I'm going to store has.
I can count them up by advancing through the stream, then allocate the
char array with the count, but then I can't go back to the beginning
of the word to actually store the characters into the char array once
I've allocated it.

I know I could just move through the characters in the istream and put
them into a vector and then once I've stored a whole word, put that
into my Str, but I don't want to do that because it renders my
"supposed to be low-level" implementation of a string-like class
somewhat high-level...it feels like cheating.

I also have thrown around the idea of writing the method to store the
word in a temp char array. I could start with a char array with only
1 element, and then if the word being read in has another character,
create a new char array with two elements, copy the old one in and add
the next character. If the word is more than 2 character, then create
a new char array with four elements, copy the old one in and add the
next few characters until it is full again, and so on. But I'm
avoiding that solution because then I'm basically reinventing the
point of a vector within my method.

Is there another solution anyone can think of?

Here is all the code for my class:

#ifndef GUARD_STR_H
#define GUARD_STR_H

#include <cstddef>
#include <iostream>
#include <iterator>

class Str {
friend std::istream& operator>>(std::istream&, Str&);

public:
typedef std::size_t size_type;
typedef char* iterator;
typedef const char* const_iterator;

/*----------------------------------------------------------------
------------------- Constructors / Destructor ------------------
----------------------------------------------------------------*/
// default - create empty Str
Str();
// create a Str containing size_type copies of char
Str(size_type, char);
// create a Str from a null-terminated array of char
Str(const char*);
// create a Str from the range denoted by iterators
template <typename In>
Str(In b, In e) {
create(b, e);
}

// destructor - deallocate array
~Str() { uncreate(); }

/*----------------------------------------------------------------
--------------------------- Operators --------------------------
----------------------------------------------------------------*/
char& operator[](size_type);
const char& operator[](size_type) const;
Str& operator+=(const Str&);

/*----------------------------------------------------------------
---------------------------- Methods ----------------------------
----------------------------------------------------------------*/
size_type size() const;
iterator begin();
const_iterator begin() const;
iterator end();
const_iterator end() const;

private:
// store the Str as an array of char
char* data;
// store the number of elements in data
size_type length;

/*----------------------------------------------------------------
---------------------------- Methods ----------------------------
----------------------------------------------------------------*/
void create();
void create(size_type, char);
void create(const_iterator, const_iterator);
void create(size_type);
void uncreate();
};

/*----------------------------------------------------------------
------------------- Constructors / Destructor ------------------
----------------------------------------------------------------*/
inline Str::Str() {
create();
}

inline Str::Str(size_type n, char c) {
create(n, c);
}

inline Str::Str(const char* cp) {
create(cp, cp + std::strlen(cp));
}
/*----------------------------------------------------------------
--------------------------- Operators --------------------------
----------------------------------------------------------------*/
char& Str::operator[](size_type s) {
return *(data + s);
}

const char& Str::operator[](size_type s) const {
return *(data + s);
}

Str& Str::operator+=(const Str& rhs) {
size_type newSize = length + rhs.size();
char temp[newSize];
std::copy(data, data + length, temp);
std::copy(rhs.begin(), rhs.end(), temp + length);
uncreate();
create(temp, temp + newSize);

return *this;
}

Str operator+(const Str& lhs, const Str& rhs) {
Str r = lhs;
r += rhs;
return r;
}

std::ostream& operator<<(std::ostream& os, const Str& s) {
for (Str::size_type i = 0; i != s.size(); ++i) {
os << s[i];
}
return os;
}

// NEEDS TO BE FIXED
std::istream& operator>>(std::istream& is, Str& s) {
}
/*----------------------------------------------------------------
------------------------ Public Methods -------------------------
----------------------------------------------------------------*/
Str::size_type Str::size() const {
return length;
}

Str::iterator Str::begin() {
return data;
}

Str::const_iterator Str::begin() const {
return data;
}

Str::iterator Str::end() {
return data + length;
}

Str::const_iterator Str::end() const {
return data + length;
}
/*----------------------------------------------------------------
------------------------ Private Methods ------------------------
----------------------------------------------------------------*/
inline void Str::create() {
data = 0;
length = 0;
}

void Str::create(size_type n, char c) {
// store our length
length = n;
// allocate the array
data = new char[length];
// initialize the array
for (size_type i = 0; i < length; ++i) {
data[i] = c;
}
}

inline void Str::create(const_iterator b, const_iterator e) {
length = e - b;
data = new char[length];
std::copy(b, e, data);
}

inline void Str::create(size_type n) {
length = n;
data = new char[length];
}

inline void Str::uncreate() {
delete[] data;
length = 0;
}

#endif
Jul 22 '05 #1
3 2298
On 21 Jul 2004 18:39:18 -0700, matthurne <ma***********@yahoo.com> wrote:
I'm doing a chapter 12 exercise from Accelerated C++ ... writing a
string-like class which stores its data in a low-level way. My class,
called Str, uses a char array and length variable. I've gotten
everything working that I want working so far, except for

std::istream& operator>>(std::istream&, Str&)

The way my Str class manages itself of course requires that the size
of the char array to store is known when it is allocated. The problem
is I don't know how many characters the word I'm going to store has.
I can count them up by advancing through the stream, then allocate the
char array with the count, but then I can't go back to the beginning
of the word to actually store the characters into the char array once
I've allocated it.

I know I could just move through the characters in the istream and put
them into a vector and then once I've stored a whole word, put that
into my Str, but I don't want to do that because it renders my
"supposed to be low-level" implementation of a string-like class
somewhat high-level...it feels like cheating.

I also have thrown around the idea of writing the method to store the
word in a temp char array. I could start with a char array with only
1 element, and then if the word being read in has another character,
create a new char array with two elements, copy the old one in and add
the next character. If the word is more than 2 character, then create
a new char array with four elements, copy the old one in and add the
next few characters until it is full again, and so on. But I'm
avoiding that solution because then I'm basically reinventing the
point of a vector within my method.

Is there another solution anyone can think of?


I would use the Str class you are writing. What's missing from your class
is a method that appends a single char to a Str.

Once you add that you can use that in operator>>, something like this

std::istream& operator>>(std::istream& in, Str& s)
{
Str tmp;
char ch;
while (more chars)
{
tmp += ch; // append char to Str
}
if (no error)
s = tmp;
return in;
}

john
Jul 22 '05 #2
On 21 Jul 2004 18:39:18 -0700, ma***********@yahoo.com (matthurne)
wrote:
I'm doing a chapter 12 exercise from Accelerated C++ ... writing a
string-like class which stores its data in a low-level way. My class,
called Str, uses a char array and length variable.
If you don't have a capacity member, it can be hard to write an
efficient operator>>.

I've gotteneverything working that I want working so far, except for

std::istream& operator>>(std::istream&, Str&)

The way my Str class manages itself of course requires that the size
of the char array to store is known when it is allocated. The problem
is I don't know how many characters the word I'm going to store has.
I can count them up by advancing through the stream, then allocate the
char array with the count, but then I can't go back to the beginning
of the word to actually store the characters into the char array once
I've allocated it.
Right, generally you have to add the characters a char at a time.
I know I could just move through the characters in the istream and put
them into a vector and then once I've stored a whole word, put that
into my Str, but I don't want to do that because it renders my
"supposed to be low-level" implementation of a string-like class
somewhat high-level...it feels like cheating.
Depending upon how Str is meant to be used, it might be the best
approach - you don't want to unnecessarily add lots of efficent
infrastructure just to service this one function when you could use
vector instead.
I also have thrown around the idea of writing the method to store the
word in a temp char array. I could start with a char array with only
1 element, and then if the word being read in has another character,
create a new char array with two elements, copy the old one in and add
the next character. If the word is more than 2 character, then create
a new char array with four elements, copy the old one in and add the
next few characters until it is full again, and so on. But I'm
avoiding that solution because then I'm basically reinventing the
point of a vector within my method.
Indeed.
Is there another solution anyone can think of?


Add operator+=(char) to your class, and make it efficient by adding
exponential growth to your string class. e.g.

int capacity;
....

Str& operator+=(char c)
{
if (capacity < size + 1)
growString(1); //might double capacity
buffer[size] = c;
buffer[++size] = '\0'; //null terminate
return *this;
}

The above assumes that the "capacity" is the character capacity, not
memory capacity.

Tom
Jul 22 '05 #3
"John Harrison" <jo*************@hotmail.com> wrote in message news:<opsbiv0sdr212331@andronicus>...
On 21 Jul 2004 18:39:18 -0700, matthurne <ma***********@yahoo.com> wrote:
I'm doing a chapter 12 exercise from Accelerated C++ ... writing a
string-like class which stores its data in a low-level way. My class,
called Str, uses a char array and length variable. I've gotten
everything working that I want working so far, except for

std::istream& operator>>(std::istream&, Str&)

The way my Str class manages itself of course requires that the size
of the char array to store is known when it is allocated. The problem
is I don't know how many characters the word I'm going to store has.
I can count them up by advancing through the stream, then allocate the
char array with the count, but then I can't go back to the beginning
of the word to actually store the characters into the char array once
I've allocated it.

I know I could just move through the characters in the istream and put
them into a vector and then once I've stored a whole word, put that
into my Str, but I don't want to do that because it renders my
"supposed to be low-level" implementation of a string-like class
somewhat high-level...it feels like cheating.

I also have thrown around the idea of writing the method to store the
word in a temp char array. I could start with a char array with only
1 element, and then if the word being read in has another character,
create a new char array with two elements, copy the old one in and add
the next character. If the word is more than 2 character, then create
a new char array with four elements, copy the old one in and add the
next few characters until it is full again, and so on. But I'm
avoiding that solution because then I'm basically reinventing the
point of a vector within my method.

Is there another solution anyone can think of?


I would use the Str class you are writing. What's missing from your class
is a method that appends a single char to a Str.

Once you add that you can use that in operator>>, something like this

std::istream& operator>>(std::istream& in, Str& s)
{
Str tmp;
char ch;
while (more chars)
{
tmp += ch; // append char to Str
}
if (no error)
s = tmp;
return in;
}

john

Aha, well I HAVE implemented the += operator so that's just what I'm
going to do. Thanks!
Jul 22 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

10
by: Oliver S. | last post by:
I've developed a string-class that holds the string in an array which is a member-variable of the class and that has maximum-size which is con- figurable through a template-parameter. If any...
11
by: JustSomeGuy | last post by:
I have a structure typedef struct { string a; string b; } atype; string ABC = ("123"); string DEF = ("456");
15
by: Andrew Brampton | last post by:
Hi, I'm new to using std classes, but I was wondering how I could do the following efficiently with the string class: Say I had a string with delimited values such as:...
23
by: YinTat | last post by:
Hi, I learned C++ recently and I made a string class. A code example is this: class CString { public: inline CString(const char *rhs) { m_size = strlen(rhs);
4
by: Carl Youngblood | last post by:
I imagine this subject has probably been brought up numerous times. Forgive me for bringing it up again. I was googling through old posts on this newsgroup about it and found a good suggestion on...
27
by: djake | last post by:
In the stroustrup C++ programming language (third edition) i can find example like this: string s1= "Hello"; So I imagine string is a standard class. Furthermore the class in the example is...
13
by: Rob Meade | last post by:
Hi all, I have written a small ProperCase function which I would like to make available to our team at work through our common class library. A colleague mentioned that I could write a new...
13
by: Peteroid | last post by:
These don't work (I'm using VS C++.NET 2005 Express with clr:/pure syntax): ostream& operator <<( ostream& output, String^ str ) { output << str ; //compile error return output ; } ...
7
by: craigkenisston | last post by:
I need an special string that have a fixed capacity and that automatically(silently) trims extra space. MySpecialString str = new MySpecialString(10); str = "1234567890ABCDE" ...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.