Lecture 4 Zoom Q&A


Q: When is next week's assignment being released?

A1: Assignments will be released the day after the preceding one is due. Since Assignment 1 is due on Friday, A2 will be releaed on Saturday.


Q: How long should the weekly assignment take us in terms of hours?

A1: live answered


Q: are YEAH sessions recorded? :)

A1: Yes!

A2: Yes, Trip should have posted the recording last night


Q: how do we view our grades?

A1: You will have a grading conference with your section leader


Q: what are YEAH sessions??

A1: Assignment Help/tip sessions


Q: How are you today?

A1: Great, thanks! :)


Q: do we get extra credit on assignment 1 if we find the 52nd perfect number

A1: Absolutely!

A2: I think you should probably just graduate from Stanford at that point!


Q: Is the string comparison technically lexographical like in Java?

A1: Yes, based on ascii character encoding, so uppercase precedes lowercase letters for example


Q: When we submit homework files, should we uncomment all provided tests if we commented them out?

A1: yes


Q: So the comparison signs for strings look at order in alphabet, not the ASCII values for the characters in the strings?

A1: It is comparing characters values by ascii encoding, but those are in alphabetical order, so it ends up looking like the same thing


Q: Is there a document on the webstie detailing all of the relevant string/character methods?

A1: yes! check the C++ reference


Q: is append basically just += for strings?

A1: Yes, they have the same behavior


Q: Wouldn’t this be s2? Not s3?

A1: yeah, but the code still works the same


Q: are strings passed by value in functions?

A1: Yes, like all C++ parameters, the default is pass by value


Q: Is it supposed to be “s3 is now”

A1: yes


Q: Is there a reason why you'd pick erase vs. concatenating the substrings like was done in assignment 1?

A1: We have found that modifying string in place (with functions such as erase) end up being more confusing for students


Q: if we're chanign s3, why do the comments say that s3 is changing?

A1: typo. Imagine they all say s2


Q: Are we appending to s3 or s2 with the s3.append(“Giraffe”)

A1: s3. Comments just say s2

A2: s3, the comments have a typo in them


Q: how does “s.compare(str)” work?

A1: http://www.cplusplus.com/reference/string/string/compare/


Q: what is string: :npos ?

A1: Special constant that represents “no position” (i.e. not found)

A2: It’s a special constant that represents “doesnt exist in the string”. If you’re coming from Python, it’s the euqivalent of -1 being returned from the find function


Q: Is there a find that takes two inputs (str, n) and returns the nth occurrence of str? or would this have to be done manually with a loop?

A1: live answered


Q: Does rfind search for the reverse of str as well as going backwards or does it search for str going forwards but just starts searching from the end?

A1: Searches from end, but match is forward


Q: Why could tolower be called individually but functions like append() have to be called with a period?

A1: live answered


Q: When does compare return 0 or -1 or 1?

A1: http://www.cplusplus.com/reference/string/string/compare/

A2: '-1 if a < b 0 if a == b 1 if a > b


Q: why aren’t there 2 a’s when doing s3.erase(4,3)?

A1: the second param means eras 3 chars


Q: I noticed there was no compareTo method in that list. Is there no compareTo in C++, only Java?

A1: The function is named compare in C++, not compareTo


Q: what are member functions?

A1: Functions that belong to an object/class, they are invoked using the syntax object.function as opposed to global functions where there is no object. in front


Q: Apologies for asking something that has already been answered, but could you reiterate what the difference between the two tyeps of strings and their uses are? If I define a new string as "hello" it will be a C string unless I specifically convert it?

A1: In general, we use C++ strings for everyhing, the only case where you will end up with a C string is when you have a string literal (in double-quotes)


Q: Will we have to use C strings in this class or know how to convert them?

A1: It would be good to know!


Q: ohh so an example of a non-member function is toLower, right?

A1: yes, exactly!


Q: okay, thanks!


Q: if “text” is a C string, then how do you define a C++ string?

A1: by storing it in a variable of type string


Q: Is there an API for all Stanford library functions

A1: https://web.stanford.edu/dept/cs_edu/cppdoc/

A2: Liked under “Stanford Library Documentation” on the home page.


Q: How do we know when we are working with a C string?

A1: In general, we use C++ strings for everyhing, the only case where you will end up with a C string is when you have a string literal (in double-quotes), if you store that in a variable of type string, it will automaticaly convert for you


Q: So string literals without string() wrapped around them are C strings, and vice versa?

A1: yes


Q: If you did string.erase (x, 0) would nothing happen?

A1: correct

A2: Yes


Q: Isn't it potentially dangerous to call str[0] on an empty string?

A1: yes, it is


Q: What types have their data copied when passed to a function?

A1: Anything passed by referrence

