Programming Forums

Programming Forums (http://www.programmingforums.org/forumindex.php)
-   C++ (http://www.programmingforums.org/forum15.html)
-   -   Vectors (http://www.programmingforums.org/showthread.php?t=15643)

White-Hat` Apr 17th, 2008 1:14 PM

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>
#include <string>
#include <vector>
using namespace std;

void main() {
        string end = ("no");
        string inser;
        string lst;
        int x = 0;
        vector<string> names;
        do {
                cout << "Enter as many names as you'd like." << endl;
                cin >> inser;
                names.push_back(inser);
                x++;
                cout << "Would you like to end?" << endl;
                cin >> end;
                cout << "Would you list to list the names?" << endl;
                cin >> lst;
                if (lst == ("yes")) {
                        for(int i=0;i<x;i++) {
                                cout << names[i] << endl;
                        }
                }
        }
                while (end != ("yes"));
}


Much appreciated.

mbd Apr 17th, 2008 1:17 PM

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().

White-Hat` Apr 17th, 2008 1:22 PM

Re: Vectors
 
Ahh. So something like this? Seems to work fine.

:

#include <iostream>
#include <string>
#include <vector>
using namespace std;

void main() {
        string end = ("no");
        string inser;
        string lst;
        vector<string> names;
        do {
                cout << "Enter as many names as you'd like." << endl;
                cin >> inser;
                names.push_back(inser);
                cout << "Would you like to end?" << endl;
                cin >> end;
                cout << "Would you list to list the names?" << endl;
                cin >> lst;
                if (lst == ("yes")) {
                        for(int i=0;i<names.size();i++) {
                                cout << names[i] << endl;
                        }
                }
        }
                while (end != ("yes"));
}


peaceofpi Apr 17th, 2008 2:03 PM

Re: Vectors
 
main is type int, you're a heretic if you do otherwise, etc.

White-Hat` Apr 17th, 2008 2:26 PM

Re: Vectors
 
Lol, my instructor always used void in her examples she gives us. Guess I picked up the bad habit.

mbd Apr 17th, 2008 2:27 PM

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>
#include <string>
#include <vector>
#include <iterator>
#include <algorithm>
using namespace std;

void main() {
        string end = ("no");
        string inser;
        string lst;
        vector<string> names;
        do {
                cout << "Enter as many names as you'd like." << endl;
                cin >> inser;
                names.push_back(inser);
                cout << "Would you like to end?" << endl;
                cin >> end;
                cout << "Would you list to list the names?" << endl;
                cin >> lst;
                if (lst == ("yes")) {
                        copy(names.begin(), names.end(), ostream_iterator< string >(cout, "\n");
                }
        }
                while (end != ("yes"));
}


White-Hat` Apr 17th, 2008 2:55 PM

Re: Vectors
 
Okay, this is what I have now:

:

#include <iostream>
#include <string>
#include <vector>
#include <iterator>
#include <algorithm>
using namespace std;

int main() {
        string end = ("no");
        string inser;
        string lst;
        vector<string> names;
        do {
                cout << "Enter as many names as you'd like; Enter stop when you're finished." << endl;
                cin >> inser;
                names.push_back(inser);
                if(inser == ("stop")) {
                names.pop_back();
                cout << "Would you like to list the names?" << endl;
                cin >> lst;
                cout << "Would you like to end?" << endl;
                cin >> end;
                }
                if (lst == ("yes")) {
                        copy(names.begin(), names.end(), ostream_iterator<string>(cout, "\n"));
                }
        }
                while (end != ("yes"));
}


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)

grumpy Apr 17th, 2008 3:52 PM

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.

White-Hat` Apr 17th, 2008 4:24 PM

Re: Vectors
 
@grumpy

Taking that into consideration, I rewrote it to be more hassle-free for the user.

:

#include <iostream>
#include <string>
#include <vector>
#include <iterator>
#include <algorithm>
using namespace std;

int main() {
        string end = ("no");
        string inser;
        string x;
        vector<string> names;
        cout << "Enter as many names as you'd like; Enter stop when you're finished." << endl;
        do {
                cin >> inser;
                names.push_back(inser);
                if(inser == ("stop")) {
                names.pop_back();
                cout << "If you would like to list the names you have written, enter \"list\". Otherwise,  enter \"quit\" to end. To enter more names, enter \"more\"." << endl;
                cin >> x;
                }
                if (x == ("list")) {
                        copy(names.begin(), names.end(), ostream_iterator<string>(cout, "\n"));
                }
                        else if (x == ("more")) {
                                cout << "Okay, we'll enter more!";
                                cout << endl;
                        }
                        else {
                                if (x == ("quit")) {
                                        end = ("quit");
                                }
                        }
        }
                while (end != ("quit"));
                return 0;
}


White-Hat` Apr 18th, 2008 3:19 PM

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