![]() |
|
![]() |
|
|
Thread Tools | Display Modes |
|
|
#1 |
|
Programmer
Join Date: Sep 2005
Location: Oopland
Posts: 36
Rep Power: 0
![]() |
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. |
|
|
|
|
|
#2 |
|
Programmer
Join Date: Nov 2007
Posts: 86
Rep Power: 1
![]() |
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().
|
|
|
|
|
|
#3 |
|
Programmer
Join Date: Sep 2005
Location: Oopland
Posts: 36
Rep Power: 0
![]() |
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"));
} |
|
|
|
|
|
#4 |
|
hi: for(;;) goto hi;
|
Re: Vectors
main is type int, you're a heretic if you do otherwise, etc.
__________________
How do you play Religious Roulette? Stand around in a circle and blaspheme till someone gets struck by lightning. |
|
|
|
|
|
#5 |
|
Programmer
Join Date: Sep 2005
Location: Oopland
Posts: 36
Rep Power: 0
![]() |
Re: Vectors
Lol, my instructor always used void in her examples she gives us. Guess I picked up the bad habit.
|
|
|
|
|
|
#6 |
|
Programmer
Join Date: Nov 2007
Posts: 86
Rep Power: 1
![]() |
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"));
} |
|
|
|
|
|
#7 |
|
Programmer
Join Date: Sep 2005
Location: Oopland
Posts: 36
Rep Power: 0
![]() |
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) |
|
|
|
|
|
#8 |
|
Programming Guru
![]() Join Date: Jun 2005
Location: Adelaide, South Australia
Posts: 1,192
Rep Power: 5
![]() |
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. |
|
|
|
|
|
#9 |
|
Programmer
Join Date: Sep 2005
Location: Oopland
Posts: 36
Rep Power: 0
![]() |
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;
} |
|
|
|
|
|
#10 |
|
Programmer
Join Date: Sep 2005
Location: Oopland
Posts: 36
Rep Power: 0
![]() |
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})?
|
|
|
|
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|
Similar Threads
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| reading from file, and vectors question | jasonfrost | C++ | 3 | Oct 3rd, 2007 9:02 AM |
| Arrays or Vectors? | can342man | C++ | 2 | Apr 20th, 2006 3:57 PM |
| Vectors and Classes | Writlaus | C++ | 11 | Apr 13th, 2006 5:25 AM |
| Passing vectors as arguments | Soulstorm | C++ | 6 | Mar 18th, 2006 4:49 PM |
| Drawing using vectors | HeX | Java | 12 | May 7th, 2005 10:25 AM |