Hi, i have the following program to display the input received
separated into any word that has Uppercase letters and all lowercase
words.
For the following input "Upper lower" i get the following
output on the console
Upper
0002A634lower
0002A634
I was expecting just
Upper
lower
Why am i getting the print of some memory address presumably, code
below:
using std::string;
using std::vector;
using std::cin;
using std::cout;
using std::ostream;
using std::endl;
bool IsUpper(const string& s);
ostream& Write(ostream& out, vector<string>& v);
vector<stringGetUpper(vector<string>& v);
int _tmain()
{
vector<stringvec;
string s;
while (cin > s)
vec.push_back(s);
vector<stringUpper = GetUpper(vec);
cout << Write(cout, Upper);
cout << Write(cout , vec);
return 0;
}
ostream& Write(ostream& out, vector<string>& v)
{
for(vector<string>::size_type i =0; i!=v.size(); ++i)
{
out << v[i] << endl;
}
return out;
}
vector<stringGetUpper(vector<string>& v)
{
vector<stringUpper;
vector<string>::iterator iter = v.begin();
while (iter != v.end())
{
if (IsUpper(*iter))
{
Upper.push_back(*iter);
iter = v.erase(iter);
}
else
{
++iter;
}
}
return Upper;
}
bool IsUpper(const string& s)
{
bool ret = false;
typedef string::size_type string_size;
string_size i = 0;
while (i != s.size() && (ret==false))
{
if (isupper(s[i]))
ret = true;
++i;
}
return ret;
} 8 1924
On 2008-01-03 11:50, dev_15 wrote:
Hi, i have the following program to display the input received
separated into any word that has Uppercase letters and all lowercase
words.
For the following input "Upper lower" i get the following
output on the console
Upper
0002A634lower
0002A634
I was expecting just
Upper
lower
Why am i getting the print of some memory address presumably, code
below:
Please post the full program, including header files. We should only
have to copy & paste and compile, not modify.
cout << Write(cout, Upper);
cout << Write(cout , vec);
Write(cout, Upper);
Write(cout, vec);
--
Erik Wikström
Sorry, header files now included
#include <iostream>
#include <string>
#include <vector>
#include <cctype>
using std::string;
using std::vector;
using std::cin;
using std::cout;
using std::ostream;
using std::endl;
bool IsUpper(const string& s);
ostream& Write(ostream& out, vector<string>& v);
vector<stringGetUpper(vector<string>& v);
int _tmain()
{
vector<stringvec;
string s;
while (cin > s)
vec.push_back(s);
vector<stringUpper = GetUpper(vec);
cout << Write(cout, Upper);
cout << Write(cout , vec);
return 0;
}
ostream& Write(ostream& out, vector<string>& v)
{
for(vector<string>::size_type i =0; i!=v.size(); ++i)
{
out << v[i] << endl;
}
return out;
}
vector<stringGetUpper(vector<string>& v)
{
vector<stringUpper;
vector<string>::iterator iter = v.begin();
while (iter != v.end())
{
if (IsUpper(*iter))
{
Upper.push_back(*iter);
iter = v.erase(iter);
}
else
{
++iter;
}
}
return Upper;
}
bool IsUpper(const string& s)
{
bool ret = false;
typedef string::size_type string_size;
string_size i = 0;
while (i != s.size() && (ret==false))
{
if (isupper(s[i]))
ret = true;
++i;
}
return ret;
}
On Jan 3, 5:22*pm, dev_15 <naumansulai...@googlemail.comwrote:
Sorry, header files now included
#include <iostream>
#include <string>
#include <vector>
#include <cctype>
using std::string;
using std::vector;
using std::cin;
using std::cout;
using std::ostream;
using std::endl;
bool IsUpper(const string& s);
ostream& Write(ostream& out, vector<string>& v);
vector<stringGetUpper(vector<string>& v);
int _tmain()
{
* * * * vector<stringvec;
* * * * string s;
* * * * while (cin >*s)
* * * * * * * * vec.push_back(s);
* * * * vector<stringUpper = GetUpper(vec);
* * * * cout << Write(cout, Upper);
* * * * cout << Write(cout , vec);
* *return 0;
}
ostream& Write(ostream& out, vector<string>& v)
{
* * * * for(vector<string>::size_type i =0; i!=v.size(); ++i)
* * * * {
* * * * * * * * out << v[i] << endl;
* * * * }
* * * * return out;
}
vector<stringGetUpper(vector<string>& v)
{
* * * * vector<stringUpper;
* * * * vector<string>::iterator iter = v.begin();
* * * * while (iter != v.end())
* * * * {
* * * * * * * * if (IsUpper(*iter))
* * * * * * * * {
* * * * * * * * * * * * Upper.push_back(*iter);
* * * * * * * * * * * * iter = v.erase(iter);
* * * * * * * * }
* * * * * * * * else
* * * * * * * * {
* * * * * * * * * * * * ++iter;
* * * * * * * * }
* * * * }
* * * * return Upper;
}
bool IsUpper(const string& s)
{
* * * * bool ret = false;
* * * * typedef string::size_type string_size;
* * * * string_size i = 0;
* * * * while (i != s.size() && (ret==false))
* * * * {
* * * * * * * * if (isupper(s[i]))
* * * * * * * * * * * * ret = true;
* * * * * * * * ++i;
* * * * }
* * * * return ret;
}- Hide quoted text -
- Show quoted text -
Hope you have seen the correction. Repeating again
Write(cout, Upper);
Write(cout, vec);
Additional thoughts:
vector<stringUpper = GetUpper(vec);
After this, Upper is used only for printing. We can write it as
const vector<stringUpper(GetUpper(vec));
Also, the for loop inside Write could well be written as
for(vector<string>::size_type i =0, const vector<string>::size_type
j(v.size()); i!=j; ++i)
Thanks,
Balaji.
dev_15 wrote:
Hi, i have the following program to display the input received
separated into any word that has Uppercase letters and all lowercase
words.
For the following input "Upper lower" i get the following
output on the console
Upper
0002A634lower
0002A634
I was expecting just
Upper
lower
Why am i getting the print of some memory address presumably, code
below:
I didn't look all that carefully at your code, but please consider:
std::ostream &operator<<(std::ostream &o,
const std::vector<std::string &v);
and the line
std::cout << v;
probably produces some code that we can think of like this:
operator<<(std::cout, v);
whereas
std::ostream &write(std::ostream &o, const std::vector<std::string&v);
and the line
std::cout << w(std::cout,v);
probably produces some code that we can think of like this:
operator<<(std::cout, write(std::cout,v));
In this case, the call to write will, if written like your code, write
out each element of v to std::cout and then return a reference to
std::cout. The call to operator<< will write out the 'value' of the
reference. I'm going to guess and say that somehow this reference
decays into a pointer of some kind. I'm probably not technically correct
about that.
You may want to play with this example and see if it clarifies things.
#include <iostream>
const int *p(std::ostream &o, const int &t) {
o << "*" << t << "*" << std::endl;
return &t;
}
int main() {
const int a = 55;
std::cout << "$" << p(std::cout, a) << "$" << std::endl;
std::cout << "#" << &a << "#" << std::endl;
}
LR
On Jan 3, 5:50 am, dev_15 <naumansulai...@googlemail.comwrote:
Hi, i have the following program to display the input received
separated into any word that has Uppercase letters and all lowercase
words.
For the following input "Upper lower" i get the following
output on the console
Upper
0002A634lower
0002A634
I was expecting just
Upper
lower
Why am i getting the print of some memory address presumably, code
below:
Because thats what you asked it to do.
>
using std::string;
using std::vector;
using std::cin;
using std::cout;
using std::ostream;
using std::endl;
bool IsUpper(const string& s);
ostream& Write(ostream& out, vector<string>& v);
vector<stringGetUpper(vector<string>& v);
int _tmain()
int main()
{
vector<stringvec;
string s;
while (cin > s)
vec.push_back(s);
read input indefinitely?
>
vector<stringUpper = GetUpper(vec);
cout << Write(cout, Upper);
cout << Write(cout , vec);
Write(...) is being used like an operator, or trying to rather.
Instead of streaming the results of a function, doesn't it make sense
to stream the object(s), in this case the elements in that
std::vector?
Why don't you define an operator<< to stream std::vector< T instead?
( staggered for readability and untested code )
#include <algorithm>
#include <iterator>
// operator<< for std::vector< std::string >
std::ostream&
operator<<( std::ostream& out,
const std::vector< std::string >& v )
{
std::copy( v.begin(),
v.end(),
std::ostream_iterator< std::string >(out, "\n") );
return out;
}
// then:
std::cout << Upper << vec; // so simple, clean
To further improve the above insertion op, template the operator so it
works with std::vectors of any type, not just std::string. Try it -
it'll be eye opening. An operator that can stream a vector of
anything, including a vector of types that don't exist yet (hint:
std::string already has an overload for op<<, same goes with all
primitive types).
template< typename T >
std::ostream&
operator<<( std::ostream& out,
const std::vector< T >& v )
{
...
}
>
return 0;
}
ostream& Write(ostream& out, vector<string>& v)
{
for(vector<string>::size_type i =0; i!=v.size(); ++i)
{
out << v[i] << endl;
}
return out;
}
vector<stringGetUpper(vector<string>& v)
{
vector<stringUpper;
vector<string>::iterator iter = v.begin();
while (iter != v.end())
{
if (IsUpper(*iter))
{
Upper.push_back(*iter);
iter = v.erase(iter);
}
else
{
++iter;
}
}
return Upper;
}
bool IsUpper(const string& s)
{
bool ret = false;
typedef string::size_type string_size;
string_size i = 0;
while (i != s.size() && (ret==false))
{
if (isupper(s[i]))
ret = true;
++i;
}
return ret;
}
Salt_Peter wrote:
On Jan 3, 5:50 am, dev_15 <naumansulai...@googlemail.comwrote:
> while (cin > s) vec.push_back(s);
read input indefinitely?
I don't understand, why would that read input indefinitely?
LR
On Jan 3, 3:44 pm, LR <lr...@superlink.netwrote:
I didn't look all that carefully at your code, but please consider:
std::ostream &operator<<(std::ostream &o,
const std::vector<std::string &v);
That function doesn't exist. And if you actually define it, it
won't be found in a template.
The correct way of handling this is to wrap std::vector in a
user defined class.
--
James Kanze (GABI Software) email:ja*********@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34
James Kanze wrote:
On Jan 3, 3:44 pm, LR <lr...@superlink.netwrote:
>I didn't look all that carefully at your code, but please consider:
>std::ostream &operator<<(std::ostream &o, const std::vector<std::string &v);
That function doesn't exist.
Oops. I should have made that clear.
And if you actually define it, it won't be found in a template.
Sorry, but I'm not sure that I understand what you meant by that. Could
you please expand on that?
The correct way of handling this is to wrap std::vector in a
user defined class.
Do you mean to wrap it if you're going to output it? Or create an
operator<< function? Why is wrapping the correct way?
TIA
LR This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
by: Victor Irzak |
last post by:
Hello,
I have an ABC.
it supports:
ostream & operator <<
I also have a derived class that supports this operator.
How can I call operator << of the base class for derived object??? Is it...
|
by: keit6736 |
last post by:
Hi, I'm using the Borland compiler and I've created two templated
classes in which I've overloaded the ostream << operator. However, when
I try and use the operator on objects of either class I...
|
by: Tim Partridge |
last post by:
I want operator<< to be a friend function of a class inside a namespace, but
I don't know how. For example:
#include <iostream>
namespace ns {
class foo {
public:
// there is something...
|
by: Johannes Barop |
last post by:
Hi,
I want to format the output of a 'std::ostream', but i dont know how to
do it .
Example:
int main()
{
my_out << "Hi.\nI'am a" << " Computer.";
my_out << "Nice.";
|
by: TungstenCoil |
last post by:
Hello,
Has anyone encountered a problem with, or have any ideas around, the
..\$MFT on NTFS becoming corrupted while using std:: ofstream? I had
some code that would run for a few days and...
| |
by: waitan |
last post by:
#include <algorithm>
#include <iostream>
#include <fstream>
#include <string>
#include <vector>
#include <sstream>
#include <iterator>
#include <iomanip>
using namespace std;
|
by: silversurfer2025 |
last post by:
Hello,
I am currently trying to derive a class from ostream (which is giving
output to my GUI), such that I can give my methods either std::cout or
my own outputstream-class to be used as output...
|
by: Peter Nimmo |
last post by:
Hi,
I am writting a windows application that I want to be able to act as if
it where a Console application in certain circumstances, such as error
logging.
Whilst I have nearly got it, it...
|
by: syang8 |
last post by:
Any one can specify the problem of the following code? The compiling
error is on the friend function. If the base class is not inherited
from ostream, or I just remove the friend function from the...
|
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,...
|
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
| |
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
|
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...
|
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...
|
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...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
|
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |
by: bsmnconsultancy |
last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...
| |