What is the difference between ios::base:precision & setprecision
Given in the following links?
http://www.cplusplus.com/reference/ios/ios_base/precision/
http://www.cplusplus.com/reference/iomanip/setprecision/
The difference it to do with the usage.
The first (setter) is a member function, so gets called like this:
std::cout.precision(10);
The second isn't a member function so is called different.
std::cout << std::setprecision(10);
The 2nd link explicitly says
"Behaves as if member precision were called with n as argument on the stream on which it is inserted/extracted as a manipulator "
Citing your second link:
Behaves as if member precision were called with n as argument on the
stream on which it is inserted/extracted as a manipulator (it can be
inserted/extracted on input streams or output streams).
Thus, there's no difference.
Related
I understood that we can use both options below to change the boolalpha flag, but why do we need to qualify ios_base in the setf() function and can't use std::boolalpha within the function parameter?
std::cout << std::boolalpha; //using manipulator
std::cout.setf(ios_base::boolalpha); // we can't use std::cout.setf(std::boolalpha);
std::boolalpha is a stream function object (also known as a manipulator) that sets the boolalpha flag on a stream. This flag causes boolean values to be written out as "true" or "false" rather than as "1" or "0".
The setf() function is a member function of the std::ios_base class, which is the base class for all stream classes. The setf() function is used to set various formatting flags on a stream, including the boolalpha flag. The first argument to setf() is an enumeration value that specifies which flag to set.
In this case, ios_base::boolalpha is the enumeration value that represents the boolalpha flag, so std::cout.setf(ios_base::boolalpha) sets the boolalpha flag on the std::cout stream.
The reason you can't use std::boolalpha within the function parameter, is that std::boolalpha is a manipulator, and setf() expects the enumeration type as its argument, not a manipulator.
Additionally, a manipulator can be used to simply change the flag, unlike setf() that is meant to be used as a more powerful and general set of operations.
I want to know actual difference between cout<<cout and cout<<&cout in c++? In my compiler cout<<cout returns 0x477864 & cout<<&cout returns 0x477860 at any time.It shows it has 1 digit of difference between them.What are the significance of these?
When you do this:
cout << cout;
You are relying on the stream's implicit conversion to void*. This value is used (pre-c++11) for testing the state of the stream. It is unspecified what the value actually is, it just needs to be NULL if the stream is in a fail state, and a non NULL otherwise. Maybe it's returning the address of a member of the object, but it's not really important, and is implementation defined.
When you do this:
cout << &cout;
That is getting the actual address of cout.
Note that in C++11 and beyond, the first one, cout << cout;, will no longer compile, because the implicit conversion to void* no longer exists. Instead there is an explicit conversion to bool to serve the same purpose.
Why program results are diffentent in such 2 cases?
case 1)
cout<<"using cout.put(c): "<<cout.put(c);
cout.put('!');
case 2)
cout<<"using cout.put(c): ";
cout.put(c);
cout.put('!');
in case 1) is:
using cout.put(c): 0x477864!
in case 2) is:
using cout.put(c): U!
To elaborate on what others have said: in case 1, the compiler
will try to find a << operator for the return value of
cout.put(c), and use it. cout.put(c) returns
a std::ostream&, for which there is no <<, but an
std::ostream will convert implicitly to a bool (C++11) or
a void* (earlier C++). So you call << for a void*
(judging from your output).
Also note that when cout.put(c) is called within the
expression is unspecified (except that it must be before the
<< for the void*). So you could easily end up with either:
Uusing cout.put(c): 0x477864!
or
using cout.put(c): U0x477864!
(Because the << are in fact function calls, which introduce
sequencing, I don't think any other combinations are possible.)
In general, anytime you have a sequence of <<, expect the
value of the right side to be output, regardless of any side
effects the expression might have. You call cout.put(c) for
its side effects, not its value, so you shouldn't use it in
a <<. (To output a single character in the sequence, just use
<< c.)
cout.put(c) returns a reference to the cout object. In the first case you print that object.
std::basic_ostream::put reference
This has been puzzling me for a while: with printf, you provide formmatters to dictate how a parameter should be interpreted, but cout doesn't require this. How does cout know to read a variable according to the correct type?
It's called function overloading. In C++, you can have as many functions as you want with the same name (operator<< is the name in this case), as long as they take different parameter sets. cout doesn't dictate how the parameters are interpreted, the compiler does. Or rather, the compiler dictates which function is called, and that function dictates how the parameter is formatted. The compiler knows the type of each parameter, and calls the appropriate function accordingly.
For example, this calls ostream::operator<<(int)
cout << 10;
And this calls ostream::operator<<(double), which is a completely different function
cout << 3.14;
I am learning operator overloading. "out" is being used instead of "cout" when overloading "<<" operator. I don't understand why.
ostream &operator<<( ostream &out, const IntList &L ) {
out << "[ ";
for (int k=0; k< L.numItems; k++) {
out << L.Items[k] << ' ';
}
out << ']';
}
I want to ask differences between cout and out and what happens if I use cout instead of out.
Thanks for answers.
What you are looking at is a overloaded "stream insertion" operator, allowing some custom class to be written to an ostream object using the typical cout << myObject syntax.
The variable in this case is called out because that's the name they've given to the ostream object being passed into the function, which may be any output stream, whether it's cout or an fstream or a stringstream. It's just a variable name, and they could have called it blah and written:
ostream &operator<<( ostream &blah, const IntList &L ) {
blah << "[ ";
// ...
}
Typically you choose a variable name which is descriptive, and out as a name for an output stream is pretty descriptive.
cout would be an especially bad variable name, as it is strongly associated with std::cout, used for writing specifically to the standard output stream. This code doesn't write specificially to standard output, it writes to any ostream object via the << operator so they've chosen a more generic name for their ostream argument.
I want to ask differences between cout and out and what happens if I use cout instead of out. Thanks for answers.
In this case, an ostream& (out) is a parameter passed to the function. This allows the operator<< to work on any ostream.
cout is a specific ostream instance - the standard output stream. If they used cout here, you wouldn't be able to use the << operator on cerr (the standard error stream) or any other ostream. If you replaced the out with cout in the body, any time you used this on a different ostream, it'd be written to cout. (Of course, if you changed the parameter to be named cout, that wouldn't happen - but it would be very misleading to anybody looking at this code, as people would expect that the code writes to the standard output stream, not to the stream being passed in.)
In general, you only would want to use cout as a name if you are specifically referring to std::cout - the standard output stream, as using it in other contexts would be very confusing.
out is the name of the ostream object passed to the overloaded operator (inside the implementation of the operator).
The overloaded operator allows you to write code like this
IntList i;
cout<<i;
or
cerr<<i;
In the implementation if you substituted out with cout, then the second call
cerr<<i;
would print to standard output whereas it should have printed to standard error.
The critical thing here is really the types in the function signature: as long as it's a freestanding function with two parameters - one of type std::ostream& and the other able to be matched by the value to be streamed, then the function body will be invoked. It should return a reference to the stream to allow chaining (as in if (cout << firstIntList << secondIntList)).
The actual parameter names are whatever you feel like, as long as they're not reserved words. I tend to use "os", as in output-stream.