Home | About | Blog | Projects | Contact
Reading through C++ Primer (5th Edition) by Lippman, Lajoie, and Moo. I already know basic C++, so I'm starting with Chapter 2 (Variables and Basic Types) and Chapter 3 (Strings, Vectors, and Arrays).
I also decided next study session I am skipping Expressions and Statements and moving straight to functions and classes
I pretty much knew all of this stuff already, so I only really did the exercises here. Reading through all of this stuff and trying to do all the exercises for chapter 2 felt redundant because it was so easy. I documented all the exercises until I made this conclusion. I also decided that the exercises take too long to write in here, so in the future, I will probably only write exercises if I see they fit well
remember to initialize all pointers. Recipe for runtime errors. At least initialize to nullptr or 0 or NULL.
one takeaway I do have is the notion that pointers "point to" (address) and references "refer to" (value).
A reference is not an object. Once reference is defined, you cant make it refer to another object. Always refers to initially bounded object This is not true for a pointer. You can change what a pointer points to at any time.
In summary, pointers and references are both used to refer to other variables, but they do so in different ways. Understanding these differences is crucial for effective C++ programming.
void* Pointers are the same as regular pointers except the type of the object being pointed to is not specified. Can be converted to any other pointer type. Used for generic data handling.
const qualifier is when we want to prevent a variable from being modified. It must be initialized when defined.
It is helpful to think of pointers and references to const as pointers or references that think they point or refer to const.
top level const: pointer itself is const. low-level const: points to const data.
constexpr indicates that the value of a variable or function can be evaluated at compile time.
constexpr is a c++ 11 feature
constexpr asks the compiler to verify something is a constant expression
good practice to use constexpr for values that are intended to use as constant expressions
constexpr must be assigned to a literal type
constexpr imposes a top-level const on objects it defines
type alias: traditionally typedef, but c++11 introduced using keyword
using new_type_name = existing_type_name;
type alias makes something a synonym for another type name.
auto is a c++11 feature that allows the compiler to figure out the type of an expression
decltype is a c++11 feature that allows you to query the type of an expression
decltype((variable)) is always a reference type, but decltype(variable) is only a reference type if variable is a reference.
c++ we define our own data types by defining a class.
class body defines members of the class.
headers should have guards, even if they aren't included by another header. Header guards are trivial to write, and by habitually defining them you don't need to decide whether they are needed
// Example for exercise 2.8
#include <iostream>
int main() {
std::cout << "\x32\x4d" << std::endl;
std::cout << "\x32\t\x4d\n" << std::endl;
return 0;
}
size returns type string::size_type, not an int
we can use auto len = line.size() to declare a variable with length of string without having to type out string::size_type
push_back adds an element to the end of the vector at run time.
key concept: It is unnecessary to define a vector of a specific size because vectors can grow and shrink dynamically as elements are added or removed.
Caution: Subscript only elements that are known to exist
Dereferencing iterators returns the value they point to.
Comparing iterators checks if they point to the same element.
you can add and subtract iterators.
you can multiply and divide iterators.
Arrays are like vectors except fixed size
Typically if you don't know how many elements you need, use vectors
Arrays offer better run-time performance than vectors at the cost of flexibility
Array dimension must be known at compile time, so the dimension must be a constant expression
you cannot initialize an array as a copy of an array or assign one array to another.
When initializing a pointer to an array, it will point to the first element.
c++11 added begin(array) and end(array) functions to get iterators to the beginning and end of an array. (defined in iterator header)
A pointer "one past" the end of a built-in array behaves the same way as the iterator returned by the end operation of a vector. We may not dereference or increment a pointer that is one past the end of an array, but we can compare it to other pointers within the array.
// Exercise 3.14: Write a program to read a sequence of ints from cin and store those values in a vector.
int main() {
int x;
vector ivec;
while (cin >> x) {
ivec.push_back(x);
}
return 0;
}
//Exercise 3.23 Vector with 10 int elements-> use iterator To
//assign each element a value that is twice its current value.
int main() {
vector ivec{1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
for (auto it = ivec.begin(); it != ivec.end(); it++){
*it = *it * 2;
}
return 0;
}
Add questions and exercises here...
unsigned u = 10, u2 = 42;
std::cout << u2 - u << std::endl;
std::cout << u - u2 << std::endl;
int i = 10, i2 = 42;
std::cout << i2 - i << std::endl;
std::cout << i - i2 << std::endl;
std::cout << i - u << std::endl;
std::cout << u - i << std::endl;
int month = 9, day = 7;
int month = 09, day = 07;
std::string global_str;
int global_int;
int main() {
int local_int;
std::string local_str;
}
int i = 42;
int main() {
int i = 100;
int j = i;
std::cout << j << std::endl;
return 0;
}
int i = 100, sum = 0;
for (int i = 0; i != 10; ++i)
sum += i;
std::cout << i << " " << sum << std::endl;