My point is giving up so soon, 4 month/a year, is a self-fulfilling, self-defeating prophesy.
a half outdated c++ book. |
One book. My programming library has 50 currently because I decided to prune the really outdated or known bad ones.
At least with books, multiple, I get a range of what is good code and what isn't.
Learning what is good code and is not happens when one writes their own code that isn't just copy 'n' paste.
There isn't a single book written even today that isn't outdated the moment it is written. How many books deal IN-DEPTH with C++14. Not a one that I can find. Forget about C++17 or known constructs of what will be in C++20.
Too many even recently published books don't really grasp what C++11 added to the language. STL containers? Nah, let's stick with C style arrays. Need random numbers? rand()/srand() when C++ now has very robust architecture for generating random numbers. Both for trivial uses and serious needs.
tutorials on YouTube from a guy named Chilli tomato |
Watching video from ONE guy. Wow.
I've seen some of this guy's videos, and frankly IMO he ain't that good. Decent grasp of some of the concepts of the language without any real depth of what is available.
Obviously you will say otherwise.
If you want to watch videos don't go with just one source.
And don't give up so quickly or easily. I've stuck with playing around with C++ for 15+ years, a good amount of that time thinking I was never learning anything.
A lot of the progress happened when I started hanging out here at cplusplus back in 2014, looking at code written by a number of experts. Each one with a different style.
Different styles that in their own way peeled back the layers of the C++ onion.
Compiling code samples at cppreference were also helpful in understanding language concepts.
And with understanding I wrote mini test-bed programs from scratch to solidify my understanding. And then wrote larger programs that pulled together more language features.
Recently I was playing around with 2 dimensional vector containers, how to create and use them effectively, passing 2D vectors into functions, etc.
I wanted, if possible, a way of outputting the contents of a 2D container as easily as doing Plain Old Data like an int or double. Adapting the idea of overloading the std::ostream operator>> into two functions, one for the rows and the other for columns.
Worked great, but for each data type I had to write overloaded functions. A-HA! Templates!
I had earlier used templates to write output functions for 1D & 2D containers of any data type. I combined two different concepts previously learned separately into one package.
The programs also used different ways of looping through containers.
This is ONE of my Frankenstein's monster of program code, put together piece by piece until I got the following:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130
|
#include <iostream>
#include <vector>
#include <numeric>
template<typename T>
void Print1DArray(std::vector<T> const&);
template<typename T>
void Print2DArray(std::vector<std::vector<T>> const&);
template <typename T>
std::ostream& operator<<(std::ostream& os, const std::vector<T>& v);
template <typename T>
std::ostream& operator<<(std::ostream& os, const std::vector<std::vector<T>>& v);
int main()
{
// creating a sized vector
std::vector<int> a1DVector(5);
std::cout << "Displaying a sized 1D vector:\n";
std::cout << a1DVector.size() << '\n';
for (size_t itr = 0; itr < a1DVector.size(); itr++)
{
std::cout << a1DVector[itr] << ' ';
}
std::cout << "\n\n";
// creating a sized vector filled with a value other than zero
std::vector<int> another1DVec(8, 100);
std::cout << "Displaying a filled 1D vector:\n";
std::cout << another1DVec.size() << '\n';
for (auto& itr : another1DVec)
{
std::cout << itr << ' ';
}
std::cout << "\n\n";
std::cout << "Let's display that 1D vector again:\n";
std::cout << another1DVec.size() << '\n';
Print1DArray(another1DVec);
std::cout << "\n\n";
std::cout << "Let's display that 1D vector yet again:\n";
std::cout << another1DVec.size() << '\n' << another1DVec<< "\n\n";
std::cout << "Creating a 2-dimensional vector, enter row size: ";
int row_size;
std::cin >> row_size;
std::cout << "Enter column size: ";
int col_size;
std::cin >> col_size;
std::cout << "\n";
// create a 2 dimensional int vector with known dimensions
std::vector<std::vector<int>> a2DVector(row_size, std::vector<int>(col_size));
// initialize the vector with some values other than zero
int start = 101;
int offset = 100;
// step through each row and fill the row vector with some values
for (auto& itr : a2DVector)
{
std::iota(itr.begin(), itr.end(), start);
start += offset;
}
// let's display the filled 2D vector
std::cout << "Displaying the filled 2D vector:\n";
Print2DArray(a2DVector);
std::cout << '\n';
std::cout << "Let's display that 2D vector again:\n";
std::cout << a2DVector;
}
template<typename T>
void Print1DArray(std::vector<T> const& a1DVec)
{
for (size_t itr = 0; itr < a1DVec.size(); itr++)
{
std::cout << a1DVec[itr] << ' ';
}
}
template<typename T>
void Print2DArray(std::vector<std::vector<T>> const& a2DVec)
{
for (const auto& row_itr : a2DVec)
{
for (const auto& col_itr : row_itr)
{
std::cout << col_itr << ' ';
}
std::cout << '\n';
}
}
template <typename T>
std::ostream& operator<<(std::ostream& os, const std::vector<T>& v)
{
for (auto const& x : v)
{
std::cout << x << ' ';
}
return os;
}
template <typename T>
std::ostream& operator<<(std::ostream& os, const std::vector<std::vector<T>>& v)
{
for (auto const& x : v)
{
std::cout << x << '\n';
}
return os;
}
|
Displaying a sized 1D vector:
5
0 0 0 0 0
Displaying a filled 1D vector:
8
100 100 100 100 100 100 100 100
Let's display that 1D vector again:
8
100 100 100 100 100 100 100 100
Let's display that 1D vector yet again:
8
100 100 100 100 100 100 100 100
Creating a 2-dimensional vector, enter row size: 3
Enter column size: 4
Displaying the filled 2D vector:
101 102 103 104
201 202 203 204
301 302 303 304
Let's display that 2D vector again:
101 102 103 104
201 202 203 204
301 302 303 304 |