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
Q

maps can be traversed with blank

A

range-for loops

26
Q

the value of the loop control variable at each step is a pair of the blank and blank

A

the value of the loop control variable at each step is a pair of the key and mapped value types

27
Q

example of loop control variable m

pairs and maps

A
map scores; 
// add some values to scores...

for(pair kv: scores)
{
cout &laquo_space;kv.first <

28
Q

Many of the things C++ can do with containers such as vector or map involve using the containers’ blank

A

iterators

29
Q

blank is a way of referring to an item in a container without needing an index

A

iterator

30
Q

each blank has its own iterator type

A

each container has its own iterator type

31
Q

Container types, like vector, have two key methods for using iterators

A

.begin( )

.end( )

32
Q

returns an iterator referring to the first item in the container

A

.begin( )

33
Q

returns an iterator indicating that the end has been reached

A

.end( )

34
Q

the most basic way to use iterators is in a blank as a replacement for the index

A

for loop as a replacement for the index

35
Q
vector numbers = { 1, 2, 3 };
for (vector : : iterator it = numbers.begin( ); 
{
  cout << *it << "\n";
}
A

vector : : iterator is the type for the iterator for a vector of ints
iterators types are attached to the container type

36
Q

iterator types are attached to the container type with blank

A

: :

37
Q

it++ moves the iterator to the blank

A

next item

38
Q

*it access the item itself though the iterator; * is the dereference operator

A

True

39
Q

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

A

it + 5 moves the operator forward five items

it -2 moves the iterator two items backward

40
Q

this is why it++ moves the iterator blank

A

forward: it adds one to it

41
Q

blank can also be used to assign values to the items they represent

A

iterators

42
Q

example of iterators

code adds one to every value in the vector

A

for(vector::iterator it = numbers.begin( ); it!= numbers.end(); it++)
{
*it = *it + 1;
}

43
Q

.cbegin( ) and .cend( ) means

A

constant iterators

not modifiable

44
Q

blank are useful when working with maps, as they follow assignment

A

iterators

45
Q

examples of map iterators

A

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
Q

dereferencing the map iterator gives you a blank, hence the use of .second

A

pair object

47
Q

blank omits the full iterator type by using this keyword

A

auto

48
Q

example of using auto

A

for(auto it = numbers.begin(); it != numbers. end(); it++)
{
*it = *it + 1;
}

49
Q

blank word backwards through the collection

A

reverse iterators

50
Q

examples of reverse operators

A

.rbegin( ) and .rend( )

51
Q

the blank header has functions for performing various operations with containers

A

algorithm

52
Q

the blank function can replace one value with another

A
replace( )
example replace(numbers.begin( ), numbers.end( ), 5, 15);
53
Q

the blank function returns true if the values are in ascending order, and false if not

A

is_sorted( ) function
example: if (is_sorted(numbers.begin( ), numbers.end( )))
{
cout &laquo_space;“these are in ascending order”;
}

54
Q

the blank function will put the items in ascending order

A

sort( ) function