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

M

matthurne

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::eek:perator[](size_type s) {
return *(data + s);
}

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

Str& Str::eek:perator+=(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::eek:stream& operator<<(std::eek:stream& os, const Str& s) {
for (Str::size_type i = 0; i != s.size(); ++i) {
os << s;
}
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 = 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
 
J

John Harrison

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
 
T

tom_usenet

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 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.

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
 
M

matthurne

John Harrison said:
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!
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,764
Messages
2,569,564
Members
45,039
Latest member
CasimiraVa

Latest Threads

Top