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: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;
}
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
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;
}
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