Lecture 4/13: Lecture 4 Q&A


Lecture 4 Questions and Answers (Exported from Zoom Q&A log)

Q: Will anyone be sticking around after the lecture to answer questions in person?

A1: yes chris will be in this call and there’s also the post-lecture zoom meeting that I’ll be manning


Q: if the OH are not possible due to timezones, can we still get a ā€œdebugging sessionā€ ?

A1: Can you attend any of my/Chris/Nick office hours?

A2: we’ve also added a 9-11am lair time


Q: where do we find the post lecture meeting

A1: zoom link is in the canvas event for lecture, same place where the webinar link is


Q: Can you also use ā€œunsigned intā€ before i to fix the warning issue?

A1: Yes!


Q: why do we have ā€œint i=0;i<(int)plainText.length();i++ā€ (why is int in bracket?)

A1: It is a ā€œtypecastā€ converting the expression to the named type. In this case, it is taking the size_t return from s.length() and converting to plain int


Q: Why is there an (int) in the code being explained

A1: It is a ā€œtypecastā€ converting the expression to the named type. In this case, it is taking the size_t return from s.length() and converting to plain int


Q: In the for loop

A1: live answered


Q: why does the ceaser cipher for loop has int i=0 instead of size_t i=0?

A1: You could do either, earlier Chris showed code where it was written using size_t, this loop is using int


Q: how is the number converted to a char?

A1: The ascii table assigns each character a number


Q: Can you explain what’s going on in that else statement and after?

A1: live answered


Q: can we use functions from Stanford libraries in our assignments?

A1: Yes


Q: so for strings we would opt for toUpperCase?

A1: Yes


Q: so i can only print out char (as an actual letter), and char is a special data type that i can add numbers to and that adds to the equivalent ascii value?

A1: Yes, exactly


Q: could the else statement been moved down one line and put aligned with the if statement??

A1: This particular else goes with the outer if, not the inner.


Q: would the isdigit function work on types like double,long, float?

A1: isdigit works on a single character not a string of characters


Q: if operators compare ascii values how would you compare lengths of strings

A1: if (s.length() < t.length()) …


Q: does it compare every letter or just the first one?

A1: it compares as many characters as needed to establish ordering, this will generally be up through the first character that differs (or if it runs off the end of one string)


Q: is it better to modify strings or create new strings?

A1: Both are reasonable, depends on which better suits the situation


Q: When comparing strings would you first need to check if they are the same length?

A1: No, if string a is a complete prefix of string b, ithen a is less than b


Q: Does he mean s2 instead of s3?

A1: I believe there are three strings declared in his example, s1, s2, and s3


Q: Can we refer to indexes ithat don’t exist in the case of other data structures such as dictionaries in C++ just as we can do in python?

A1: It depends on the data structure what the effect will be. For example, attempting to access the 9th member of. Vector that has only 4 elements will raise an error. Trying to access a non-existent key in a Map will generate create a new empty entry for it


Q: how do hackers exploit a buffer overflow?

A1: there’s a whole class on this, cs155! It’s a little too complicated to explain concisely unfortunately

A2: Come to my office hours and I’ll tell you in person! (but only if you promise not to do evil)


Q: does that compare length or ASCII value

A1: Can you identify what you are referring to by ā€œthatā€?


Q: Can you use s.find() to look for a ch instead of string?

A1: yes, the function is overloaded, two versions one that looks for single character, another that looks for substring


Q: could we get an example of s.compare?

A1: string s = ā€œappleā€, t = ā€œbananaā€; int result = s.compare(t)

result will be negative, zero, or positive depending on ordering of s to t (less, equal, greater)


Q: yes sorry the compare function Chris just went over

A1: string s = ā€œappleā€, t = ā€œbananaā€; int result = s.compare(t)

result will be negative, zero, or positive depending on ordering of s to t (less, equal, greater)


Q: that returns -1, 0 or 1

A1: live answered


Q: Where is the other video on strings?

A1: posted in the same place the lecture videos are posted! Canvas > course Videos


Q: where is the video on strings - I only saw a video on references?

A1: posted in the same place the lecture videos are posted! Canvas > course Videos


Q: Does C++ work like Java where strings with the same characters can still be false when evaluating equality?

A1: No, that is a quirk particular to Java (about whether equivalent strings evalute as true ==), C++ string equality works as you would expect


Q: when/why do we use references?

A1: To pass a variable with a function and allow the function to make a persistent modification to it


Q: What is the difference between taking in a string by value versus by reference?

A1: If the function changes a string that was passed by reference, the change will be persistentand seen after function completes If the parameter passed by value, any change only exists inside the function


Q: there’s 3 t’s in b ??

A1: live answered


Q: why isn’t it TreeFOOt?

A1: live answered


Q: isnt b just TreeFOOt?

A1: live answered


Q: Isnt there an extra e in the new b value

A1: live answered


Q: does the insert put what you are inserting at the index (thus replacing the previous char at that index), or after that index?

A1: first character of the inserted string will be placed at index


Q: Where did the extra 'e' come from?

A1: live answered


Q: Why is there an e after FOO?

A1: live answered


Q: Shouldn't that be trefooet?

A1: live answered


Q: was there an extra e added by mistake?

A1: live answered


Q: Is there an extra ā€œeā€?

A1: live answered


Q: how did he get an extra e at the end of Tree?

A1: live answered


Q: I think it should be TreFOOet, not TreeFOOet

A1: live answered


