473,386 Members | 1,706 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

std::istream& operator >> (std::istream& is, String& s)

I have to overload this operator to cin a character array to eventually put the strings in lexicographical order. The [size] is unknown and therefore will change accordingly.

I do not know how to do this
Nov 27 '09 #1
7 5629
Banfa
9,065 Expert Mod 8TB
I think your problem would be more easily understood if you had posted code that included the "passed argument 's'".

Also why are you using an array rather than a vector? Or for that matter why aren't you using string?
Nov 27 '09 #2
I can use vector, but am more familiar with arrays. I hear that vertors are much easier, as the [size] issue is automatically taken care of. Here the the header file:
class String;

// stand alone operators
std::ostream& operator << (std::ostream& os, const String& s);
std::istream& operator >> (std::istream& is, String& s);

int operator == (const String& s1, const String& s2);
int operator != (const String& s1, const String& s2);
int operator < (const String& s1, const String& s2);
int operator <= (const String& s1, const String& s2);
int operator > (const String& s1, const String& s2);
int operator >= (const String& s1, const String& s2);

class String
{

public:
String ( );
explicit String ( const char * cstring );
String ( size_t sz , char init );
String ( const String& source );
~String ( );

String& operator = ( const String& source );
char& operator [] ( size_t i );
const char& operator [] ( size_t i ) const; // const version of []

void Wrap ( const char* cstring );

size_t Size () const;
size_t Length () const;
const char* Cstr () const;

// helper functions
static int StrCmp ( const String& s1 , const String& s2 );
static char* NewStr ( size_t size );

private:
size_t size_; // 1 + size_ = total space allocation
char* data_; // last element data_[size_] = '\0'
};
Nov 27 '09 #3
Banfa
9,065 Expert Mod 8TB
Why have you created your own string class when there is std::string that already has a getline overload for use with an istream?
Nov 28 '09 #4
It is a project for school. Some features: Expandable Size (so input operator can take any string w/o restricting the number of chars.), Equality and comparison operators (so string objects can be compared by client programs), and Constructors, Destructors, and assignment operators to make it a proper type.
Nov 28 '09 #5
Banfa
9,065 Expert Mod 8TB
OK I understand. In your operator you need to read a fixed amount of data at a time, probably 1 char to prevent the function from accidentally reading data destined for a different String.

Having got the character you need to check it to see if it meets your end condition; that is if it is whatever character you are going to say delimits strings, for std::string that is any white space but you could make it anything you want.

If it is you end condition return.

Otherwise add it to the array in your string.

Here is the problem, as you say you have no idea how many characters you are going to get input so you can not allocate enough data in advance, that means that every-time you store a new character in the String the first thing you have to check is if there is enough data to store the character. If there isn't then you must reallocate the Strings internal array.

Here is the crunch, if you store the data you have (size and pointer) that implies that the size is the exact size of the String, every time you store a new character you will need to reallocate, that will be very inefficient (but it would work).

What would be better is to have a array that is at least big enough to store the string but may be bigger. Strings private data should contain 3 members, size, capacity and pointer where size is the actual size of the string and capacity is the current size of the buffer. That way the buffer size can be larger than the string size. Then when you allocate extra data you can expand the buffer by more than a single character which means you wont have to allocate every time you store an extra character.

What allocation algorithm you use is up to you, it could be something simple like +10 characters, I have sometimes used *1.6 of the current size which is a fairly efficient growth algorithm for its complexity.
Nov 28 '09 #6
I had this to start:
for(unsigned int 1 =0; 1 < 100; i++)
{
is >> s[ i ];
}
return is;

This would return the "String index out of bounds" error seen in the code below.

const char& String::operator [] (size_t i) const
{
if (i < size_)
return *(data_ + i);
if (size_ == 0)
{
std::cerr << "** String memory unallocated\n"
<< " Unable to recover\n"
<< " Exiting program\n";
exit (EXIT_FAILURE);
}
std::cerr << "** String index out of bounds\n"
<< " Returning last element\n";
return *(data_ + (size_ - 1));
}
Nov 28 '09 #7
Banfa
9,065 Expert Mod 8TB
Of course it will if you are trying to insert data into the string when it is already at behond its current length.

Firstly you need to read is into a local variable not straight into string.

Secondly you overloaded operator>> can not use the public interface to string defined by operator[] because it needs to be able to tell the string to insert the character allocating new data if required but operator[] doesn't do this, it checks the array index provided and raises an exception if it is out of bounds.

Your overloaded operator>> specifically needs a function that will change the bounds not raise an exception if the are exceeded.
Nov 29 '09 #8

Sign in to post your reply or Sign up for a free account.

Similar topics

7
by: Paul Davis | last post by:
I'd like to overload 'comma' to define a concatenation operator for integer-like classes. I've got some first ideas, but I'd appreciate a sanity check. The concatenation operator needs to so...
30
by: | last post by:
I have not posted to comp.lang.c++ (or comp.lang.c++.moderated) before. In general when I have a C++ question I look for answers in "The C++ Programming Language, Third Edition" by Stroustrup....
2
by: Xavier Decoret | last post by:
The following code does not compoile with gcc-3.2.3 namespace dummy { //++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ // Interface of Foo...
1
by: joesoap | last post by:
Hi can anybody please tell me what is wrong with my ostream operator??? this is the output i get using the 3 attached files. this is the output after i run assignment2 -joesoap #include...
7
by: Nicolay Korslund | last post by:
Hi! I'm having a little trouble with the typecast operator, can anybody help me with the rules for when the this operator is invoked? In the class 'Test' in the code below, the typecast operator...
3
by: KK | last post by:
Hi, im working on this bigInt class. Need help writing algorithm for the operator*, andy help will be appreciated. Thanks in advance bigInt.h...
2
by: Nimmi Srivastav | last post by:
There's a rather nondescript book called "Using Borland C++" by Lee and Mark Atkinson (Que Corporation) which presents a rather good discussion of typecast operator overloading. I am presenting...
6
by: YUY0x7 | last post by:
Hi, I am having a bit of trouble with a specialization of operator<<. Here goes: class MyStream { }; template <typename T> MyStream& operator<<(MyStream& lhs, T const &)
5
by: raylopez99 | last post by:
I need an example of a managed overloaded assignment operator for a reference class, so I can equate two classes A1 and A2, say called ARefClass, in this manner: A1=A2;. For some strange reason...
3
by: y-man | last post by:
Hi, I am trying to get an overloaded operator to work inside the class it works on. The situation is something like this: main.cc: #include "object.hh" #include "somefile.hh" object obj,...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
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,...

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.