C++ standard library 2 Flashcards
blank are usually just sequences of bytes stored in your computer’s storage
files are usually just sequences of bytes stores in your computer’s storage
two basic types of files:
text files
binary files
are intended to be interpreted as a string of characters
can be produced and read with programs such as Notepad
text files .txt files .cpp files .py files .html files
are sequences of bytes intended to be interpreted according to a program-defined scheme
Binary files
.docx files
.jpg files
.mp3 files
C++ allows us to work with text files using blank
streams
to use text file must include
Two data types in #include blank
include fstream
#include input and output with file stream
ifstream: input file stream
ofstream: output file stream
ifstream used for
ofstream used for
ifstream used for reading files
ofstream used for writing files
to use file stream
blank them
and call blank with the name of the file you want to open:
example
declare them and call .open( )
ifstream fin;
fin.open(“filename.txt”);
ofstream fout; ;
fout.open(“filename.txt”);
reading and writing files
to read data from an ifstream use blank and blank as you would with cin
use»_space; and getline()
example of ifstream reading data
fin»_space; x»_space; y;
to write data to an ofstream, use blank as you would with cout
«
example of writing data with ofstream
fout «_space;x «_space;” + “ «_space;y «_space;” is “ «_space;x + y;
when you are done, use blank to close the file
.close( )
to ensure the file is properly closed and put away use blank
fin. close( );
fout. close( );
C++ has a header called blank that contains various useful features for many programs
utility
two utilities
pair data type the swap ( ) function
swapping variables
example
**old way
temp = x;
x = y;
y = temp;
**old way
using a blank variable ensures that you don’t lose either value by overwriting it with the other
**old way
temporary value
**old way
C++11 gave new way to swap using blank and blank
include utility and swap ( );
the blank data type allow us to pair together two values of any types
pair data type
declare a pair by specifying the types of the two items and using make_pair( );
example
pair p = make_pair(5, 2.5)
to access the items in a pair, use blank and blank
.first and .second
to access the items in a pair, use .first and .second
cout «_space;p.first «_space;”, “ «_space;p.second «_space;“\n” ;
maps can be traversed with blank
range-for loops
the value of the loop control variable at each step is a pair of the blank and blank
the value of the loop control variable at each step is a pair of the key and mapped value types
example of loop control variable m
pairs and maps
map scores; // add some values to scores...
for(pair kv: scores)
{
cout «_space;kv.first
Many of the things C++ can do with containers such as vector or map involve using the containers’ blank
iterators
blank is a way of referring to an item in a container without needing an index
iterator
each blank has its own iterator type
each container has its own iterator type
Container types, like vector, have two key methods for using iterators
.begin( )
.end( )
returns an iterator referring to the first item in the container
.begin( )
returns an iterator indicating that the end has been reached
.end( )
the most basic way to use iterators is in a blank as a replacement for the index
for loop as a replacement for the index
vector numbers = { 1, 2, 3 }; for (vector : : iterator it = numbers.begin( ); { cout << *it << "\n"; }
vector : : iterator is the type for the iterator for a vector of ints
iterators types are attached to the container type
iterator types are attached to the container type with blank
: :
it++ moves the iterator to the blank
next item
*it access the item itself though the iterator; * is the dereference operator
True
Adding to or subtracting from an iterator yields the iterator for a different item in the sequence
example: it + 5 moves the iterator blank
it - 2 moves the iterator blank
it + 5 moves the operator forward five items
it -2 moves the iterator two items backward
this is why it++ moves the iterator blank
forward: it adds one to it
blank can also be used to assign values to the items they represent
iterators
example of iterators
code adds one to every value in the vector
for(vector::iterator it = numbers.begin( ); it!= numbers.end(); it++)
{
*it = *it + 1;
}
.cbegin( ) and .cend( ) means
constant iterators
not modifiable
blank are useful when working with maps, as they follow assignment
iterators
examples of map iterators
map scores;
scores[“Alice”] = 99.5
scores[“Blake”] = 115
scores[“Natalie”] = 103
for (map:: iterator it = scores.begin( ); it != scores.end( ); it++)
{
(it).second = (it).second + 1.5;
}
dereferencing the map iterator gives you a blank, hence the use of .second
pair object
blank omits the full iterator type by using this keyword
auto
example of using auto
for(auto it = numbers.begin(); it != numbers. end(); it++)
{
*it = *it + 1;
}
blank word backwards through the collection
reverse iterators
examples of reverse operators
.rbegin( ) and .rend( )
the blank header has functions for performing various operations with containers
algorithm
the blank function can replace one value with another
replace( ) example replace(numbers.begin( ), numbers.end( ), 5, 15);
the blank function returns true if the values are in ascending order, and false if not
is_sorted( ) function
example: if (is_sorted(numbers.begin( ), numbers.end( )))
{
cout «_space;“these are in ascending order”;
}
the blank function will put the items in ascending order
sort( ) function