# merge two vectors in C++, help



## Sphinx (Aug 5, 2003)

Hi Im trying to figure out how to merge two vectors (of different sizes most likely) into one vector, while at the same time removing DUPLICATES. Both vectors are sorted in the same manner.

my function basically creates a third vector. So I want to go through and push_back vectors in the same order that the originals are sorted in, while also not push_back any duplicates.

help!


----------



## Shadow2531 (Apr 30, 2001)

You could do it like this (if I got what you're saying correclty. If not, tweak it to your liking.).


```
#include <iostream>
#include <vector>
#include <string>
#include <algorithm>
#include <iterator>

using namespace std;

template <class T>
T merge_no_dups(const T& v1, const T& v2) {
    T ret(v1);
    for (typename T::const_iterator i = v2.begin(); i != v2.end(); ++i) {
        bool notfound = true;
        for (typename T::const_iterator z = ret.begin(); z != ret.end(); ++z) {
            if (*z == *i) {
                notfound = false;
                break;
            }
        }
        if (notfound) {
            ret.push_back(*i);
        }
    }
    return ret;
}


int main() {
    // example data
    const int a[5] = {4, 3, 7, 9, 16};
    const int b[6] = {5, 17, 2, 33, 9, 3};
    const vector<int> one(a, a + sizeof(a) / sizeof(a[0]));
    const vector<int> two(b, b + sizeof(b) / sizeof(b[0]));
    
    // merge 2 vectors, don't add dups, copy while preserving order
    vector<int> x = merge_no_dups(one, two);
    
    // Print
    copy(x.begin(), x.end(), ostream_iterator<int>(cout, "\n"));
    
    // example data
    const string c[5] = {"4", "3", "7", "9", "16"};
    const string d[6] = {"5", "17", "2", "33", "9", "3"};
    const vector<string> three(c, c + sizeof(c) / sizeof(c[0]));
    const vector<string> four(d, d + sizeof(d) / sizeof(d[0]));

    // merge 2 vectors, don't add dups, copy while preserving order
    vector<string> y = merge_no_dups(three, four);
    
    // Print
    cout << "\n";
    copy(y.begin(), y.end(), ostream_iterator<string>(cout, "\n"));

}
```
(It assumes the first vector doesn't have any dups in it though. You could change the function to handle that or do vector<string> y = merge_no_dups(merge_no_dups(vector<string>(), three), four); to handle it with the current version.)

Or, you might want to check out the algorithm functions to see what you can do.


----------



## Shadow2531 (Apr 30, 2001)

Here's about the same thing, just set up differently.


```
#include <iostream>
#include <vector>
#include <algorithm>
#include <iterator>
using namespace std;

template<class T>
T merge_copy(const T& a, const T& b) {
    T ret(a);
    copy(b.begin(), b.end(), back_inserter(ret));
    return ret;
}

template<class T>
T nuke_dupes_no_sort_copy(const T& c) {
    T ret;
    for (typename T::const_iterator i = c.begin(); i != c.end(); ++i) {
        if (find(ret.begin(), ret.end(), *i) == ret.end()) {
            ret.push_back(*i);
        }
    }
    return ret;
}

int main() {
    const int a[5] = {4, 3, 7, 9, 16};
    const int b[6] = {5, 17, 2, 33, 9, 3};
    const vector<int> one(a, a + sizeof(a) / sizeof(a[0]));
    const vector<int> two(b, b + sizeof(b) / sizeof(b[0]));
    
    const vector<int> x = nuke_dupes_no_sort_copy(merge_copy(one, two));
    
    copy(x.begin(), x.end(), ostream_iterator<int>(cout, "\n"));
    
    const string c[5] = {"4", "3", "7", "9", "16"};
    const string d[6] = {"5", "17", "2", "33", "9", "3"};
    const vector<string> three(c, c + sizeof(c) / sizeof(c[0]));
    const vector<string> four(d, d + sizeof(d) / sizeof(d[0]));

    const vector<string> y = nuke_dupes_no_sort_copy(merge_copy(three, four));
    
    cout << "\n";
    copy(y.begin(), y.end(), ostream_iterator<string>(cout, "\n"));
}
```


----------

