CS Week 2 - Streams Flashcards

1
Q

ostream

A

output stream
a class that supports output
available via iostream and in namespace std

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

insertion operator

A

&laquo_space;
for converting different types of data into a sequence of characters, that sequence is normally placed into a buffer, and the system then outputs the buffer at various times

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

cout

A

a predefined ostream object that is pre-associated with a system’s standard output, usually a computer screen

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

how the ostream &laquo_space;works

A
  1. the insertion operator converts the string literal to characters, temporarily storing characters in an output buffer
  2. the system then writes the buffer’s content to the screen
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

istream

A

input stream
a class that supports input
available via iostream

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

extraction operator

A

> > to extract data from a buffer and write the data into different types of variables

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

cin

A

a predefined istream pre-associated with a system’s standard input, usually a keyboard

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

how istream&raquo_space; works

A
  1. the system automatically puts the standard input into a data buffer associated with cin
  2. > > operator skips leading whitespace and extacts as many characters as possible consistent with the target variable’s type
  3. the operator then stops at the next whitespace
  4. converts the extracted characters to the target variable’s type and stores the result into the variable
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

to read file input

A

include <fstream></fstream>

can create a new input stream that comes from a file

ifstream inFS;

inFS.open(str);

if(!inFS.is_open()){
cout &laquo_space;“error opening”; return 1;}

inFS&raquo_space; aVar;

inFS.close();

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

when would we use cin, cout, ifstream, ofstream, istringstream, ostringstream

A

cin - input from the keyboard

cout - output to the screen

ifstream - input from a file

ofstream - write to a file

istringstream - read input from a string

ostringstream - insert characters into a string buffer (instead of screen)

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

reading to the end of the file

A

while (inFS&raquo_space; aVar) {
//do something with aVar;
}

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

inFS.eof();

A

returns true if the previous stream operation reached the end of the file

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

inFS.fail();

A

returns true if the previous stream operation had an error
- instead of this use while (inFS&raquo_space; aVar){ /do something/ }

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

stream error

A

occurs when insertion or extraction fails causing the stream to enter an error state
- wrong type, too large, too small
- may skips extraction, set the var to 0, or set the var to the max or min of the type

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

inFS.clear();
inFS.good();
inFS.eof();
inFS.fail();
inFS.bad();

and when to use

A

1-bit error flags

clear - stream’s error state is cleared

good- returns true if no stream errors have occurred

eof - returns value of eofbit, if end of file reached on extraction

fail - returns true if either failbit or badbit is set, indicating an error for the previous stream operation

bad - returns true id badbit is set. indicating the stream is bad

  • use if you want to know what went wrong
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

how to write to a file

A

include <fstream></fstream>

declare a variable of type ofstream to write to a file

ofstream outFS;

outFS.open(fileName);

if(!outFS.is_open()) {
//output error
}

outFS &laquo_space;“Hiya!! &laquo_space;endl;

outFS.close();

14
Q

how to ask if the right value was extracted

A

if (!cin) {
cout &laquo_space;“bad input” &laquo_space;endl;
}

15
Q

how to use input string stream

A

include <sstream></sstream>

read input from an associated string

istringstream inSS (strToUse);

string firstName;
string lastName;

inSS&raquo_space; firstName;
inSS&raquo_space; lastName;

istringstream inSS;
string lineStr;
string data;
cout &laquo_space;“Enter names separated by spaces” &laquo_space;endl;
getline(cin, lineStr);
inSS.str(lineStr);
while (inSS&raquo_space; data) {
cout &laquo_space;data &laquo_space;endl;
}

16
Q

.get();
» or «

A

get - gets one character

> > or &laquo_space;- stops at whitespace

getline(stream, storingVar, stopWhere)
stopWhere - “,” or empty to stop at a new line

17
Q

getline with string streams

A

while (getline(cin, lineStr) {
inSS.str(lineStr);
inSS&raquo_space; name;
}

18
Q

inSS.str();
oSS.str();

A

.in - copies a string into the string streams input buffer

o- returns the contents of an ostringstream buffer as a string

19
Q

how to use output string stram

A

include <sstream></sstream>

ostringstream infoOSS;
cin&raquo_space; aVar;

infoOSS &laquo_space;aVar;

string infoStr = infoOSS.str();

20
Q

command line arguments

A

values entered by a user when running a program from a command line

21
Q

argc

A

number of command line arguments in argv
- includes the program name itself

int argc

22
Q

argv

A

array of strings, one string for each command line argument
argv[0] is the program name

char* argv[]

23
Q

when you run the program with argc and argv what happens

A
  • system passes an int parameter argc to main(), indicating the number of command line arguments
  • system passes a second parameter argv to main(), defined as an array of string, one string for each command line agument
24
Q

what happens when user does not type in the amount of command lines

A

extra is ignored
too few cause a problem

  • error- accessing elements in argv without first checking argc to ensure the user entered enough arguments, out of range array access
25
Q

usage message

A

lists a programs expected command line arguments, program should then return 1, indicating the system that an error occurred

if (argc != the size of argv/a #) {
cout &laquo_space;“Usage: programName input 1…input n” &laquo_space;endl;
return 1;
}

26
Q

command line arguments are what…

A

c strings

27
Q

what converts a string into an integer

A

in var = atoi(ageStr.c_str());

included in cstdlib

28
Q

putting quotes arounf an arument in a command line…and how should you access argv elements

A

allows the string to have any number of spaces

[], no .at

29
Q

what do command line arguments and files allow you to do

A

allow user to specify the location of an input or outpur file

argv[#] - # is where that file is located in the command line

inFS.open(argv[1]);

30
Q

selection sort defifniton

A

sorting algorithm that treats the input as 2 parts, a sorted part, and an unsorted part and repeatedly selects the proper next value to move from the unsorted to sorted part

31
Q

selection sort code

A

function to find location of smallest

int locationOfSmallest (const vector<int> v, int begin, int end) {
int smallest = v.at(begin);
int location = being;</int>

for (i = begin + 1; i < end; ++i) {
if (v.at(i) < smallest) {
location =i; }
}
return location;
}
—————–
code in main

for ( i = 0; i < v.size(); ++i) {
location = locationOfSmallest(v, i, v.size());
swap(v.at(i), v.at(location);
}

32
Q

runtime of selection sort algorithm

A

O(N^2)

a list with N elements
outer loop executes N-1 times
inner loop executes an average of N/2 times

if input increases in size by x times, the runtime increases x^2 times

33
Q

you can use inFS&raquo_space; data in the while conditional when

A

you know your data is clean

34
Q

both ways of writing argv

A

char** argv

char* argv[]