Thread: pointers and vectors

  1. #1
    Master of Puppets rwmarsh's Avatar
    Join Date
    Feb 2006
    Location
    Texas
    Posts
    96

    Question pointers and vectors

    I have a program that takes customer information ( such as name, address, city and state ect....) from a disk file and loads it into a vector. Each element of the vector is a struct like this.
    Code:
    struct mystruct
    {
        string name;
        string address;
        string city;
        string state;
        string zip;
        string phone;
    };
    mystruct customers;
    The elements that are saved on disk are in no particular order but I want to be able to sort the vector in alphabetical order to display to the screen. I was thinking about creating a header file with a class called sort to do the dirty work. Can I pass a pointer to the entire vector to this sort class and return a pointer to the new sorted vector back to the program? If so, is the pointer syntax the same and how do I dereference it to replace the old vector with the new sorted vector?
    I know there are functions such as sort that can do this for me but I want to a sort by city, then sort those by name. Plus, I just want to see if I can do it
    Last edited by rwmarsh; 04-01-2006 at 10:25 AM.
    Using DEV-C++ Under Windows XP
    +------------------------------+

    "No! Do, or Do Not. There is no Try..."

  2. #2
    Master of Puppets rwmarsh's Avatar
    Join Date
    Feb 2006
    Location
    Texas
    Posts
    96
    Here is a simple program I made up real quick. This is as far as I have got.
    Code:
    #include <iostream>
    #include <vector>
    
    using namespace std;
    
    struct mytype
    {
        int one;
        int two;
        int three;
    };
    
    void SortMyVec(vector<mytype>::iterator iter1, vector<mytype>::iterator iter2);
    
    int main()
    {
        mytype numbers;
        
        vector<mytype> myvec;
        vector<mytype>::iterator iter1;
        vector<mytype>::iterator iter2;
    
        numbers.one=5; numbers.two=6; numbers.three=7;
          myvec.push_back(numbers);
        numbers.one=9; numbers.two=0; numbers.three=1;    
          myvec.push_back(numbers);
        numbers.one=1; numbers.two=2; numbers.three=3;
          myvec.push_back(numbers);
        numbers.one=5; numbers.two=3; numbers.three=4;
          myvec.push_back(numbers);
        numbers.one=8; numbers.two=9; numbers.three=0;
          myvec.push_back(numbers);
        
        for (int a=0; a<myvec.size(); a++)
        {
            numbers=myvec.at(a);
            cout<<numbers.one<<" "<<numbers.two<<" "<<numbers.three<<endl;
        }
        cout<<endl;
        
        iter1=myvec.begin();
        iter2=myvec.end();
    
        SortMyVec(iter1, iter2);
    
        for (int a=0; a<myvec.size(); a++)
        {
            numbers=myvec.at(a);
            cout<<numbers.one<<" "<<numbers.two<<" "<<numbers.three<<endl;
        }
        cout<<endl;
    
        cin.get();
        return 0;
    }
    
    void SortMyVec(vector<int>::iterator iter1, vector<int>::iterator iter2)
    {
        sort(iter1, iter2);
    
        return;
    }
    What I want to do is sort the vector by the first integer and then by the second integer so my output is like this
    Code:
    1 2 3
    5 3 4 
    5 6 7
    8 9 0 
    9 0 1
    It works fine if I use just a single integer for each element of the vector instead of the struct. But when I use the struct I get
    Code:
      [Linker error] undefined reference to `SortMyVec(__gnu_cxx::__normal_iterator<mytype*, std::vector<mytype, std::allocator<mytype> > >, __gnu_cxx::__normal_iterator<mytype*, std::vector<mytype, std::allocator<mytype> > >)'
    I am not sure where to go from here. Anyone got ay ideas?
    Using DEV-C++ Under Windows XP
    +------------------------------+

    "No! Do, or Do Not. There is no Try..."

  3. #3
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    All you need is to create a sorting function for your struct:
    Code:
    bool mystructLess(const mystruct& lhs, const mystruct& rhs)
    {
      // return true if lhs < rhs, return false if lhs >= rhs.
    }
    Then just pass this to the sort function:
    Code:
    std::sort(myvec.begin(), myvec.end(), myStructLess);
    The only hard part is filling in the code for the mystructLess function. If you only want to sort by name it would be easy, but it gets more complicated if you want to sort by multiple members of the struct.

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    I want to a sort by city, then sort those by name
    Define say:
    Code:
    bool cmp_mystruct(const mystruct& a, const mystruct& b) {
    	if (a.city == b.city) {
    		return a.name < b.name;
    	} else {
    		return a.city < b.city;
    	}
    }
    Then use std::sort()
    Code:
    std::sort(myvec.begin(), myvec.end(), cmp_mystruct);
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  5. #5
    Registered User
    Join Date
    Apr 2003
    Posts
    2,663
    1) The parameter types in your function declaration before main() do not match the parameter types in the function definition after main().

    2) Instead of this type of loop:
    Code:
    for (int a=0; a<myvec.size(); a++)
    {
            numbers=myvec.at(a);
            cout<<numbers.one<<" "<<numbers.two<<" "<<numbers.three<<endl;
    }
    use iterators:
    Code:
    for(iter = myvec.begin(); iter != myvec.end(); ++iter)
    {
    
    
    }

  6. #6
    Master of Puppets rwmarsh's Avatar
    Join Date
    Feb 2006
    Location
    Texas
    Posts
    96
    OK, That works great! But something still seems strange to me. Here's what I got
    Code:
    #include <iostream>
    #include <vector>
    
    using namespace std;
    
    struct mytype
    {
        int one;
        int two;
        int three;
    };
    
    bool SortMyStruct(const mytype& a, const mytype& b);
    
    int main()
    {
        mytype numbers;
        
        vector<mytype> myvec;
        vector<mytype>::iterator iter;
    
        numbers.one=5; numbers.two=6; numbers.three=7;
          myvec.push_back(numbers);
        numbers.one=9; numbers.two=0; numbers.three=1;    
          myvec.push_back(numbers);
        numbers.one=1; numbers.two=2; numbers.three=3;
          myvec.push_back(numbers);
        numbers.one=5; numbers.two=3; numbers.three=4;
          myvec.push_back(numbers);
        numbers.one=8; numbers.two=9; numbers.three=0;
          myvec.push_back(numbers);
        numbers.one=5; numbers.two=3; numbers.three=5;
          myvec.push_back(numbers);
        
        for (iter=myvec.begin(); iter!=myvec.end(); ++iter)
        {
            numbers=*iter;
            cout<<numbers.one<<" "<<numbers.two<<" "<<numbers.three<<endl;
        }
        cout<<endl;
        
        sort(myvec.begin(), myvec.end(), SortMyStruct);
    
        for (iter=myvec.begin(); iter!=myvec.end(); ++iter)
        {
            numbers=*iter;
            cout<<numbers.one<<" "<<numbers.two<<" "<<numbers.three<<endl;
        }
        cout<<endl;
    
        cin.get();
        return 0;
    }
    
    bool SortMyStruct(const mytype& a, const mytype& b) 
    {
        if (a.one == b.one) 
        {
            if (a.two == b.two)
                return a.three < b.three;
            else
                return a.two < b.two;   
        }
        else 
            return a.one < b.one;
    }
    Is there any way I can tighten this up any?
    Maybe it is all the different return statements in the SortMyStruct function, I'm used to having only one return in any given function.I realize they have to be there, it just does not seem right.

    BTW, Thanks again for the help!
    Using DEV-C++ Under Windows XP
    +------------------------------+

    "No! Do, or Do Not. There is no Try..."

  7. #7
    Registered User
    Join Date
    Apr 2003
    Posts
    2,663
    Compare this:
    Code:
    for (iter=myvec.begin(); iter!=myvec.end(); ++iter)
    {
    	numbers=*iter;
    	cout<<numbers.one<<" "<<numbers.two<<" "<<numbers.three<<endl;
    }
    to this:
    Code:
    for (iter=myvec.begin(); iter!=myvec.end(); ++iter)
    {
    	cout<<iter->one<<" "<<iter->two<<" "<<iter->three<<endl;
    }
    Maybe it is all the different return statements in the SortMyStruct function, I'm used to having only one return in any given function.I realize they have to be there, it just does not seem right.
    You don't have to have more than one return statement:
    Code:
    bool SortMyStruct(const mytype& a, const mytype& b) 
    {
        bool result;
    
        if (a.one == b.one) 
        {
            if (a.two == b.two)
               result =  a.three < b.three;
            else
                result = a.two < b.two;   
        }
        else 
        {
            result =  a.one < b.one;
        }
    	
        return result;
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. pointers and vectors
    By larne in forum C Programming
    Replies: 6
    Last Post: 09-12-2008, 10:27 AM
  2. Vectors using object pointers.
    By theJ89 in forum C++ Programming
    Replies: 2
    Last Post: 04-09-2006, 10:29 PM
  3. Problem with Vectors And Pointers
    By Shamino in forum Game Programming
    Replies: 3
    Last Post: 01-21-2006, 07:23 PM
  4. Vectors of pointers and function templates
    By 7words in forum C++ Programming
    Replies: 3
    Last Post: 05-02-2004, 11:39 AM
  5. Vectors of pointers to objects
    By Myownworstenemy in forum C++ Programming
    Replies: 3
    Last Post: 09-01-2003, 11:23 PM