Lecture 5 Zoom Q&A


Q: Hey! On homework, when it says add at least __ more test cases, does this mean that many more types of test cases (e. g. checking for squares, checking small numbers) or simply checking that many unique values through your function?

A1: While we strongly encourage you to consider multiple classes of types of test input, the minimum test requirements stated in the assignment handouts refer to one input/output check for one function. That being said, a truly comprehensive testing plan to validate the full correct functionality of your code will often have to involve much more than the minumum specified number of tests.


Q: is peek like a get function aka acessors for objects?

A1: Yes


Q: Do stacks have an initialized size or can they change in size as you push more and more values?

A1: Will grow as needed


Q: what does ADT stand for

A1: Abstract Data Type


Q: does stacks use RAM?

A1: Yes, all program data (variables, etc) is stored in memory (RAM) during execution


Q: Does pop return the value removed?

A1: Yes


Q: Why would you ever use a stack over a list, where you can access all the elements?

A1: live answered


Q: Is peek() essentially a way to access the top element?

A1: Yes, without removing it

A2: For context, pop will provide access and remove the top element


Q: Someone else asked it, but I missed the answer: Why use stacks over arrays?

A1: Simplicity. Although you could eat dinner using a full-featured tool like a Swiss army knife, it’s going to be simpler and less error prone to just use a simple fork


Q: is const in c++ equivalent to final in java?

A1: Pretty much, yes

A2: When used on variables yes.


Q: Is there a particular reason we used all caps for SPACE over camel case?

A1: live answered

A2: We use all caps for constants


Q: why do we need to reset the word to be empty in step 3?

A1: live answered


Q: Could constexpr also be used here?

A1: Yes, it is a compile-time constant.


Q: why do you have to write const before string? can u just declare it as a regular variable and then not change it

A1: live answered

A2: It’s protection against accidental changes later on + good style to indicate to othe rprogrammers reading your code that this value should never be changed.


Q: what did you #include?

A1: #include “stack.h”


Q: Can’t we just use .isspace command?

A1: yes, there are multiple ways to do it


Q: If possible, should we always use a for each loop rather than a for loop?

A1: up to you. It is a stylistic choice!

A2: A for-each is often more convenient, less error prone


Q: wouldn't this fail if the sentence has spaces like this?

A1: no

A2: It would end up pushing some empty strings, but when you printed them, they are emtpy, so basically it wouldn’t be visible


Q: This code doesn't account for puncuation correct?

A1: Yeah punctuation is just considered to be part of words.


Q: do we need to use word again? or could we just do cout « wordStack.pop() « SPACE;

A1: That way works as well


Q: if word is a C++ string, why couldn’t we just directly concatenate it with + with char?

A1: live answered


Q: how do we add a string and a char without cenverting types?

A1: live answered

A2: You can just use the + operator, it will take care of the type conversion for you


Q: why do we reset the word?

A1: We don’t want to concatenate each word onto the previous words, we want that word as separate string


Q: Is there a way to see stack size

A1: stack.size()


Q: The code will output an extra space at the end right?

A1: live answered

A2: Yep


Q: Can you ever like see what the stack actually looks like

A1: Yes, you can inspect the stack in the debugger!


Q: Could you possibly zoom out so we can see the whole program?

A1: The text size gets small for people if we do that


Q: Is there a way to use a for-loop with a bound increased by 1 to kind of combine the last if (word != “”) block into the loop?

A1: live answered


Q: cout « word + SPACE « endl? Is this ok?

A1: Yes, that also works


Q: can we do char+string or only string+char is allowed? what about char+char?

A1: no you can only add strings to strings

A2: adding a string to char (or reverse) is fine, but adding two chars will produce a char that is sum of the ascii values, not a string of length 2


Q: is stack.size() inefficient? Does it need to loop through every element to get the size

A1: No, the stack ADT keeps track of its own size and is able to return that efficiently


Q: I dont understand the purpose of the line where it says "if word doesnt equal empt char, push word onto wordStack"

A1: live answered


Q: Could you have just written cout « wordStack.pop() « SPACE; in the body of the while loop

A1: yes


Q: Could you use string::find to find the spaces and create substrings instead of using chars

A1: Sure, there are lots of ways to write this code


Q: could you just append a space onto the sentence to make sure that the last word is pushed?

A1: sure, that would work but we probably wan’t to avoid modifying the intiial sentence


Q: how did you know, logically, that you do should do the space condition first (as the if) and the word creation second (as the else statement)? / does it matter?

A1: live answered

A2: Same thing happens either way — only one of the if/else is entered based on the condition, there is no first/second ordering to the two options


Q: So “a” is a C string, whereas when we declare string s = “a”, then s is a C++ string?

A1: yes

A2: Yes, once a string literal gets stored in a string type varaible, it becomes a c++ string


Q: Would you do this in a similar way with std::cin?

A1: Simplest would be to use getLine() to read the line from input, then run the algorithm on the string


Q: what does the “else word+=c;” do again?

A1: Appends a character to the end of the string

A2: Continues building up the current word by appending on the current character form the original sentence that we’re considering


Q: if a spce is the first char in the string, can the program push an empty value onto the stack?

