C++ standard library 2 Flashcards

1
Q

blank are usually just sequences of bytes stored in your computer’s storage

A

files are usually just sequences of bytes stores in your computer’s storage

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

two basic types of files:

A

text files

binary files

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

are intended to be interpreted as a string of characters

can be produced and read with programs such as Notepad

A
text files 
.txt files
.cpp files
.py files
.html files
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

are sequences of bytes intended to be interpreted according to a program-defined scheme

A

Binary files
.docx files
.jpg files
.mp3 files

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

C++ allows us to work with text files using blank

A

streams

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

to use text file must include

Two data types in #include blank

A

include fstream

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q
#include
input and output with file stream
A

ifstream: input file stream
ofstream: output file stream

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

ifstream used for

ofstream used for

A

ifstream used for reading files

ofstream used for writing files

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

to use file stream
blank them
and call blank with the name of the file you want to open:
example

A

declare them and call .open( )
ifstream fin;
fin.open(“filename.txt”);

ofstream fout; ;
fout.open(“filename.txt”);

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

reading and writing files

to read data from an ifstream use blank and blank as you would with cin

A

use&raquo_space; and getline()

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

example of ifstream reading data

A

fin&raquo_space; x&raquo_space; y;

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

to write data to an ofstream, use blank as you would with cout

A

«

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

example of writing data with ofstream

A

fout &laquo_space;x &laquo_space;” + “ &laquo_space;y &laquo_space;” is “ &laquo_space;x + y;

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

when you are done, use blank to close the file

A

.close( )

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

to ensure the file is properly closed and put away use blank

A

fin. close( );

fout. close( );

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

C++ has a header called blank that contains various useful features for many programs

A

utility

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
17
Q

two utilities

A
pair data type
the swap ( ) function
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
18
Q

swapping variables
example
**old way

A

temp = x;
x = y;
y = temp;
**old way

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
19
Q

using a blank variable ensures that you don’t lose either value by overwriting it with the other
**old way

A

temporary value

**old way

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
20
Q

C++11 gave new way to swap using blank and blank

A

include utility and swap ( );

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
21
Q

the blank data type allow us to pair together two values of any types

A

pair data type

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
22
Q

declare a pair by specifying the types of the two items and using make_pair( );
example

A

pair p = make_pair(5, 2.5)

23
Q

to access the items in a pair, use blank and blank

A

.first and .second

24
Q

to access the items in a pair, use .first and .second

A

cout &laquo_space;p.first &laquo_space;”, “ &laquo_space;p.second &laquo_space;“\n” ;

25
maps can be traversed with blank
range-for loops
26
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
27
example of loop control variable m | pairs and maps
``` map scores; // add some values to scores... ``` for(pair kv: scores) { cout << kv.first <
28
Many of the things C++ can do with containers such as vector or map involve using the containers' blank
iterators
29
blank is a way of referring to an item in a container without needing an index
iterator
30
each blank has its own iterator type
each container has its own iterator type
31
Container types, like vector, have two key methods for using iterators
.begin( ) | .end( )
32
returns an iterator referring to the first item in the container
.begin( )
33
returns an iterator indicating that the end has been reached
.end( )
34
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
35
``` 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
36
iterator types are attached to the container type with blank
: :
37
it++ moves the iterator to the blank
next item
38
*it access the item itself though the iterator; * is the dereference operator
True
39
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
40
this is why it++ moves the iterator blank
forward: it adds one to it
41
blank can also be used to assign values to the items they represent
iterators
42
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; }
43
.cbegin( ) and .cend( ) means
constant iterators | not modifiable
44
blank are useful when working with maps, as they follow assignment
iterators
45
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; }
46
dereferencing the map iterator gives you a blank, hence the use of .second
pair object
47
blank omits the full iterator type by using this keyword
auto
48
example of using auto
for(auto it = numbers.begin(); it != numbers. end(); it++) { *it = *it + 1; }
49
blank word backwards through the collection
reverse iterators
50
examples of reverse operators
.rbegin( ) and .rend( )
51
the blank header has functions for performing various operations with containers
algorithm
52
the blank function can replace one value with another
``` replace( ) example replace(numbers.begin( ), numbers.end( ), 5, 15); ```
53
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 << "these are in ascending order"; }
54
the blank function will put the items in ascending order
sort( ) function