Final Exam Flashcards
List 3 limitations with C-style strings.
1) Forgetting to end string with ‘\0’
2) Not allocating enough memory for string
3) Cannot add, delete, or combine elements
What is the preprocessor directive for strings?
include
What are two ways to initialize a string variable?
string name = "Mike"; or string name("Mike");
How do you use string concatenation?
Use + operator.
fullname = firstname + lastname;
What is the preprocessor directive for writing to files?
include
What are the five steps of writing to a file?
1) Create an output stream object
2) Establish connection to file
3) Verify connection
4) Start writing
5) Close file
What is the syntax for declaring an output stream object?
ofstream fout;
What is the syntax for opening a file?
fout.open(“filename.txt”, ios::out);
What is the syntax for verifying an output stream connection?
if(!fout.is_open()) { // cerr & exit }
What is the syntax for closing a file?
fout.close();
When do you need to convert a string to a c-sring and what is the syntax for converting a C++ string to C-style string?
Convert to c-string when using a string as the file name in an output file stream.
Syntax: stringName.c_str();
What is the function that returns the size of a string?
stringName.length()
What is the function that returns the size of a vector?
vectorName.size()
What is the preprocessor directive needed to use the sort function?
include
What is the syntax of the sort function?
sort(list.begin(), list.end());
What are two drawbacks to vectors?
1) Vectors take up more memory
2) Could take more time
When should arrays be used over vectors?
When the array is of a fixed size and when speed is an issue
What is the major limitation of arrays?
Size is constant and cannot be changed. Therefore, you might have allocated memory for the array that you did not use.
What are the six steps of reading a file?
1) Know the format of the file.
2) Create an input stream object (input file handler)
3) Establish connection to file.
4) Verify connection.
5) Start reading.
6) Close the file.