 |
C++Talk.NET C++ language newsgroups
|
| View previous topic :: View next topic |
| Author |
Message |
Eric Lilja Guest
|
Posted: Mon Dec 20, 2004 10:46 pm Post subject: Use stand-alone function in cout-statement |
|
|
Hi, I need a function that expects a const std::string& containing the
visual representation of a number in binary format. It should then output
this number in groups of four bits separated by a space. If the length of
the string is not divisable by four (normally, it should receive 8-bit
numbers (that is, strings of length , it should output the number as-is,
no grouping). I came up with:
void
output_binary_string(ostream& os, const string& binary_string)
{
/* If the length of the string (i.e., the number of bits in *
* the binary number), isn't divisable by four, simply output *
* the entire string and return. */
if(binary_string.length() % 4)
{
os << binary_string;
return;
}
string::size_type index = 0;
for(string::size_type i = 0; i < binary_string.length() / 4; ++i)
{
for(int j = 0; j < 4; j++)
{
os << binary_string[index++];
}
os << ' ';
}
}
Now, what if I want to use this function in a cout-statement? Do I have to
create a class or struct then and make my function the overloaded operator<<
for that class/struct?
Right now I do:
cout << "binary_representation: ";
output_binary_string(cout, s);
cout << endl;
I would like to be able to cout << "binary representation: " <<
output_binary_string(cout, s) << endl;
Thanks for any replies
|
|
| Back to top |
|
 |
Jonathan Mcdougall Guest
|
Posted: Mon Dec 20, 2004 11:34 pm Post subject: Re: Use stand-alone function in cout-statement |
|
|
Eric Lilja wrote:
| Quote: | Hi, I need a function that expects a const std::string& containing the
visual representation of a number in binary format. It should then output
this number in groups of four bits separated by a space. If the length of
the string is not divisable by four (normally, it should receive 8-bit
numbers (that is, strings of length , it should output the number as-is,
no grouping). I came up with:
void
output_binary_string(ostream& os, const string& binary_string)
{
/* If the length of the string (i.e., the number of bits in *
* the binary number), isn't divisable by four, simply output *
* the entire string and return. */
if(binary_string.length() % 4)
{
os << binary_string;
return;
}
string::size_type index = 0;
for(string::size_type i = 0; i < binary_string.length() / 4; ++i)
{
for(int j = 0; j < 4; j++)
{
os << binary_string[index++];
}
os << ' ';
}
}
Now, what if I want to use this function in a cout-statement? Do I have to
create a class or struct then and make my function the overloaded operator
for that class/struct?
Right now I do:
cout << "binary_representation: ";
output_binary_string(cout, s);
cout << endl;
I would like to be able to cout << "binary representation: "
output_binary_string(cout, s) << endl;
|
An easy way would be
# include
# include <string>
class output_binary_string
{
private:
std::string s_;
public:
output_binary_string(const std::string &s)
: s_(s)
{
}
void print(std::ostream &stream) const
{
stream << s_; // just format it as you wish
}
};
std::ostream &operator<<(std::ostream &stream,
const output_binary_string &obs)
{
obs.print(stream);
return stream;
}
int main()
{
std::string s = "101001010";
std::cout << "Binary: " << output_binary_string(s) << std::endl;
}
You could work out something with manipulators also. Let us know if you
need help.
Jonathan
|
|
| Back to top |
|
 |
Mike Wahler Guest
|
Posted: Mon Dec 20, 2004 11:40 pm Post subject: Re: Use stand-alone function in cout-statement |
|
|
"Eric Lilja" <ericliljaNoSpam (AT) yahoo (DOT) com> wrote
| Quote: | Hi, I need a function that expects a const std::string& containing the
visual representation of a number in binary format. It should then output
this number in groups of four bits separated by a space. If the length of
the string is not divisable by four (normally, it should receive 8-bit
numbers (that is, strings of length , it should output the number as-is,
no grouping). I came up with:
void
|
string
| Quote: | output_binary_string(ostream& os, const string& binary_string)
|
binary_string(const string& binary_string)
| Quote: | {
/* If the length of the string (i.e., the number of bits in *
* the binary number), isn't divisable by four, simply output *
* the entire string and return. */
|
ostringstream os;
| Quote: | if(binary_string.length() % 4)
{
os << binary_string;
return;
}
string::size_type index = 0;
for(string::size_type i = 0; i < binary_string.length() / 4; ++i)
{
for(int j = 0; j < 4; j++)
{
os << binary_string[index++];
}
os << ' ';
}
|
return os.str();
| Quote: | }
Now, what if I want to use this function in a cout-statement?
|
cout << binary_string("10010010") < 'n';
| Quote: | Do I have to
create a class or struct then and make my function the overloaded
operator
for that class/struct?
Right now I do:
cout << "binary_representation: ";
output_binary_string(cout, s);
cout << endl;
I would like to be able to cout << "binary representation: "
output_binary_string(cout, s) << endl;
Thanks for any replies
|
-Mike
|
|
| Back to top |
|
 |
Eric Lilja Guest
|
Posted: Mon Dec 20, 2004 11:44 pm Post subject: Re: Use stand-alone function in cout-statement |
|
|
"Mike Wahler" wrote:
| Quote: |
"Eric Lilja" <ericliljaNoSpam (AT) yahoo (DOT) com> wrote in message
news:cq7knk$r2n$1 (AT) news (DOT) island.liu.se...
Hi, I need a function that expects a const std::string& containing the
visual representation of a number in binary format. It should then output
this number in groups of four bits separated by a space. If the length of
the string is not divisable by four (normally, it should receive 8-bit
numbers (that is, strings of length , it should output the number
as-is,
no grouping). I came up with:
void
string
output_binary_string(ostream& os, const string& binary_string)
binary_string(const string& binary_string)
{
/* If the length of the string (i.e., the number of bits in *
* the binary number), isn't divisable by four, simply output *
* the entire string and return. */
ostringstream os;
if(binary_string.length() % 4)
{
os << binary_string;
return;
}
string::size_type index = 0;
for(string::size_type i = 0; i < binary_string.length() / 4; ++i)
{
for(int j = 0; j < 4; j++)
{
os << binary_string[index++];
}
os << ' ';
}
return os.str();
}
Now, what if I want to use this function in a cout-statement?
cout << binary_string("10010010") < 'n';
Do I have to
create a class or struct then and make my function the overloaded
operator
for that class/struct?
Right now I do:
cout << "binary_representation: ";
output_binary_string(cout, s);
cout << endl;
I would like to be able to cout << "binary representation: "
output_binary_string(cout, s) << endl;
Thanks for any replies
-Mike
|
Thanks both Mike and Jonathan for taking the time to help me out! I now
consider this problem solved and I've moved my attention to other issues in
this program. Thanks again.
/ Eric
|
|
| Back to top |
|
 |
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
|