CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 7 of 7

Thread: Swapping Vector

  1. #1
    Join Date
    Feb 2003
    Posts
    28

    Swapping Vector

    I have a vector of objects, the type is not important

    At some point, I have an object down the list that I would like to move farther up the list (an example follows)

    What is the best way to do this?

    Example:

    1,2,3,4,5,6,7,8,9

    ...then...

    1,2,3,7,4,5,6,8,9

    Notice how seven is promoted to vector[3]? What would be the best way to do this?

    Thanks

  2. #2
    Join Date
    Dec 2001
    Location
    Ontario, Canada
    Posts
    2,236
    did you try the swap function?

  3. #3
    Join Date
    Feb 2003
    Posts
    28
    I have my list....moduleList

    tried moduleList.swap(4,8)

    where 4 & 8 are positions

    returned an error.

  4. #4
    Join Date
    Aug 2000
    Location
    West Virginia
    Posts
    7,725
    I'm don't think there is a good way to do this with vectors.
    Basically, you are erasing and element and inserting a new
    element. These operations are inefficient for vectors, except
    at the end. There are two methods I can think of do this.
    One would be to do an actual erase/insert. Another would be to
    use rotate(). I think that rotate() might be slightly more
    efficient, but I am not sure.

    [ I noticed in your last post that you mentioned a list,
    instead of vector. Which is it ? ]

    Code:
    #include <vector>
    #include <algorithm>
    #include <iostream>
    
    using namespace std;
    
    
    int main()
    {
        int i;
    
        // method 1 : using erase/insert
    
        vector<int> v1;
    
        for (i=1; i<10; ++i) v1.push_back(i);
    
        copy (v1.begin(),v1.end(),ostream_iterator<int>(cout," "));
        cout << endl;
    
        int tmp = v1[6];
        v1.erase(v1.begin()+6);
        v1.insert(v1.begin()+3,tmp);
    
        copy (v1.begin(),v1.end(),ostream_iterator<int>(cout," "));
        cout << endl << endl;
    
        // method2 : using rotate
    
        vector<int> v2;
    
        for (i=1; i<10; ++i) v2.push_back(i);
    
        copy (v2.begin(),v2.end(),ostream_iterator<int>(cout," "));
        cout << endl;
        rotate(v2.begin()+3,v2.begin()+6,v2.begin()+7);
    
        copy (v2.begin(),v2.end(),ostream_iterator<int>(cout," "));
        cout << endl;
    
        return 0;
    }

  5. #5
    Join Date
    Aug 2000
    Location
    West Virginia
    Posts
    7,725
    FYI, if you have a std::list instead, I think the splice()
    member function is best:

    Code:
    #include <list>
    #include <algorithm>
    #include <iostream>
    #include <iterator>
    
    using namespace std;
    
    
    int main()
    {
        int i;
    
        // method : std::list::splice() member function
    
        list<int> v1;
    
        for (i=1; i<10; ++i) v1.push_back(i);
    
        copy (v1.begin(),v1.end(),ostream_iterator<int>(cout," "));
        cout << endl;
    
        list<int>::iterator i1 = v1.begin();
        advance(i1,3);
    
        list<int>::iterator i2 = v1.begin();
        advance(i2,6);
    
        v1.splice(i1,v1,i2);
    
        copy (v1.begin(),v1.end(),ostream_iterator<int>(cout," "));
        cout << endl << endl;
    
    
        return 0;
    }

  6. #6
    Join Date
    Apr 1999
    Posts
    27,449
    Originally posted by bbrunno
    I have my list....moduleList

    tried moduleList.swap(4,8)

    where 4 & 8 are positions

    returned an error.
    There is no function that is a member of std::list<T> called swap() that takes two integers.

    The only swap function that is also a member of list<T> swaps two lists. Also, a majority of the STL functions do not use "position numbers", but instead iterators are used. If you are calling an STL function and you are refering to your items by "position number" instead of iterators, look up the function in your favorite STL reference and make sure that what you are doing is correct.

    The std::swap function and iter_swap are the ones that you are looking for if your goal is to swap two values.

    Regards,

    Paul McKenzie

  7. #7
    Join Date
    Jun 2002
    Location
    Letchworth, UK
    Posts
    1,020
    Try

    Code:
    #include <algorithm>
    ...
    swap(moduleList[4], moduleList[8]);
    Succinct is verbose for terse

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured