337 C++ (23-24) Flashcards
(47 cards)
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.