Couple Questions!

Pages: 12
Does ios stand for Input/Output Supervisor?

Also in this code

cout << std::setiosflags(std::ios::left);

Does that mean that ios is a namespace inside of std?

And a flag is just a Boolean that can be set off or on, in this case it modifies the output stream if it is on or off?

In this link: http://msdn.microsoft.com/en-us/library/943z481t(v=vs.110).aspx

I dont understand the return type, can anyone please explain!
It stands for input/output stream.

std::ios is a typedef for std::basic_ios<char> which is a class that is publicly derived from std::ios_base, which is a class that has a public static member constant called "left"

You could have written std::cout << std::setiosflags(std::ios_base::left); for the same effect, or just std::cout << std::left; (there is a slight difference in that setiosflags resets all flags, while std::left only affects the adjustment flags)

As for the return type, it's just low-level machinery behind the I/O manipulators: those manipulators that take arguments (like setw or this setiosflags) are functions that return objects of unspecified type that have operator<< or operator>> overloads to apply the changes to the stream.
Last edited on
Setiosflags resets all the flags?

Also if it is a class why do you use :: to access it and not .?


Oh ok thanks for the return type thing.
Last edited on
You use :: to access static member of a class:
1
2
3
4
5
6
7
8
#include <iostream>
struct A { 
    static const int example = 1;
};
int main()
{
    std::cout << A::example << '\n';
}
You cant use .?

Or do you use :: so that it doesnt create a new class?
Yes, you can use the dot if you have an object of that class. For your case, std::cout works:

1
2
3
4
5
6
#include <iostream>
#include <iomanip>
int main()
{
    std::cout << std::setiosflags(std::cout.left);
}


it's more common to use :: to indicate that what you're accessing is a static
Last edited on
Oh I got it so you use :: to indicate you are accessing a static, and it can be used without creating a new member of the class.

Also what do you mean it resets all flags, and I am still a bit confused with the return type, what is str?
Last edited on
Setiosflags resets all the flags?

I didn't phrase that right, std::setiosflags() can set all formatting flags, e.g. hex, uppercase, and left at once. std::left/std::right/std::internal only affect adjustments.
am still a bit confused with the return type, what is str?

str is just the name given by MSDN documentation to the stream (std::cout in your case).

returns an object that, when extracted from or inserted into the stream str, calls str.setf(_Mask), and then returns str.

This says that, given
1
2
auto object = std::setprecision(std::ios::left);
std::ostream& str = std::cout;
the following two lines do the same thing
1
2
str << object;
str.setf(std::ios::left)

What do you mean only affects adjustments? Also std::left, what is left a function or what is it?

Also do I have flags right, a Boolean value?
Last edited on
std::left is an I/O manipulator that you can use instead of the much longer (and potentially ambiguous) std::setiosflags(std::ios::left). Like all I/O manipulators, it's a template function.

do I have flags right, a Boolean value

They are bits in a bitmask. E.g, in LLVM libc++
http://llvm.org/svn/llvm-project/libcxx/trunk/include/ios

1
2
3
4
5
6
7
class ios_base
{
public:
    typedef unsigned int fmtflags;
    static const fmtflags left        = 0x0020;
    static const fmtflags right       = 0x0080;
...
Last edited on
One more question, the setw manipulator only acts on the next object to be outputted right?
Simply put, yes. To be precise, it changes the field with value in the stream object, and most I/O operators change it back to zero. The full list of width-resetting operations is here: http://en.cppreference.com/w/cpp/io/manip/setw
What do you mean bitmask?
Hello google.
I tried google, I found a link to wikipedia: http://en.wikipedia.org/wiki/Mask_(computing)

So basically a bitmask is any data the bitwise operators can operate on?

And the Flag is a bool type (1 byte) so it can only be set to true or false.
Last edited on
Im still confused on the return type but i'll just ignore it for now.
Also by adjustments you mean it only affects what its meant to do?
Can you please help cire or anyone, is a bitmask any data the bitwise operators can operate on?

And a Flag is a bool type (1 byte) so it can only be set to true or false?
A bitmask is a pattern of bits. A bitmask can be used to set, check or clear a bit pattern depending on which bitwise manipulation is used. The smallest representation we can manipulate that can be used to indicate true or false (1 or 0) is a bit, so a bit may be referred to as a flag.

The following program checks to see which bits are set in an unsigned int. It's pretty basic and I would encourage you to check out the bitwise operators and play around with this before you ask further questions.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
#include <limits>
#include <iostream>

class Binary
{
public:
    Binary(unsigned value) : _value(value) {}
    operator unsigned() { return _value ; }

private:
    unsigned _value ;
};

std::ostream& operator<<(std::ostream& os, Binary b)
{
    unsigned bitmask = 1u << (std::numeric_limits<unsigned>::digits-1u) ;

    while ( bitmask )
    {
        os << ((b & bitmask) != 0) ;
        bitmask >>= 1 ;
    }
    return os ;
}

int main()
{
    unsigned n ;

    while ( (std::cout << "Enter a number: ")  &&  (std::cin >> n) )
        std::cout << Binary(n) << '\n' ;
}
Enter a number: 2
00000000000000000000000000000010
Enter a number: 4
00000000000000000000000000000100
Enter a number: 8
00000000000000000000000000001000
Enter a number: 6
00000000000000000000000000000110
Enter a number: 12
00000000000000000000000000001100
Enter a number: 14
00000000000000000000000000001110
Enter a number: 15
00000000000000000000000000001111
Enter a number: 16
00000000000000000000000000010000
Enter a number:
Pages: 12