in a pickle here with sorting and popping from a vector
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 3 of 3

Thread: in a pickle here with sorting and popping from a vector

  1. #1
    Join Date
    Feb 2010
    Posts
    21

    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] << "  ";
    
    	}
    	
    }

  2. #2
    Join Date
    Oct 2002
    Location
    Austria
    Posts
    1,284

    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 &#37; 0 might throw a division by zero.
    Last edited by ZuK; April 25th, 2010 at 05:58 AM.

  3. #3
    Join Date
    Apr 2008
    Posts
    726

    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 10: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
  •  


Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center