473,748 Members | 2,239 Online
Bytes | Software Development & Data Engineering Community
+ 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+=(cons t 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_typ e, char);
void create(const_it erator, const_iterator) ;
void create(size_typ e);
void uncreate();
};

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

inline Str::Str(size_t ype 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.b egin(), 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_iter ator Str::begin() const {
return data;
}

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

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

void Str::create(siz e_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(con st_iterator b, const_iterator e) {
length = e - b;
data = new char[length];
std::copy(b, e, data);
}

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

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

#endif
Jul 22 '05 #1
3 2323
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***********@y ahoo.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:<opsbiv0sd r212331@androni cus>...
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
4841
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 operation would grow the string beyond its maximum size, an exeception would be thrown. This kind of string obviously has superior performance over a std::string because there's never any additional memory-allocation. But before I'm going to re-invent...
11
2243
by: JustSomeGuy | last post by:
I have a structure typedef struct { string a; string b; } atype; string ABC = ("123"); string DEF = ("456");
15
2026
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: "field1,field2,field3,field4" and I wanted to to place those 4 fields into different variables I would do something like: /* Note code quickly pulled from my head - may be wrong :) */
23
4756
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
9106
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 how to add functionality to the string class. Rather than trying to inherit from the string class, the author was suggesting wrapping the string class in another class, like so: >Right !!!, we should not derive from string, but we should wrap...
27
2438
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 initialized with a constructor like this
13
3160
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 class which derived from the String class, add the function there and then we would all use the new class which contained my function and all of the others etc....sounded relatively straight forward...
13
2882
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 ; } istream& operator >>( istream& input, String^ str )
7
2737
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" Console.WriteLine(MySpecialString.ToString()) // output "1234567890" i.e. trimes the "ABCDE"
0
9544
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
9324
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9247
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8243
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6074
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4606
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 last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4874
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3313
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 we have to send another system
2
2783
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.