Você está na página 1de 3

/*

* File: 20160909_CPP_L02.cpp.
* Title: 2016-09-09 C++ Lesson 02
* Author: Renato Montes
* Description: Notes for Albert Wei's C++ class
*/

/* Output */

int m=1, n=2;


cout << m << n << endl; //12
//within the ostream class there is a method:
ostream& operator<<(int)
//so what this is doing is calling this method
//can also do this:
cout.operator<<(m)
//the compiler invokes the method

cout << m
|--------|
//this (|-----|) returns cout, so you can keep on inserting with <<

ostream& operator<<(int)
//note that it returns a reference
//ostream objects cannot be copied anyway: cannot make a copy of
// standard output

cout << "hello";


//translated to:
operator<<(cout, "hello")

/* Formatted Output */

//output format is controlled by format flags within the ostream


// object
//the format flags have type:
ios_base::fmtflags
//this is the type of the variable we need to create
//ios_base is the name of another class

ios_base::fmtflags f = cout.flags(); //retrieve current flags


//then we can change the state, i.e. the format capability
......
//when resetting to the previous configuration:
cout.flags(f);//set flags

//some possible values of ios_base::fmtflags:


ios_base::showpos
// hex
// oct
//we don't directly manipulate those flags
//obviously there are methods that allow us to use the flags
//2 ways to change output format:
// 1) using methods (setf/unsetf), i.e. setf = set flag
// (think of it as changing bits within an integer)
// 2) using manipulators

/* * Using methods */
int n = 1;
cout << n << endl;//1
cout.setf(ios_base::showpos);
//now we print n:
cout << n << endl;//+1
cout << m << endl;//+2
cout.unsetf(ios_base::showpos);
cout << n << endl;//1
//this works for things that are ON or OFF
//there are things that aren't on or off
//some enim cases are more complicated

int n = 123;
cout << n << endl;//123
cout.setf(ios_base::hex, ios_base::basefield);
cout << n << endl;//76 or something
cout.setf(ios_base::dec, ios_base::basefield);
//you never call unsetf
//ios_base::basefield is sometimes called the "group name"
//hex and dec is part of a group with octo
//the group name unsets all three, and then sets hex on
//if two are on there's undefined behaviour!

/* * Using Manipulators */
int n=1, m=2;
cout << n << endl //1
<< showpos //manipulator
//good thing about manipulators: you use the same << operator
<< n << endl //+1
<< m << endl //+2
<< noshowpos
<< n << endl; //1

int n=123;
cout << n << endl //123
//now we simply insert the hex manipulator:
<< hex << n << endl //76
<< dec << n << endl; //123

//some manipulators:
//integer types: dec/hex/oct
//also for integers: showbase/noshowbase
//showbase is 0x76, where 0x is hex
//noshowbase would just be 76

//floating-point types: fixed/scientific/defaultformat


//fixed point format may be 1234.56
//while scientific may be 1.23456e+003

//numbers: showpos/noshowpos
//namely showpos is +123, noshowpos is 123

//for numbers basically: uppercase/nouppercase


//7B vs. 7b, or 0X7B vs. 0x7b, or 1.23E+002 vs. 1.23e+002

//for boolean: boolalpha/noboolalpha


//boolalpha is true/false, noboolalpha is 1 or 0

//now... let's say we want to print -12 in a width of 5:


//adjustment: left/right/internal
//left: -12 | //two spaces trailing
//right: | -12 //two spaces at the beginnning
//internal: - 12 //two spaces in the middle
//haven't been told how to set the width, but imagine a width of 5

//manipulators that take arguments


//to use them, include a special header file:
#include <iomanip>

setprecision //number of decimal places for floating point number


//for fixed/scientific mode
setfill //set the fill character
//the fill character is the target to fill things with
//probably a space
setw //set minimum field width

long double f = 123.456; //%10.2Lf


cout << setprecision(2) << setfill(' ') << setw(10) << f << endl;
//output: | 123.4b //four spaces //!?!?

//Note that setw only sets the width of the next formatted output
//applies to the next thing that is printed
//then the minimum field width reverts back to 0

Você também pode gostar