Midterm 1 - Specific Topics Flashcards

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
Q

what type of behavior do queues have?

A

FIFO

LILO

26
Q

where can you add/remove elements from a queue?

A

add elements to end

remove elements form front

27
Q

how do you declare a stanford queue?

A

include “queue.h”

Queue myQueue;

28
Q

what are some basic queue member functions?

A

.isEmpty()
.clear()
.size()
.peek()

29
Q

how do you add/remove from a queue?

A

.enqueue()

.dequeue()

30
Q

what type of behavior do stacks have?

A

LIFO

31
Q

where do you add/remove from a stack?

A

add to top of stack

remove from top of stack

32
Q

how do you delcare a stack?

A

include “stack.h”

Stack myStack;

33
Q

what does myStack.push(element) do?

A

pushes an element to the top of the stack

34
Q

what does myStack.pop() do?

A

removes element from top of the stack and returns it

35
Q

infix vs. postfix notation

A

infix - binary operators appear between the operands

postfix - binary operators appear after operands

36
Q

what is a map?

A

-container that has two types (key and a value)

37
Q

how do you declare a map?

A

include “map.h”

Map myMap;

38
Q

what does myMap.put() do?

A

-associates specified key with a value

myMap.put(key, value)

39
Q

what does myMap.containsKey.() do?

A

-returns true if the key is associated with a value in the map

40
Q

how do you use .containsKey()?

A

if (myMap.containsKey(key)) { // key is associated with something }

41
Q

what does myMap.get() do?

A

returns value associated with the key, provided that key is in the map
(can also use [ ] operator)

42
Q

which containers support [ ] operator?

A

-maps
-vectors
-arrays
-

43
Q

do friend functions belong to the class?

A

no

44
Q

when is a function recursive?

A

when it calls itself

45
Q

what is the base case?

A

-what prevents the recursion function from going on forever

46
Q

what is a stack overflow?

A

-too many functions active at once

47
Q

when does stack overflow often occur?

A

when your base case is wrong

48
Q

write a binary search function

A
int binarySearch (Vector&amp; vec, std::string&amp;, value) {
return binarySearchPart(vec, value, 0, vec.size()-1); 
}
49
Q

what is Big O notation?

A

describe worst-case performance of a particular algorithm

50
Q

what is linear search in big O notation?

A

O(n)

51
Q

what is binary search in big O notation?

A

O(log n)

52
Q

what is selection sort in big O notation?

A

O(n^2)

53
Q

what are 2 other O(n^2) sorts?

A
insertion sort (stable)
bubble sort (stable)
54
Q

what are 2 other O(n log n) sorts?

A
merge sort (not stable)
quicksort (stable)