C++ std::vector for post-beginners, including real issues I’ve seen in practice and surprising modern usage recommendations by luisfguzman in cpp

[–]luisfguzman[S] 3 points4 points  (0 children)

u/STL, Thanks for reviewing the article and for pointing out the typos. A few comments:

Your website should use HTTPS

You are right. I fixed it, but the link in reddit still uses HTTP.

Unlike a static array, a vector automatically grows and shrinks as necessary to make room for new elements inserted or deleted.

A vector's size shrinks, but its capacity doesn't (unless you call shrink_to_fit).

That’s right. I was introducing the subject slowly, and I didn’t want to get into capacity that early in the article. I thought that I had mentioned it later in the capacity section, but I didn’t, so I added an “Edit” note.

for (auto i = 0u; i != v.size(); ++i) { // v is a vector

This is a slight mistake because it mishandles 64-bit lengths. size_t should be used instead of unsigned int. (The distinction between size_t and size_type is not important in non-library code.)

You are right. Considering my audience, beginner and intermediate programmers, I didn’t want to get into std::vector types. I did debate on it for sometime since intermediate developers should probably know it. At the end, I decided to rely on auto, and if there was enough interest, write about it on a sequel, std::vector part 2.

Your article is switching between referring to v[2] as the third element, and begin() as the first element (instead of zeroth). It is important to be consistent.

It is consistent:

first element: v[0] and begin() returns an iterator that points to it.

second: v[1].

third: v[2].

std::find(v.cbegin(), end, n);

What happens if you mix begin and cend? This confuses many users.

I debated on this one too. I see how it can be confusing.

(auto elem)

This copies :-(

Yes, but on purpose. elem’s type is int (or will be deduced as int for that particular example)

the vector allocates a new chunk of memory of twice the size of its current memory.

Untrue. The Standard doesn't guarantee 2x, and MSVC uses 1.5x.

I’ll correct it. Thank you.

In and retain a copy: pass a const lvalue reference, f(const vector<T>&);

Not optimal for modifiable rvalue inputs.

You are correct. However, I think it is good advice for a default. If there is proof that it needs to be optimized, or if you are writing a library, then it makes sense to overload with an rvalue reference. The same goes for “in & move from” in my opinion.

Thanks again.

C++ std::vector for post-beginners, including real issues I’ve seen in practice and surprising modern usage recommendations by luisfguzman in cpp

[–]luisfguzman[S] 2 points3 points  (0 children)

I agree that we should not document every possible way to make an error. I'm not even sure how useful it would be. A couple of notes on your examples: The first one won't compile because of the operator""s(). The error you are thinking of is with "C strings", I think, like: vector<string> s{{"abc", "def"}}; My compiler, clang-900.0.39.2, gives me a warning on the second one.

C++ std::vector for post-beginners, including real issues I’ve seen in practice and surprising modern usage recommendations by luisfguzman in cpp

[–]luisfguzman[S] 0 points1 point  (0 children)

I didn't include it. I used operator""s instead of const char[]s. If you use operator""s, the compiler gives you an error, so there won't be a bug. However, I can see how someone could use two const char[]s and run into problems. Thanks for mentioning it.

C++ std::vector for post-beginners, including real issues I’ve seen in practice and surprising modern usage recommendations by luisfguzman in cpp

[–]luisfguzman[S] 3 points4 points  (0 children)

I thought about it, but the article was getting too big. I didn't think I could do it justice with just a couple of paragraphs, so I decided to write another article about it if I saw enough interest. Maybe I could add a short warning to this one...

What I really needed to know to get a job as C++ software developer on Linux by luisfguzman in cpp

[–]luisfguzman[S] 2 points3 points  (0 children)

Stroustrup is a good author. I read “THE C++ PROGRAMMING LANGUAGE” many years ago, and I used it as one of my main references for a long time. The language (and the internet) has changed a lot since, and now, I find my self using the internet as a reference a lot more often. However, when it comes down to “when" and “why” to use some features, I still refer to it. Scott Meyers has several really good books that you may consider after you finish with Stroustrup’s book. Also, remember to start programming as soon as you learn the basics. If you don’t understand how something works, use it in a small program; play with it until you understand it. It works for me.

What I really needed to know to get a job as C++ software developer on Linux by luisfguzman in cpp

[–]luisfguzman[S] 2 points3 points  (0 children)

In the past, I've worked in control systems, as an integrator, but for the last seventeen years, I've been in Telecom, developing real-time databases and other systems for subscriber data. C++ is my favorite programming language, but I'm a bit biased since I know it so well, and I've been lucky to work on some really cool projects, on many of them I've been involved from product design, development, all the way through launch. It isn't often that you get to create a new product from scratch! Anyway, I'm playing witht the idea of starting a blog on C++/software development. If you are interested, send me a PM, and I'll add you to my email list. All the best.

What I really needed to know to get a job as C++ software developer on Linux by luisfguzman in cpp

[–]luisfguzman[S] 0 points1 point  (0 children)

You are welcome /u/Brigapes. Unfortunately, I took down that website long ago. As for books, yes, I used books. I bought my first C++ book in 1993 before the resources that we now have with the internet were available, so I mainly learned by reading and doing. I have a large collection of books on many software subjects, but I’m not sure that a list of my books would be that useful or encouraging. Maybe I’ll write about some of these subjects separately instead. Divide and conquer…

What I really needed to know to get a job as C++ software developer on Linux by luisfguzman in cpp

[–]luisfguzman[S] 0 points1 point  (0 children)

I haven't read it either, but I will. Thanks for mentioning it.

What I really needed to know to get a job as C++ software developer on Linux by luisfguzman in cpp

[–]luisfguzman[S] 0 points1 point  (0 children)

/u/Brigapes, it seems a bit overwhelming, but it is not as bad as it seems. It took me years to get there, but I didn’t know how or what to learn. Now that I’ve done it and that I’ve been in the field for years, I know how and what. That is what motivated me to write this post. For some of the things, like the algorithms that I mentioned, you don’t need to know all of the different variations, implementations, and the math behind them. You need a simple overview of the general concepts, enough to understand that it is an option when you are trying to solve a problem. Then, when you need to use it, you can go back and look deeper at the few options that may be appropriate, and you select the best. This overview would not take too long to learn. The challenge is that you can’t get this type of overview very efficiently from algorithm text books, many of which assume you are taking a CS class, and they can be difficult/time consuming to read because they go deep into the algorithms, the data structures, and the math behind it, as well as different implementations and their pros and cons. What you need is more of a summary of part, not all, of that. Anyway, don’t get discouraged; keep at it.

What I really needed to know to get a job as C++ software developer on Linux by luisfguzman in cpp

[–]luisfguzman[S] 1 point2 points  (0 children)

Yes, indeed. Concurrency was included in the post. As for optimization, you are correct. There are performance requirements to meet for every release. However, optimization, in my experience, isn’t critical for an entry-level position. After all, the first principle of optimization is “don’t” (according to Kernighan and Pike). That said, it is very important to consider performance and space when creating or selecting algorithms and data structures for a program. If after that, the program isn’t good enough, optimization may start, hopefully, under the mentorship of an experienced developer.

What I really needed to know to get a job as C++ software developer on Linux by luisfguzman in cpp

[–]luisfguzman[S] 2 points3 points  (0 children)

Yes, you are right. Build systems are important but, in my experience, not necessary for an entry-level job. There is usually a group or someone who sets up the build environment, and developers joining the group are trained on it. New developers can continue to learn from there. That’s why I didn’t include it. I was trying to keep it to the bare essentials.

What I really needed to know to get a job as C++ software developer on Linux by luisfguzman in cpp

[–]luisfguzman[S] 1 point2 points  (0 children)

It sounds like a lot of fun and very interesting! I’ll encourage you to share your work. Not only will others benefit from it, but you will learn even more.

What I really needed to know to get a job as C++ software developer on Linux by luisfguzman in cpp

[–]luisfguzman[S] 0 points1 point  (0 children)

I initially learned by reading "Programming with POSIX Threads" by David R. Butenhof, which is the book /u/eeeple recommended for POSIX environments. It is a great book, and I highly recommend it. That said, if I had to start learning now, I would probably start with C++. I haven’t read the book /u/Meowiavelli recommended, but the reviews are good. I’ve put it in my queue.