Midterm 1 - Specific Topics Flashcards
what are some collections we have learned?
- stacks
- sets
- vectors (stanford)
- arrays
- string (collection fo chars
- maps, sets
- queues
what is a string a collection of?
chars
how do you declare a string?
std::string msg = “hello”;
what is casting?
allows you to forcibly convert from one type to another
casting example
std::string name = std::string(“Nate”) + “Dog”;
what are some member objects of a string?
.clear() .empty() .front() .back() .find() .substr()
what is .substr()
takes 2 parameters
-index to start at
-number of char to include
returns new string that contains requested range char
substr example
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 do you check to see if an item is in a string?
.find()
what is .find()
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
example of .find()
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
what is passing by reference?
change will persist
what is passing by value?
make a copy
what do we pass by value?
basic types (int, double, float, char)
what do we pass by reference?
non-basic types (std::string, std::ifstream, std::ofstream etc.)
how do you declare a stanford vector?
Vector myVector;
how do you declare a stanford vector?
include “vector.h”
Vector myVector;
vector example: declare a vector of strings
Vector> names;
what are some member functions of vectors?
.size() .add() .remove(index) .isEmpty() (returns true if empty) .clear()
what is myVector.insert(index, element)
-inserts element before the specified index
what is std::getline
allows you to get an entire line of input (so it doesn’t stop until an enter)