Copying objects from one vector to another

The program I'm working on is beginning to get the better of me. I'm storing class objects (each one with a string and a double) in a vector, then trying to sort them. I'm using a Merge Sort, and that's where I'm having trouble. Is there a way to copy objects, one at a time, from one vector to another without creating new objects? I'm having a ton of trouble with this, and would appreciate some help. Thanks.
Is there a way to copy objects ... without creating new objects?

Do you mean "move" instead of copy? Because copying things necessarily implies making a copy (i.e. a new object).

So, you're asking to make a sorting algorithm without using copy constructors? I mean, it's possible, but are you actually concerned about possibly premature optimization, or are you just not sure how the merge sort algorithm works?

Sorry for all the questions, but can you be more specific with what the problem is? Do you want vector A to be unsorted, but vector B to have the same data as A, but sorted? Should vector A and B both point to the same underlying memory?
Last edited on
typically classes are put into containers via pointers so if you sort them or do other heavy movement internal to the container, all it really has to move is a pointer (effectively an integer worth of work).

so if you wanted to sort a big vector of fat classes, better to have vector<classtype*> foo than vector<classtype>.

you can also move the pointers just as cleanly.
vector<classtype*> a(maxsize);
vector<classtype*> b(maxsize);
vector<classtype*> sorted(maxsize);


sorted[0] = a[0];
sorted[1] = b[0];
etc...

making your merge sort (assuming a and b are sorted) more or less have the guts that look like this oversimplified code:

int x = 0; int y = 0; int z = 0;

for(blah)
{
if(a[x][0].sortkeyfield < b[y][0].sortkeyfield) //remember to get to the actual data comparison you wanted
sorted[z++] = a[x++];
else
sorted[z++] = b[y++];
}

you need a bit more, to check for when you get to the ends of a or b and clean it all up, but this is the gist of it.

now remember this too:
if you change sorted[something][0].somedata, you ALSO changed perhaps b[37][0].somedata (random example) because the pointer in b and the pointer in sorted are the SAME object in the SAME memory location. Best to destroy a and be (or sorted) once you have what you wanted (you might kill a and b if you wanted the sorted list, and you might kill sorted if you just wanted to print the sorted list but keep the originals intact).
Last edited on
> Is there a way to copy objects ... without creating new objects?

Use std::list and its splice operation if that is an essential requirement.
https://en.cppreference.com/w/cpp/container/list/splice

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
#include <iostream>
#include <list>

struct A {

    A( int v = 0 ) : value(v) { std::cout << value << ". construct\n" ; }
    A( const A& that ) : value(that.value) { std::cout << value << ". copy construct\n" ; }
    A( A&& that ) : value(that.value) { std::cout << value << ". move construct\n" ; }
    A& operator= ( const A& that ) { value = that.value ; std::cout << value << ". copy assign\n" ; return *this ; }
    A& operator= ( A&& that ) { value = that.value ; std::cout << value << ". move assign\n" ; return *this ; }
    ~A() { std::cout << value << ". destroy\n" ; }

    int value ;
    operator int () const { return value ; }
};

int main() {

    std::list<A> lst_one ;
    for( int i = 0 ; i < 10; ++i ) lst_one.emplace_back(i) ;

    std::list<A> lst_two ;

    // transfer objects (with odd values) from lst_one to lst_two
    std::cout << "\ntransferring objects from lst_one to lst_two\n" ;
    for( auto iter = lst_one.begin() ; iter != lst_one.end() ;  )
    {
         if( *iter % 2 == 1 ) lst_two.splice( lst_two.end(), lst_one, iter++ ) ;
         else ++iter ;
    }

    std::cout << "\nlst_one [ " ;
    for( const A& a : lst_one ) std::cout << a << ' ' ;
    std::cout << "]\n" ;

    std::cout << "\nlst_two [ " ;
    for( const A& a : lst_two ) std::cout << a << ' ' ;
    std::cout << "]\n\n" ;
}

http://coliru.stacked-crooked.com/a/99fc4aebc1bce91d
http://rextester.com/IUSNB55679
Wow! Thank you to everyone! I'll digest this information and see what I can learn.

Edit: Granado, I'm trying to use a merge sort, but for some reason when I try to copy the original vector this way:

tempList[index] = actorList[index];

nothing is copied. It either throws a runtime error or continues the program as though the merge sort wasn't there. I'm using a merge sort that seems, to me, to be the standard one would find searching the internet, and yet it won't sort.
Last edited on
Topic archived. No new replies allowed.