Part 5 - String class, Files and Streams, C++ vs Java Flashcards

1
Q

WHat will

string s4(5, ‘x’);

do?

A

Make a string with 5 ‘x’ s i.e.

“xxxxx”

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

What does the .at method do in strings?

What is the implication of this?

A

Returns a reference to the character at that position

Can do

s.at(2) = ‘e’;

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

What does the append function do to strings?

A

s1.append(s2) will append s2 to the end of s1

same as +=

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

What will s2.append(3, ‘!’);

do?

A

append 3 !s to the end of s2

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

Demonstrate substrings, replacing strings and erasing parts of strings

A

s1. substr(m, n) will get a copy of the string n characters long starting at m
s1. replace(m, n, x) will replace substring or length n starting at m with a copy x
s1. erase(m, n) will erase n characters starting with m

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

How do the < <= > and >= operators compare strings?

A

Lexicographically using ASCII ordering

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

what does the compare function do with strings?

A

s1.compare(s2) returns 0 if they are equal, negative number if s1 is before s2 and positive number of s2 is before s1

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

What will the find and rfind functions do with strings?

A

s. find(‘l’) finds the index of the first instance of l in the string s
s. rfind finds the index of the last instance of l in the string s

if l is not found string::npos is returned

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

What is the second argument that can optionally be used with the find or rfind functions on the string class?

A

start index

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

Write a function to count the number of instances of a given character in a string

A

int count(string s, char c) {

int tot = 0;

int pos = s.find(c);

while (pos != string::npos) {

tot++;

pos = s.find(c);

}

return tot;

}

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

What do the find_first_of and find_first_not_of functions do?

A

s. find_first_of(“ \t\n”) finds the first white space character
s. find_first_not_of(“ \t\n”) finds the first non whitespace character

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

What is the getline function used for? How is it used?

A

To get a line of text that may contain whitespace

Takes reference to a stream and reference to string where result will be stored, e.g.

getline(cin, s2)

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

What are the benefits of a string stream?

A

Input and output to be performed to and from string objects

Formatting like setw and setfill can be used

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

What class and header can be used for an output string stream?

Give an example of a method to output something using one

A

<sstream></sstream>

ostringstream

void fun(string s) {

ostringstream os;

os << setfill(‘0’) << setw(10) << s;

return os.str();

}

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

Give an example using istringstream to convert a string to an int

A

int stoi2(string s) {

istringstream str(s);

int i;

str >> i;

return i;

}

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

How can files be

a) input
b) output

using file streams?

A

a) ifstream
b) ofstream

17
Q

What header is needed for filestreams?

A

<fstream></fstream>

18
Q

What are the 4 different access modes for input/output fstreams?

A

ios: :in (input)
ios: :out (output)
ios: :app (append)
ios: :binary (binary files)

19
Q

Write the body of a method that will append “hi” to a file “output.txt” using an output file stream

A

ofstream os;

os.open(“output.txt”, ios::app);

if (os) { os << “hi” << endl }

os.close();

20
Q

What does the get function do with input streams?

What if the end of the file has been reached?

A

Gets the next character in the file (possibly whitespace)

If end of file reached, EOF will be returned

21
Q

what does the function eof() do to an input stream?

A

Returns true if the end of the file has been reached

22
Q

How does the getline function work with input streams?

A

is.getline(addrofbuffertostorelinein, maxlinelength);

23
Q

Describe what the following code will do:

ifstream str(“data.txt”);

const int SIZE = 80;

char buffer[SIZE];

while (!str.eof()) {

str.getline(buffer, SIZE);

// process buffer

}

A

Read data.txt line by line

if any line contains more than 79 characters, it will be read as multiple 79 character lines

24
Q

How can you switch between fixed and scientific outputs in output streams?

A

cout << fixed << pi << endl;

cout << scientific << pi << endl;

25
Q

How can you change the precision of floats in output streams?

A

cout << precision(5) << pi << endl;

26
Q

What two alignment types are permitted in fixed witdh fields of output streams?

A

cout << left << setw(5) << n << endl;

cout << right << setw(5) << n << endl;