Exercises

Pages: 12
How do I turn on pedantic and Wall like you mentioned?
closed account (jwC5fSEw)
I don't know how to do it in Dev-C++. In Code::Blocks (which, by the way, I'd recommend switching to; it's rather like Dev-C++ but VERY actively developed), you go to Compiler and Debugger options, and there's a list of compiler flags there. Switch on -Wall and -pedantic.

I'm sure Dev-C++ has a menu similar to Code::Blocks'.
I understand the usefulness of extensions but I don't think they are very good for promoting good practices.
And don't use dev. You can switch to wxdev instead, which is (afaik) still developed. I use VC++ personally.
Well, I'm limited to the compiler that I can use because I have to have something I can upload at work that isn't blocked by my work's IT. And DevC++ isn't blocked.
Doesn't hurt to try. But you just gotta love the restrictions of some boneheaded IT departments.
@oghmaosiris this isn't my first program. I've used arrays before, and understand them fairly well I think, and I get what vectors do, I'm just not sure how to set up the vector for this particular program. Do I need to use something like:
1
2
3
for (int loop = 0; loop < 5000; loop++) {
          grades.push_back(loop);
          }

somewhere near the beginning of my code? This is the only thing I can find in my book that seems useful...
Well, the vector starts out empty. To add an element, you, as you've noticed, push_back. So you'd have something like this, in my imagination:
1
2
3
4
5
6
7
8
9
while (temp != 0) // or some other exit flag
// you could also use while (!cin) which checks for eof and such
{
    cin >> temp;
    if (temp == 0)
        break; // this is better than archaic priming which I used to do
        // and you don't want the exit flag to be inserted into the vector
    grades.push_back(temp);
}

This allows for an indeterminate number of elements.
You wouldn't use a for loop in most situations with a vector like this (for indeterminate input). You'd use a while loop, as I suggested. Or maybe do while, if that's how you like it. You see, you can have any number of elements in a vector, constrained only by the ram your comp provides you. So you want to let the user decide when they're done. That's the advantage vectors have over arrays, and the one that makes them so much better: they have an indeterminate size. Vectors are also typelike and can be passed as plain objects (although the convention seems to be passing by pair of iterators), where as arrays are essentially pointerlike and therefore less.. simple to work with.
Last edited on
I think I get what you're saying. Thanks. And if I'm not mistaken(please correct me if I am), you should try not to use pointers with vectors, because when they reallocate their space, whatever you're pointer was pointing to may end up in a different place, right?
Last edited on
People don't use pointers to vectors because you have iterators. That's the main reason I'm aware of. But yes, that's also possible. I can't speak to whether or not it's true but I can imagine it happening.
Understand how vectors work, though. (this is in case you don't yet know this) A vector reserves a contiguous block of memory in which it can store several elements, more than it starts with (vectors start empty normally so no surprise there). Unlike length(), this mass is returned from capacity(). When the vector is forced to exceed its preallocated space, it must seek out a new, larger block of memory (how much larger is up to the implementation, and can vary) and move all its elements there, followed by deallocating its old block. I believe you can force a realloc with reserve().
So a vector won't relocate out of nowhere. It will take some time for it to do so, normally. But it will probably end up doing so eventually if you fill it up enough.
Ah, ok. This is all very interesting stuff. I can't believe I used to think HTML was a handful. haha.
Topic archived. No new replies allowed.
Pages: 12