A2: All types are passed by value by default in C++. You have to explicitly ask for a parameter to be passed by reference


Q: why is there no ampersand before the a in string a

A1: live answered


Q: Would it be wrong to write b = "b.insert……"? I feel like that makes it clearer that it's been modified.

A1: It would be redundant but not incorrect


Q: When using insert or erase on a C++ string, do the characters to the right of the change point (whose indices get changed) get moved in memory?

A1: Effectively, yes, the characters move index. How that works internally is opaque to us as a client of string


Q: why is there a return 0

A1: By convention, main always needs to return a value


Q: If you print a in the first function it will be edited right?

A1: Correct


Q: so can you chose when it will be reference by using &. In other words, could we have made A reference just by adding &?

A1: Yes, exactly


Q: What is the difference between member and non member functions again?

A1: Quoting Julie from earlier, member functions are functions that belong to an object/class, they are invoked using the syntax object.function as opposed to global functions where there is no object. in front. Non-member functions are standalone (like tolower)


Q: can we use the stoi function for string to integer?

A1: Yes.


Q: What is the difference between tolower() and toLowerCase()?

A1: The argument to tlower is a single char and returns single char, toLowerCase converts/returns string of characters


Q: So even complex data structures and instances of user-defined classes will be deep-copied every time they are passed by value?

A1: yes, which can make code slow.


Q: does trim remove whitespaces in the middle of words? or only before/after?

A1: only at the beginning and end


Q: when do we use C++ str and C str? Is there any benefit of using C str?

A1: In general, we use C++ strings for everyhing, the only case where you will end up with a C string is when you have a string literal (in double-quotes)


Q: what does Chris mean when he says C++ strings are more functional?

A1: They’re eaier to work with and have many more hepful funcitons defined on them


Q: If I do something like cout « "hello", is it printing a C string or a c++ one?

A1: C string


Q: what are sections like + how are they run

A1: https://web.stanford.edu/class/cs106b/about_section

A2: Think of it as a 50 minute long interactive problem solving time, led by your section leader


Q: Does this mean that to initialise a C++ string, I need to put it in a variable?

A1: Correct


Q: can we attend multiple sections or is that not allowed?

A1: It is allowed but def try to make your own


Q: Can’t we just return a collection if we want to return more than one thing from a function

A1: Yes, if the multiple things are of same type (the collection elements have to be homogenous type)

A2: Sometimes, but if you want to return multiple types it gets tricky


Q: Is Vector like an array in C?

A1: yes, but dynamic

A2: Yes, but with additional convenient functions, managed memory, bounds-checking so much better than raw array


Q: what happends if you don't have the bound check

A1: program will crash or behave unpredictably

A2: You might get an out of bounds error during runtime


Q: if you access an element from index and you want it to be bounds checked do you have to use vector.get(index) or can you just use vector[index]

A1: Both are bounds-checked


Q: Can you make a vector of vectors?

A1: Yes, you can


Q: Can Vectors hold Objects?

A1: yes

A2: Yes


Q: Is a library a class or can a class be part of a library?

A1: A class can be in a library


Q: When you print a vector can you do cout « vec « endl; or will that not work?

A1: Yup!


Q: can you print out the entire vector

A1: live answered

A2: Yes, you can cout « vec « endl;


Q: For this class, can we (or will we) use arrays at any point?

A1: yes, we will

A2: Yes, we will learn all about arrays in the middle of the quarter


Q: is there a simple-ish way to prepend to a vector?

A1: you can do vec.insert(val, 0) to say to insert something at index 0

A2: insert, or add one vector to another


Q: When you delete an element, does it leave behind a None or a null?

A1: It is removed and elements shifted over to close gap


Q: Can I use a for-each loop for Stanford library vectors

A1: yes

A2: yes, chris is talking about that right now


Q: is it more efficient to create a variable size and then use the size variable in the for loop rather than putting the function inside the for loop if you plan to use the function at least once inside the for loop?

A1: In some tiny way, yes. This is not a level of optimization we want you to pursue


Q: if you’re doing a foreach loop on a vector and inside the loop you add an element to the vector, i’m guessing the loop will not include the newly added element?

A1: It is not legal to modify a collection while iterating over it


Q: if vector got modified in a sun function, will it change the original value

A1: if passed by referrence

A2: If the Vector were passed by value, no; if passed by reference, yes

A3: It dependson whether or not the vector is passed by reference or not


Q: sub fucntion


Q: Is Vector a stanford creation or available in normal C++?

A1: Stanford specific

A2: Vector with capital V is a Stanford-specific thing


Q: Kind of a tangential question, but arrays don’t have the nice property of shifting everything left when removing an element, right? How do we deal with that

A1: We have to do it by hand! We’ll see that when we learn about arrays later in the quarter

A2: Copy everything over manually when deleting an element


