#include <iostream>
#include <iomanip> // for setw and setfill stream manipulators
#include <sstream> // for ostringstream class
#include <string>
using namespace std;
C++ I/O occurs in streams, which are sequences of bytes
A stream is an abstraction for input/output. You can think of it as a source (input) or destination (output) of characters of indefinite length.
iostream
¶basic_istream
for stream input operationsistream
is a basic_istream<char>
that enables char input, i.e. cin
's typebasic_ostream
for stream output operationsostream
is a basic_ostream<char>
that enables char output, i.e. the type of cout
, cerr
and clog
basic_ostream
member function put outputs one character at a time.std::cout.put('A');
A
std::cout.put(65);
A
getline
¶getline
reads a line of text.Enter
key after typing data, the system inserts a newline in the input streamgetline
reads from the standard input stream object cin
the characters the user enters, up to, but not including, the newline, which is discardedgetline
places the characters in its second argument{
std::string s;
std::cin >> s;
std::cout << "String 's' is \"" << s << "\"" << std::endl;
}
jkljfd fsdkjf dkdlkfldkf String 's' is "jkljfd"
{
std::string s;
std::getline(std::cin, s);
std::cout << "String 's' is \"" << s << "\"" << std::endl;
}
lfg jgkfg kfjgkfjgfk kfjgkfjg String 's' is " lfg jgkfg kfjgkfjgfk kfjgkfjg "
setw(n)
sets the minimum width of the input for the next stream operation. If the data doesn't meet the minimum field requirement, it is padded with the default fill character until it is proper size.std::cout << "Output:" << 10 << std::endl;
Output:10
#include <iostream> // for setw()
std::cout << "Output: " << std::setw(5) << 10 << std::endl;
Output: 10
setprecision(n)
sets the precision of the stream out or in to exactly n.#include <iostream>
#include <cmath> // for pow()
#include <iomanip> // for setprecision()
std::cout << "1 petabyte is 2^50 bytes = "
<< std::setprecision(20)
<< pow(2., 50) << " bytes"
<< std::endl;
1 petabyte is 2^50 bytes = 1125899906842624 bytes
boolalpha
determines whether or not the stream should output boolean values as 1
and 0
or as true and false. The opposite manipulator is noboolalpha
, which reverses this behavior.std::cout << true << std::endl;
1
std::cout << std::boolalpha<< true << std::endl;
true
oct
, dec
, hex
set the radix on the stream to either octal (base 8), decimal (base 10), or hexadecimal (base 16). This can be used either to format output or change the base for input.
More example of stream library use in here: https://blog.andersonbanihirwe.dev/2018/01/20/play-interactively-with-cpp-streams.html
std::cout << 1000 << std::endl;
1000
std::cout << std::oct << 1000 << std::endl;
1750
std::cout << std::hex << 1000 << std::endl;
3e8
std::cout << std::dec << 1000 << std::endl;
1000
setfill
, it applies for all subsequent values that are displayed in fields wider than the value being displayedsetfill
is a sticky settingsetw
, which applies only to the next value displayedsetw
is a nonsticky settingstd::cout << std::setfill('0') << std::setw(4) << 10 << std::endl;
0010
std::cout << 10 << std::endl;
10
std::cout << std::setw(8) << 10 << std::endl;
00000010
Parameterized stream manipulator setfill specifies the fill character that's displayed when an integer is output in a field wider than the number of digits in the value.
Once the fill character is specified with setfill, it applies for all subsequent values that are displayed in fields wider than the value being displayed:
Each sticky setting should be restored to its previous setting when it’s no longer needed.
<<
and >>
operators are overloaded to accept data items of specific types.<<
and >>
have not been overloaded for a user-defined type and you attempt to use those operators to input into or output the contents of an object of that user-defined type, the compiler reports anerror.
istream
s read
and ostream
s write
member functions, respectively.char
s in memorychar
s.{
char buffer[]{"HAPPY BIRTHDAY"};
std::cout.write(buffer, 10);
}
HAPPY BIRT
{
char buffer[]{"HAPPY BIRTHDAY"};
std::cout << "Enter a sentence:\n";
std::cin.read(buffer, 6);
std::cout << "\nThe sentence entered was:\n";
std::cout << buffer << std::endl;
}
Enter a sentence: hello-hello The sentence entered was: hello-BIRTHDAY
ostringstream
objects provide the same functionality as cout
, but write their output to string
ostringstream
's str
member function gets the formatted string
<sstream>
{
int hour{10}, minute{3}, second{44};
std::ostringstream output;
output << std::setfill('0') << std::setw(2) << hour << ":"
<< std::setw(2) << minute << ":" << std::setw(2) << second;
std::cout << output.str() << std::endl; // returns the formatted string
std::stringstream input{"10 d"};
char c;
int n;
input >> n >> c;
std::cout << "n: " << n << ", c: " << c << std::endl;
}
10:03:44 n: 10, c: d
to_string
(from header <string>
), which converts a numeric value to a string
object.C++ offers two types of strings
string
class objects, andC
-style, pointer-based strings (C strings).C++ string
class is preferred it eliminates many of the security problems that can be caused by manipulating C strings.
{
char c = 'A';
int i{c};
std::cout << "Char: '" << c << "' is an integer: "<< i << std::endl;
}
Char: 'A' is an integer: 65
{
std::string s{"abcd3340+-\\$"};
std::cout << "Some string: \"" << s << "\"" << std::endl;
}
Some string: "abcd3340+-\$"
'\0'
.sizeof
a string literal is the length of the string including the terminating null character.chars
or a variable of type const char*
.{
char color[]{"blue"};
const char* colorPtr{"green"};
std::cout << "Some string: \"" << color << "\"" << std::endl;
std::cout << "Another string: \"" << colorPtr << "\"" << std::endl;
}
Some string: "blue" Another string: "green"
{
char color[]{"blue"};
std::cout << "Some string: \"" << color << "\"" << std::endl;
std::cout << "A third character in the string is '" << color[2] << "'" << std::endl;
color[0] = 'g';
std::cout << "Same string: \"" << color << "\"" << std::endl;
}
Some string: "blue" A third character in the string is 'u' Same string: "glue"
"Jane Green"
.string
<string>
.string
, belongs to namespace std
.Class string has overloaded operators and several other useful member functions, including empty, substr and at
Class string has overloaded equality and relational operators perform lexicographical comparisons
std::string s1{"happy"};
std::string s2{" birthday"};
std::string s3; // creates an empty string
// test overloaded equality and relational operators
std::cout << boolalpha
<< "s1 is \"" << s1 << "\"; s2 is \"" << s2
<< "\"; s3 is \"" << s3 << '\"' << " and is empty: " << s3.empty()
<< "\n\nThe results of comparing s2 and s1:"
<< "\ns2 == s1 yields " << ( s2 == s1 )
<< "\ns2 != s1 yields " << ( s2 != s1 )
<< "\ns2 > s1 yields " << ( s2 > s1 )
<< "\ns2 < s1 yields " << ( s2 < s1 )
<< "\ns2 >= s1 yields " << ( s2 >= s1 )
<< "\ns2 <= s1 yields " << ( s2 <= s1 );
s1 is "happy"; s2 is " birthday"; s3 is "" and is empty: true The results of comparing s2 and s1: s2 == s1 yields false s2 != s1 yields true s2 > s1 yields false s2 < s1 yields true s2 >= s1 yields false s2 <= s1 yields true
Class string provides member function empty to determine whether a string is empty
Class string has overloaded += operator performs string concatenation.
+=
// test overloaded string concatenation assignment operator
std::cout << "s1 += s2 yields s1 = ";
s1 += s2; // test overloaded concatenation
std::cout << s1;
// test string concatenation with a C string
std::cout << "\n\ns1 += \" to you\" yields\n";
s1 += " to you";
std::cout << "s1 = " << s1;
s1 += s2 yields s1 = happy birthday s1 += " to you" yields s1 = happy birthday to you
const
reference), depending on the context in which the call appears.// test copy constructor
std::string s4{s1};
std::cout << "s4 = " << s4 << "\n\n";
// test overloaded copy assignment (=) operator with self-assignment
std::cout << "assigning s4 to s4\n";
s4 = s4;
std::cout << "s4 = " << s4;
// test using overloaded subscript operator to create lvalue
s1[0] = 'H';
s1[6] = 'B';
std::cout << "\n\ns1 after s1[0] = 'H' and s1[6] = 'B' is:\n" << s1 << "\n\n";
s4 = happy birthday to you assigning s4 to s4 s4 = happy birthday to you s1 after s1[0] = 'H' and s1[6] = 'B' is: Happy Birthday to you