A1: live answered


Q: When should we use vectors vs stacks vs queues?

A1: vectors — when you want to access elements that aren’t first or last

queue — when you only want to access the element that was put in first

stack — when you only want to access the last inserted element


Q: I remember Netflix has a queue for your movies. Does that use a queue?

A1: Very likely!

A2: yep, first-in-first-out!


Q: Is there also a runtime error if peek() is dont on an empty queue?

A1: Yup


Q: Is front() the same as peek()?

A1: live answered

A2: front() no longer exists, we’re only going to use peek()


Q: what is the difference between peek() and front() again

A1: front() no longer exists, we’re only going to use peek()


Q: Are there any limitations to what types we can include in a queue or stack?

A1: Nope, any type can be used


Q: does back() still exist?

A1: No


Q: Does the Stanford Libraries have abstract data types for a double ended queue or a priority queue?

A1: Yes, Deque and PriorityQueue respectively


Q: oh right, about LaIR… how do we use it?

A1: Search for “lair” on course web site to get info!


Q: is there a reason why the Stanford library uses different names from the standard library for the same functions?

A1: live answered


Q: like where do we find it?


Q: Ohh okay!


Q: can people outside of Stanford use the Stanford library?

A1: Yes, its freely available though likely used primarily in our course.

A2: Yes, but it typically isn’t done


Q: will you tell us in assignments/assessments if we should use a queue as opposed to a vector or is it our choice

A1: A lot of times you will have to make reasoned choices about which data structures to use.


Q: say we stop at 106b and they ask us c++ stuff on a job interview, how should we explain that the functions we know about are different, and how do we learn which are the “standard” ones? Or can we use the stanford library at all times?

A1: They are really effectively the same, it is just a naming convention that differs

A2: Sometimes you can ask to use an external library (like stanford) Google is also a good resource if you want to find the official C++ documentation


Q: is the order that prof. chris did his example in the order youre supposed to do things in the STANDARD library?

A1: live answered


Q: C++ data structures/objects are automatically instantiated?

A1: live answered


Q: what happens if queue.size() changes during the loop. does it just use the initial value?

A1: It will use the current size. You usually don’t want to change a data structure that you’re looping through


Q: Does the for-each loop exist for stacks and queues?

A1: live answered

A2: It does not, since you can’t loop over ome of those structures without destroying them


Q: What does toString() do?

A1: live answered

A2: Converts the data structure to a string representation that can be printed.


Q: why does queue.dequeue(0) return 1? is it because 1 was in the 0 index?

A1: dequeue does not take any parameters. queue.dequeue() Returns the element at the fornt of the queue, which was 1 in this case

A2: Dequeue does not take an argument, it always removes and returns the element at front of queue


Q: does the for loop reevaluate the queue.size() evertime the program iterates?

A1: yes

A2: Yes, all parts of the stopping condition get evaluated at each iteration of the for loop


Q: so if we had a different function in place of queue.size, for loops could be inefficient because it computes the value every time?

A1: Yes if you had an “expensive” function as part of the for loop condition then that could get inefficient wquick


Q: so in a for loop, the i++ gets evaluated before the i < queue.size() check right?

A1: Yes


Q: how would you write it correctly so the numbers are all dequeued?

A1: live answered


Q: Difference between: cout « queue « endl; vs. cout « queue.toString() « endl;?

A1: they are the same

A2: No difference. The former is a little less cumbersome

A3: Same output, cout will automatically call .toString if you don’t do it yourself


Q: also there is no way to “index” in a loop, you only have access to the front?

A1: Correct


Q: does stack also have a .size() method?

A1: yes


Q: Does the standard library throw an error if you try to modify a data structure while doing a for each loop or just the Stanford libraries?

A1: Our libraries have many more safeguards than the standard libraries, yes


Q: Is that notation also called reverse Polish notation?

A1: Yep!


Q: what will break when we modify during a for each loop if we use the references (int and other related values will not change since it’s copy by value)? I know sometimes the contract will break, but what will break in these ADTs?

A1: Consider: If you were to insert/remove a value into the collection while iterating over it — does that new element get a turn in the iteration or no?


Q: How do you handle more than a digit

A0: How do you handle more than a digit

A1: numbers and operators are separated by spaces so you have arbitrarily long numbers


Q: Do we need to know postfix notation for assignments?

A1: No its just for this example


Q: is there any benefit to using const as opposed to #define?

A1: Yes, const is better C++ style. #define is left over from C


Q: could you also use a stack to calculate normal expressions (like 1+2*3 *(4+5) )

A1: live answered


Q: looking ahead, i think the code on slide 17 is buggy? there are two main()s etc.

A1: live answered


Q: Not actually related to the lesson at hand, but I was thinking about it earlier. Because the original value of i is stored in post-increment for loops, is the pre-increment technically more efficient, especially for user-defined types?

A1: live answered


Q: Coming from a Java perspective, sometimes, we are not even allowed to modify the elements in an ADT, much less the size, which messes up things in a sorted ADT. Is there any parallel in C++?

A1: live answered


Q: in a for each loop


Q: are the section times we received in PST?

A1: live answered

A2: PDT