Q: why are there 3 ā€˜e’s?

A1: live answered


Q: There should only be one "e" before foot, right?

A1: live answered


Q: Why is the final string TreeFooet and not TreeFoot? Where does the third e come from?

A1: live answered


Q: how can you refer to string a if it’s defined in a seperate function?

A1: You generally are not able to access variables outside of the current function.


Q: why is it et at the end?

A1: live answered


Q: Wait is it TreFOOet or TreeFOOet? Does the insert put the FOO in before or after the referenced character?

A1: live answered


Q: cool yeah thx for catching

A1:


Q: Thank you!

A1:


Q: So insert(3,…) will insert the string before the current character at index 3?

A1: Correct.


Q: Does the world at large also have access to the Stanford library?

A1: We publish Stanford library, but it not in common use outside of academia


Q: In the .insert function, is the FOO inserted before or after the third indexed character?

A1: the first characer of the inserted string is place at the index you insert at (in this case index 3)


Q: how do ascii values work for strings? is the ascii value of a string the sum of the ascii values of its characters? or the ascii values put next to one another?

A1: Ascii is defined for single characters, there is not an equivalent for an entire string.


Q: sorry if this has been answered / is obvious, but if I have a helper function that returns a string can I then access that string in say, a main function or something?

A1: Save the return value of function call by assignig to a string variable:

int main() { string result = myFunction(args);


Q: where can we find the reference video?

A1: The Course videos page of Canvas


Q: Are the two videos (extra preview and mini reference) both mandatory and cover material relevant to the assignment due this week?

A1: live answered


Q: are hashmaps collections

A1: Yes


Q: this doesn’t directly relate to the current lecture but is it considered improper form/syntax to subtract from the index within a for loop? as in for(int i = 0, i<x. i++) and then having an i- - for certain cases within the loop

A1: It is unusual and makes the code a little harder to follow, but works just fine


Q: should we watch the additional videos? is it supplemental information or like lecture?

A1: live answered


Q: are vectors arrays?

A1: Vectors are not arrays - there are some key differences between the two that we’ll talk about later in the class!


Q: what's the meaning of collection? what is it used for?

A1: Collection means an aggregate type, does not store a single value but many values gathered into one


Q: Do we have the Stanford library by default in Qt?

A1: All of the assignment projects will have the Stanford library avaialble by default


Q: is vec part of the syntax or is that the name of your vector?

A1: It is the variable name


Q: Can we use vectors in both parts or assignment one?

A1: Yes, you will need Vectors in both perfect and soundex


Q: so when we initialize a vector vec, we dont need to set it equal to = {}? we can just type vec; ?

A1: Yes. The default initialization for a Vector creates an empty vector


Q: in terms of memory or efficiency, are there any advantages of a vector of characters versus a string? (of course, we wouldn’t have access to all of the built in functions for string)

A1: string is generally more efficient and richer operations/functionality


Q: How do you actually import a Stanford library outside of just assignments for class?

A1: Copy one of our provided project as a starting point


Q: Where do we initialize a vector? is it at the top of our program like in Java?

A1: You can initialize the vector where you declare it


Q: Is there ever a reason to create a vector of chars instead of a string?

A1: Not really


Q: so is collection just like array that in another language (don't remember which tho)?

A1: Yes, arrays, lists, vectors are all the same abstraction


Q: why do you not loop through magic.size() - 1?

A1: The last iteration of the loop is size -1, correct


Q: what does the function size actually do for a vector?

A1: returns the number of elements in the vector


Q: How do str.substr() work when you want to split a string from the other end of a string?

A1: substr somewhat like python ā€œsliceā€, can to pick off prefix, suffix, etc. by how you choose start and stop arguments


Q: do we need to do capital V Vector functionName when declaring a function?</i>

A1: The typename is capital V Vector in all places where you use the type


Q: Can the data type in the vector only be primitive data types?

A1: You can store any type in Vector, primitives, classes, structs, custom user types, and so on


Q: Wait I may have missed this, but do you need to watch the extra videos to do assignment 1?

A1: live answered


Q: can a vector have characters instead of numbers, and if so then is it still vector or something else?</i>

A1: yes it can! if you wanted to store characters that would be Vector


Q: like what if I want it to be a list of strings?

A1: Vectors can store alomost any type! So you would declare a Vector


Q: Is there a way to print the vector whole? What would this output look like?

A1: cout « vec « endl;


Q: is ā€œvecā€ in the examples like vec[i] a stand in for the actual name of the vector?

A1: The variable was named vec so that is how we later refer to it


Q: for vec[i], where shall we specify the string of concern?


Q: is vec.add() equivalent to append or extend in Python?

A1: Yes


Q: can we do arithmetic with vectors? Like sum, multiply, divide with another vector of same size?

A1: No


Q: Does vec.sort() change the original vector?

A1: yes! it modifies the vector in place and does not return anything


Q: Is there a function or possibly a variable for putting a placeholder inside a vector (eg in matlab, NaN can be used as placeholders inside a vector) ?

A1: No, there is not the notion of a placeholder that fills empty slots


Q: Can you call vec.size() on an empty vector?

A1: Yes, it will return 0


Q: where do we find these other vidoes?

A1: Course videos tab on Canvas


Q: Where can we find the lecture slides again?

A1: course webiste under the lectures tab


Q: Will there always be videos or is that just because we’re running behind this week?

A1: live answered


Q: since a string is an array of characters, can i add a character to a string?

A1: Yes