![]() |
Vectors
Hi y'all.
I learn by example and writing my own code, and I've been working on vectors recently. I'm going to post my code here, and if anyone could point out a more efficient way to do it, and more commands I could add so I may educate myself further, please do so. Thanks :) :
#include <iostream>Much appreciated. |
Re: Vectors
well you dont need to keep track of the size of the vector in your x variable. you can just use names.size().
|
Re: Vectors
Ahh. So something like this? Seems to work fine.
:
#include <iostream> |
Re: Vectors
main is type int, you're a heretic if you do otherwise, etc. |
Re: Vectors
Lol, my instructor always used void in her examples she gives us. Guess I picked up the bad habit.
|
Re: Vectors
you could also use an algorithm and an iterator to print the vector instead of the for loop. then your code would not depend on an random access container.
:
#include <iostream> |
Re: Vectors
Okay, this is what I have now:
:
#include <iostream>The only thing I don't understand is where the iterator comes into play here.. If you could explain that for me it would be great. (edit: found a flaw in the stop loop, adding it as a name. included the pop_back command) |
Re: Vectors
View a file as a container of strings, where the first item in the container is the first thing written to the file and the last item in the container is the last thing written. osteam_iterator is simply an adapter that, as far as the copy algorithm is concerned, makes an ostream look like a container.
Incidentally, for entry of a long list, a user will find it rather inconvenient to have to enter each element and then, for every element, having to give two responses. Users are inconvenient at times, but it is usually a good idea for a programmer not to inconvenience them. |
Re: Vectors
@grumpy
Taking that into consideration, I rewrote it to be more hassle-free for the user. :
#include <iostream> |
Re: Vectors
I was entertaining the thought of re-writing the program and using classes. How could I add to the program without exiting my realm (being semi-new to the c++ game), while giving classes within the program actual meaning (i.e., a reason to use an OOP design versus a procedural syntax {I guess a larger program would be in order})?
|
| All times are GMT -5. The time now is 4:58 PM. |
Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC