-
April 25th, 2010, 12:04 AM
#1
in a pickle here with sorting and popping from a vector
I am having problems with a series of sorting and deleting. The program is supposed to sort the names in a vector in a way that the "goose" is deleted. The "goose" is input by the user as a number (which represents that names position in the vector). The "goose" is moved to the end of the vector and then popped off. After the goose is popped off, the program will start counting from where it left off to find the next goose and delete it until there is only one name left.
Example output would look something like this:
Position of the goose: 3
Bill Sue Al May Gin Tim
Bill Sue May Gin Tim
Bill Sue May Gin
Bill Sue Gin
Bill Gin
Bill
Here is my problem, I can get the first "goose" to pop off, but I am having a hard time writing a code that will find the next goose, move it to the end, and then delete it successfully. The order of the names cannot change besides the deletion of the goose every time.
My wackGoose function is where the goose is sorted to the end and deleted, and where most of my thinkin has been taking place, I've wrote a lot of random code into in the past half hour just hoping something would word.
Code:
void getNames(vector<string>&names);
//fills the vector with names
void getGoose(int&goose);
//gets the gooseth entry from the user
int gooseIndex(const vector<string>&names, int goose);
//returns the index of the location of the goose
void wackGoose(vector<string>&names, int gooseLoc);
//moves the goose to the last spot in the vector and removes it
template <class myType>
void interchange(myType&a, myType&b);
//interchanges two names
int main()
{
introduction();
cout << endl << endl;
vector<string> names;
int goose, gooseLoc;
getNames(names);
getGoose(goose);
gooseLoc = gooseIndex(names, goose);
while(names.size()>1)
{
cout << endl;
wackGoose(names, gooseLoc);
cout << endl;
gooseLoc = gooseLoc+(goose-1);
}
cin.ignore();
cin.get();
return 0;
}
//DEFINITIONS SECTION
void getNames(vector<string>&names)
//fills the vector with names
{
string name;
ifstream fin;
fin.open("C:/Users/Josh/Desktop/goosey2.txt");
while(fin >> name)
{
names.push_back(name);
cout << names.back() << " ";
}
cout << endl << endl;
}
void getGoose(int&goose)
//gets the gooseth entry from the user
{
do{
cout << "Goose position must be greater than zero.\n";
cout << "Enter the position of the GOOSE: ";
cin >> goose;
}while(goose<1);
}
int gooseIndex(const vector<string>&names, int goose)
//returns the index of the location of the goose
{
int index;
if(goose<names.size())
{
index = goose-1;
return index;
}
else
{
index = goose%names.size();
index--;
}
return index;
}
void wackGoose(vector<string>&names, int gooseLoc)
//moves the goose to the last spot in the vector and removes it
{
int index = gooseLoc;
if(index==names.size()-1)
{
names.pop_back();
cout << endl;
for(int count = 0; count<names.size(); ++count)
cout << names[count] << " ";
}
else
{
if(index>names.size()-1)
index=index-(names.size()-1);
while(index<names.size()-1)
{
interchange(names[index], names[index+1]);
index++;
}
names.pop_back();
cout << endl;
for(int count = 0; count<names.size(); ++count)
cout << names[count] << " ";
}
}
-
April 25th, 2010, 04:49 AM
#2
Re: in a pickle here with sorting and popping from a vector
look at the gooseIndex() function.
What will it return if you have only 1 goose left or goose is a multiple of names.size() ?
Kurt
EDIT: I haven't tried but x % 0 might throw a division by zero.
Last edited by ZuK; April 25th, 2010 at 04:58 AM.
-
April 25th, 2010, 09:29 AM
#3
Re: in a pickle here with sorting and popping from a vector
here's how I'd do it. No need to sort yourself - just let vector.erase(...) do it.
Code:
#include <vector>
#include <string>
#include <iostream>
using namespace std;
template< class T>
void GetNext( vector<T> & vec, typename vector<T>::iterator& position, int step )
{
for( int i = 0; i < step; ++i )
{
++position; // should be a bit more careful here about receiving .end(), but meh.
if( position == vec.end() )
position = vec.begin();
}
}
template< class T>
void DeleteEntry( vector<T> & vec, typename vector<T>::iterator& position )
{
position = vec.erase(position);
if( position == vec.end() )
position = vec.begin();
}
template<class T>
void PrintVec( vector<T> const & vec)
{
cout << endl;
vector<T>::const_iterator it = vec.begin();
vector<T>::const_iterator end = vec.end();
for(; it != end; ++it)
{
cout << *it << endl;
}
cout << endl;
}
int main()
{
vector<string> names;
names.push_back("bill");
names.push_back("sue");
names.push_back("al");
names.push_back("may");
names.push_back("gin");
names.push_back("tim");
int goose = 3;
vector<string>::iterator position = names.begin();
while( names.size() )
{
PrintVec( names );
GetNext( names, position, goose-1 );
DeleteEntry( names, position );
}
cin.get();
}
Last edited by Amleto; April 25th, 2010 at 09:35 AM.
Tags for this Thread
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|