Midterm 1 - Specific Topics Flashcards

(54 cards)

1
Q

what are some collections we have learned?

A
  • stacks
  • sets
  • vectors (stanford)
  • arrays
  • string (collection fo chars
  • maps, sets
  • queues
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

what is a string a collection of?

A

chars

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

how do you declare a string?

A

std::string msg = “hello”;

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

what is casting?

A

allows you to forcibly convert from one type to another

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

casting example

A

std::string name = std::string(“Nate”) + “Dog”;

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

what are some member objects of a string?

A
.clear()
.empty()
.front()
.back()
.find()
.substr()
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

what is .substr()

A

takes 2 parameters
-index to start at
-number of char to include
returns new string that contains requested range char

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

substr example

A

std::string title = “A Tale of Two Cities”;

//yields "of Two"
std::string ofTwo = title.substr(7,6);
//yields "Two Cities"
std::string twoCities = title.substr(10)
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

how do you check to see if an item is in a string?

A

.find()

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

what is .find()

A

takes two parameters

  • the string you are searching for
  • index you want to start search at
  • returns index of first appearance of the requested string
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

example of .find()

A

std::string title = “A Tale of Two Cities”;

// search for first instance of "of"
// starting at index 0
int index = title.find("of", 0);
std::cout << index << std::endl;
// search for first instance of "hello"
// will return value of npos because "hello" is not found
std::cout << title.find("hello") << std::endl
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

what is passing by reference?

A

change will persist

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

what is passing by value?

A

make a copy

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

what do we pass by value?

A

basic types (int, double, float, char)

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

what do we pass by reference?

A

non-basic types (std::string, std::ifstream, std::ofstream etc.)

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

how do you declare a stanford vector?

A

Vector myVector;

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

how do you declare a stanford vector?

A

include “vector.h”

Vector myVector;

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

vector example: declare a vector of strings

A

Vector> names;

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

what are some member functions of vectors?

A
.size()
.add()
.remove(index)
.isEmpty() (returns true if empty)
.clear()
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
20
Q

what is myVector.insert(index, element)

A

-inserts element before the specified index

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

what is std::getline

A

allows you to get an entire line of input (so it doesn’t stop until an enter)

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

how does std::getline work?

A

takes two parameters

  • input stream you want to grab line from
  • string that you want to store the line in (passed by reference)
23
Q

std::getline example with cities

A

std: :cout &laquo_space;“Enter a city: “;
std: :string city;
std: :getline(std::cin, city)
std: :cout &laquo_space;“You entered “

24
Q

how do you fix cin and getline problem?

A

std::cin.ignore();

before getline

25
what type of behavior do queues have?
FIFO | LILO
26
where can you add/remove elements from a queue?
add elements to end | remove elements form front
27
how do you declare a stanford queue?
#include "queue.h" Queue myQueue;
28
what are some basic queue member functions?
.isEmpty() .clear() .size() .peek()
29
how do you add/remove from a queue?
.enqueue() | .dequeue()
30
what type of behavior do stacks have?
LIFO
31
where do you add/remove from a stack?
add to top of stack | remove from top of stack
32
how do you delcare a stack?
#include "stack.h" Stack myStack;
33
what does myStack.push(element) do?
pushes an element to the top of the stack
34
what does myStack.pop() do?
removes element from top of the stack and returns it
35
infix vs. postfix notation
infix - binary operators appear between the operands postfix - binary operators appear after operands
36
what is a map?
-container that has two types (key and a value)
37
how do you declare a map?
#include "map.h" Map myMap;
38
what does myMap.put() do?
-associates specified key with a value myMap.put(key, value)
39
what does myMap.containsKey.() do?
-returns true if the key is associated with a value in the map
40
how do you use .containsKey()?
if (myMap.containsKey(key)) { // key is associated with something }
41
what does myMap.get() do?
returns value associated with the key, provided that key is in the map (can also use [ ] operator)
42
which containers support [ ] operator?
-maps -vectors -arrays -
43
do friend functions belong to the class?
no
44
when is a function recursive?
when it calls itself
45
what is the base case?
-what prevents the recursion function from going on forever
46
what is a stack overflow?
-too many functions active at once
47
when does stack overflow often occur?
when your base case is wrong
48
write a binary search function
``` int binarySearch (Vector& vec, std::string&, value) { return binarySearchPart(vec, value, 0, vec.size()-1); } ```
49
what is Big O notation?
describe worst-case performance of a particular algorithm
50
what is linear search in big O notation?
O(n)
51
what is binary search in big O notation?
O(log n)
52
what is selection sort in big O notation?
O(n^2)
53
what are 2 other O(n^2) sorts?
``` insertion sort (stable) bubble sort (stable) ```
54
what are 2 other O(n log n) sorts?
``` merge sort (not stable) quicksort (stable) ```