337 C++ (23-24) Flashcards
True or false, you can copy built in arrays
False, you cannot copy built in arrays
Can you resize built in arrays if they are created on the stack?
No. You cannot resize them if they are created on the stack
When using a vector, what header file must you include?
#include <vector>
What is the syntax to declare a vector object?
vector <data type> arrayName;
`
What does the following code snippet do? vector<double> a;
creates an empty vector
`What does the following code snippet do?
vector<double> a(5)
`
creates a vector with 5 elements all with 0.
True or false, vectors are automatically initialized to 0.
True
What does the following do? vector<double> a(3, 300)
Creates a vector that contains 3 elements, each containing 300.0
Can [] be used to access vectors?
yes, it works just fine for vectors. For example vector<double> grades(5); grades[4] = 8.0;
How do we resize vectors. Let's say we want to resize the following. vector <int> a(originalSize);
`
a.resize(newSize);
`
What does the following do?
`
v.empty
v.push_back(value);
v.pop_back(value)
v.at(index);
v.assign(n, value);
v.clear();
`
`
v.empty /returns true of vector is empty
v.push_back(value); //inserts the value to the end of the vector
v.pop_back(value) //returns and removes the last element
v.at(index); //same as v[index]
v.assign(n, value); //assigns value to the first n elements
v.clear(); //deletes all elements
`
Should vectors always be passed by reference?
It is recommended that vectors always be passed by reference. If necessary, use the const keyword. Example double average(const vector<int> &data) {}
What is typedef used for?
You can use typedef to create a new data type name from existing data types.
What is the general format for using typedef to create a new data type name?
typedef existing_type_name new_type_name;
Let's say that we want to define a vector of a vector for a 'matrix'. We currently have: typedef vector<int> row;
typedef vector<row> matrix; or we can do typedef vector<vector<int> > matrix;
The following code will create a matrix with 3 rows and 5 columns. Fill in the blanks
`
const int numRows = 3;
const int numCols = 5;
m.resize(numRows); //sets the number of rows
for(int j = ___; j <_______; j++)
m.at(j).resize(______);
`
`
const int numRows = 3;
const int numCols = 5;
m.resize(numRows); //sets the number of rows
for(int j = 0; j < numRows; j++)
m.at(j).resize(numCols);
`
We want to create a column for every row.
True or false, if a matrix is used as a type for function parameters, it must be defined prior to the prototype of the function.
True. This intuitively makes sense, since if a function uses it, it must be defined beforehand.
When declaring strings what header file, if any should you include?
#include <string>
What does the following do? string stringName;
It will create an empty string.
Are spaces allowed in strings? How many characters is the following?
`
string course( “ENCM 399” );
`
The string has 8 characters. Spaces are counted
What does the following do?
`
string firstInitial(1, ‘J’);
`
This creates a string with 1 element and initializes it to J
What would be the contents of student 2?
`
string student1(“John Smith”);
string student 2(student1);
`
The second string is also John Smith
What would be the range of a string, where N is the length of that string?
The index into the string should be in the range of 0 to N-1.
What is the difference between .size() and .length()?
.length() is part of the string/vector library.
.size() is more general.
Both of them will return the number of characters in a string.
What is the following value of b? string s2(1, ' '); int b = s2.size()
b is 1
What is the output? string b("String"); b = 'z'; cout<<b<<endl;
b = z. The string on the left hand side will automatically be resized.
Should we pass a string by reference in a function?
It is recommended but not required that a string be passed by reference in a function.
What does the following output?
`
string message(“Hello”);
cout «message;
`
Outputs Hello
What does the following output? #include <iostream> #include <string> int main() { std::string result = "Hello, "; // Append additional text using += result += "World!"; std::cout << result << std::endl; return 0; }
Output: Hello, World!
What does the following output? ` #include <iostream> #include <string> int main() { std::string result = "Hello, "; // Append a specific substring result.append("World!", 3, 3); std::cout << result << std::endl; return 0; } `
Output: Hello, rld
What does the following output? ` #include <iostream> #include <string> int main() { std::string result = "Hello, "; result.append("World!"); std::cout << result << std::endl; return 0; } `
Output: Hello, World
When will cin stop reading input?
It will stop at the first whitespace character. This means you can only read one word at a time.
When will the getline function stop reading input?
It will read a line of text up to but not including the delimiter character (which defaults to \n).
Complete the implementation of the getline function. string line_of_text; cout << "Enter a line of text: "; getline(\_\_\_, \_\_\_\_\_\_\_\_\_,\_\_\_\_)
string line_of_text; cout << "Enter a line of text: "; getline(cin, line_of_text ,'\n');
What will lines 2 and 3 do?
`
string course(“ENCM339 class”);
course.erase(4, 3);
course.insert(4, “ 369”);
`
`
string course(“ENCM339 class”);
course.erase(4, 3); // course is “ENCM class”
course.insert(4, “ 369”); // course is “ENCM 369 class”
`
What does the following code do? #include <iostream> #include <string> using namespace std; string trim_trailing_spaces(string& s){ while((s.at(s.size()-1) == ' '|| s.at(s.size()-1) == '\n') && s.size()> 0) s.pop_back(); return s; } string trim_leading_spaces(string& s) { size_t start = s.find_first_not_of(" \n"); return s.substr(start); } int main(void){ string s = " Hello World "; cout << trim_trailing_spaces(s) << endl; // prints “ Hello World” cout << trim_leading_spaces(s) << endl; // prints “Hello World” return 0; }
It will remove leading and trailing space using pop_back
What do the following string member functions do?
size()
length()
resize()
capacity
clear()
empty()
back()
front()
push_back()
c_str()
data()
Find()
compare()
size() Return length of string
length() Return length of string
resize() Resize string
capacity Return size of allocated storage
clear() Clear string
empty() Test if string is empty
back() Access last character
front() Access first character
push_back() Append character to string
c_str() Get C string equivalent
data() Get string data
Find() Find content in string
compare() Compare strings
How would we initialize a pointer to a pointer.
int **p2; We can also use the double ** to dereference. **p2 = 16; //assuming that p2 is pointer to another pointer.
What does the following code do? int * a[5];
It creates an array of five elements of type int*
What is the asterisk used as for the unary and binary operators?
Multiplication is a binary operation
Pointer notation and dereferencing of pointers are unary operators
What has the higher precedence, [] or *
The operator [ ], has higher precedence than unary *
operator. Therefore applying the order of precedence for []
and * implies to read the following declaration as: a is an
array of 3 pointers:
int *a[3];
What is the output of the following code? #include <iostream> int main() { const char* p[3]; // p is an array of char* pointers p[0] = "XYZ"; p[1] = "KLM"; p[2] = "ABC"; // Output statements std::cout << p[1] << std::endl; std::cout << *p[1] << std::endl; std::cout << **p << std::endl; std::cout << *(p + 1) << std::endl; return 0; }
KLM
K
X
KLM
In the following code, which area is the string located in?
p[0] = “XYZ”;
first element points to “XYZ” in the static area
What is the third element?
char *arr2[3 ] = { “ABC”, “XYZ”};
The third element is a null pointer
What is the value of arr4[1]?
const char *s = “KLM”;
char *arr4[ ] = {s , &s[1]};
arr4[1] points to “LM”
What does the following do? int main() { double *p[3]; int i; for (i = 0; i < 3; i++) p[i] = new double[i+2]; **p = 5.3; p[1][0] = 9.0; //???
We assign the first element of the 2nd value to 9.0.
Does C and C++ allow arguments in the main function?
Yes, the first argument is a pointer that may point to an array. Each pointer may refer to a token from the command line.
Assume we have: ./a.exe cat cow dog int main(int argc, char **argv) { for(int j = 0; j < argc; j++) cout << argv[j]; // point one return 0; } // prints: ./a.exe cat cow dog