Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > istream >> (my own string class) - low-level solution?

Reply
Thread Tools

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

 
 
matthurne
Guest
Posts: n/a
 
      07-22-2004
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:perator[](size_type s) {
return *(data + s);
}

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

Str& Str: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:stream& operator<<(std:stream& 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
 
Reply With Quote
 
 
 
 
John Harrison
Guest
Posts: n/a
 
      07-22-2004
On 21 Jul 2004 18:39:18 -0700, matthurne <(E-Mail Removed)> 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
 
Reply With Quote
 
 
 
 
tom_usenet
Guest
Posts: n/a
 
      07-22-2004
On 21 Jul 2004 18:39:18 -0700, http://www.velocityreviews.com/forums/(E-Mail Removed) (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 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
 
Reply With Quote
 
matthurne
Guest
Posts: n/a
 
      07-22-2004
"John Harrison" <(E-Mail Removed)> wrote in message news:<opsbiv0sdr212331@andronicus>...
> On 21 Jul 2004 18:39:18 -0700, matthurne <(E-Mail Removed)> 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!
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Does the C++ standard define the global function of " istream&operator >>(istream& in, string& str); "? xmllmx C++ 5 06-15-2010 11:57 AM
[iostream] how to get the rest string of a istream mos C++ 3 06-26-2006 03:29 AM
istream and string nnandini8@gmail.com C++ 4 04-22-2006 02:24 AM
Setting mininum string read width in istream Richard Clay C++ 2 04-05-2005 05:50 PM
from std::string to std::istream? Gernot Frisch C++ 4 03-18-2005 02:26 PM



Advertisments