Q: so a combination of vectors would be better if you plan on changing the size of the "grid" right?

A1: Sometimes! It depends on what you’re implementing and what you care about

A2: You can change the size of the grid, see resize function


Q: What's the difference between grid abc(10,20) and int two_d[10][20];</i>

A1: similar to the difference between an array and a vector – the class collection version is easier to work with and has functiones defined on it


Q: Can you create a 3D array with a vector of grids?

A1: Yes


Q: What’s the difference between a grid and a matrix?

A1: a matrix is of type grid

A2: Same idea, different term


Q: Can you have empty squares in a grid?

A1: All squares will have some value – they are initialized to the type default value to begin with


Q: Can you fill values within a grid with Null to simulate a "ragged right" matrix?

A1: yes


Q: Is Grid only Stanford specific?

A1: yes


Q: in a grid, is the first index the row or the column?

A1: row


Q: do you have to capitalize “Vector” and “Grid” whenever you use them

A1: yes, that is very important


Q: matrix here is just a name?

A1: yes


Q: Are grids and vectors passed by value or reference? How does the & operator play into this?

A1: All C++ parameters are passed by value by default (or any type), by adding & to the parameter type you are changing to pass by reference


Q: Can you print out a grid just like that? I know in Java it is stored weirdly so you need a specific function in roder to do so…

A1: yes you can print out a grid by saying cout « gridName « endl;


Q: which is faster: grid or combination of vectors?

A1: Performance is similar.


Q: Do we have to do a #include for Grid?

A1: #include “grid.h”


Q: Does grid.resize() do the same thing as assigning a new empty grid to the same variable

A1: Correct


Q: Is there is a way to 'purge' a variable in C++ if it's no longer needed

A1: yes, if it takes up memory, we will talk about this later in the quarter


Q: Can you intialize a Grid like Grid matrix(2,2) = { {1,3}, {2, 4} }</i>

A1: yup!


Q: so we would never have to have our column variable, only go up to grid[r].numCols() because the rows and columns are always rectangular?

A1: A Grid is rectangular, yes, every row has same number of columns


Q: For this example, would it be 3 arrays of 2 elements each, or 2 arrays with 3 elements each

A1: I’ve lost the context for this, but when creating a matrix, the arguments are in the order number of rows, number of columns

A2: 3 rows and 2 columns


Q: does it matter if the rows or columns are on the inside or outside loop?

A1: it will change the order in which things are iterated over


Q: Is it convention to have the row loop on the outside and the column loop on the inside?

A1: yes, that is the way you will see most loops over girds written


Q: syntactically, where does the & go? is it type& var, type & var, type &var, or does it not matter

A1: Does not matter, but pick a style and be consistent!

A2: Doesn’t matter, just stylistic preference


Q: Why is the & in the middle with spaces for passing with reference? When do we put it next to the variable name?

A1: Either is fine, means same thing, but pick a style and be consistent

A2: It doesn’t matter functionally, its more of a style thing. Just pick something and stay consistent in your own code!


Q: In “Grid”, are all the values in the grid bool? like T/F?</i>

A1: yes


Q: can you make a grid of vectors and have like a 3D array?

A1: Yes

A2: yes


Q: what’s GCanvas?

A1: It a class from our Stanford library that can draw a grid of pixels onto a graphics window


Q: In which language was the Instagram originally programmed?

A1: Good question! We should ask Mike!


Q: Is GCanvas more similar to Qt’s QPixmap or QImage?

A1: QImage (but you really don’t need to know that :-)


Q: Are we supposed to but & right next to the type or adjacent to the variable name

A1: It’s a stylistic choice. Either as fine as long as you’re consistent


Q: why is palette passed in as a reference variable when it isn’t being modified by the toonShade function

A1: passing by referrence is more efficient for large data structures


Q: is there a reason why palette is passed by reference in this function?

A1: passing by referrence is more efficient for large data structures


Q: Are we going to make something similar like ToonShade for class project?

A1: live answered


Q: Could you go over the passing by reference with a, b, and c again? I was a bit confused haha

A1: live answered


Q: If you pass an object by reference, and you make the variable refer to a new object within the function (not just modify the object), will the original variable refer to the new object?

A1: yes


Q: In slide 19, why do you use grid to hold bool values instead of int? In the loop, it puts integers in the grid.

A1: What do you mean when you say in the loop it puts integers in the Grid? The chosen type f the grid is bool, which is what it stores


Q: thank you Chris, Julie, Nick, and Chase!

A1: No problem!


Q: Can you concatenate a C string with a C++ string?

A1: Yes


Q: Is it okay to have the space between and &? (slide 19)</i>

A1: yes! It’s a style choice


Q: Thanks!


Q: Can you add and multiply booleans with ints?

A1: live answered