Promoting C++11 and the STL

Pages: 123
size_t is nothing.
What about explaining what's wrong with this? And convincing them to do it the "right" way.

1
2
for (int i = 0; i < v.size(); ++i);
// where v.size() is std::vector<int>::size_type 


(For the sake of argument consider that you need the index i.)
1
2
3
for(auto& ele : vec) {
    //do stuff with ele
}


Ranged based for ftw.
closed account (3hM2Nwbp)
catfish3 wrote:
What about explaining what's wrong with this?


There's no need to re-explain every possible type-comparison that they might encounter. Each method that they encounter provides its return type, and figuring out where to plug it in is even easier ( just put it where your instructor told you to put 'int' ).

From what I can see, there is only one additional step required: look at the method's return type.

(For the sake of argument consider that you need the index i.)


'i' won't do you much good on most platforms when v has enough elements for the sign bit to be used when accessing indices.
Last edited on
From what I can see, there is only one additional step required: look at the method's return type.

I gotcha but the return type is size_type, isn't it?
And when they try to use that, it won't work, will it?

'i' won't do you much good on most platforms when v has enough elements for the sign bit to be used when accessing indices.

You mean cases where int would be safe to use instead?
Luc,

CollegeProfessor wrote:
Don't worry about the return type, 'size_t' -- you can think of it as 'int'.


Regarding: http://cplusplus.com/forum/lounge/88608/2/#msg476216

Do you really EVER expect to have a string with a size of 4294967295? That is absurd. How can you possibly see a practical conflict? Especially in a beginner class where people are learning how to type "Hello World".

Is he wrong? Technically, but in what possible scenario would him giving the "correct" answer not simply confuse students in their first C++ class?

The quoted statment above is a safe assumption when someone is still learning the syntax of an if statement, and what a while loop does. On my first day of class, if you introduced #include <iostream> in the hello world tutorial and went off to a tangent regarding typedefs and long vs int vs unsigned int, I would have never learned C++. I'm glad I never learned from you.
Last edited on
closed account (o1vk4iN6)
You are on a system where int is 8 bits and <>::size_type is 16 bits.
closed account (3hM2Nwbp)
Stewbond wrote:
I'm glad I never learned from you.


Incidentally, I am very glad that I am not a college professor, as I lack the needed patience.

In the vast majority of American universities that I have researched, C++ is not an entry level class. Prior topics usually involve a type of introduction to computer programming most often using pseudo-code. The concepts of variables, control structures, memory theory, etc should already have been mastered in a language-agnostic fashion prior to taking a C++ course. Learning the syntax of the various facets of C++ is very important and should most definitely take longer than a single session, being taught in the correct order. From my experience, these topics that are glazed over the first time around almost always end up coming back to haunt the student later on in their career. What's worse? The students are usually given no indication that what they've been taught is considered to be bad practice.

Having a 2^32 character long string is absurd, and there is certainly no "practical conflict" in most cases. That's quite a bad example for me to be nitpicking, but that was on the first hit that I got when I searched google for a college C++ course. That is nothing compared to the greater horrors that are being taught even as I type this.

Is he wrong? Technically, but in what possible scenario would him giving the "correct" answer not simply confuse students in their first C++ class?

Shouldn't it be the professor's responsibility to make sure that the students are actually absorbing the material rather than standing at the podium and reading verbatim from a text-book? It seems to me that any professor that just glazes over material, dumbing it down enough so there is no confusion, is taking the easy way out and frankly is not doing their job. Conversely, the students that aren't willing to devote the time to conquer their confusion need to reevaluate what they are going to school for.

I'm extreme, I know and accept that.

On a trailing note, I just hate the signedness comparison warnings that many people just ignore (or aren't aware of) and leave in their code. If only that college professor had hit that home...

Ah...just remembered another thing:

Do you really EVER expect to have a string with a size of 4294967295?


A std::string (size_t = unsigned 32 bit integer) with that size would already be broken, as it would have a length of std::string::npos.
Last edited on
Topic archived. No new replies allowed.
Pages: 123