Answered by:
How to insert two or more vector values into another vector without duplicates

Question
-
Hi Friends,
I have two vectors (values of maps)
map1: (it1)
1=>1 3 5 7
map2: (it2)
3=>1 2 5 8
How to insert these two vectors (I mean the vectors are it1->second and it2->second) into another vector (vector<int>vect) without any duplicate values, That is vect contains 1 3 5 7 2 8?
Wednesday, September 10, 2014 7:41 AM
Answers
-
Hi Friends,
I have two vectors (values of maps)
map1: (it1)
1=>1 3 5 7
map2: (it2)
3=>1 2 5 8
How to insert these two vectors (I mean the vectors are it1->second and it2->second) into another vector (vector<int>vect) without any duplicate values, That is vect contains 1 3 5 7 2 8?
A simple (but not very efficient) way to do this is to use functions like
void add_distinct(int val, vector<int>& dest) { if (find(dest.begin(), dest.end(), val) == dest.end()) { dest.push_back(val); } } void add_distinct(const vector<int>& source, vector<int>& dest) { for (size_t i = 0; i < source.size(); i++) { add_distinct(source[i], dest); } }
Then just insert each original vector to the destinationvector<int> dest; add_distinct(it1->second, dest); add_distinct(it2->second, dest);
David Wilkinson | Visual C++ MVP
- Marked as answer by Shel88 Saturday, November 15, 2014 5:59 PM
Wednesday, September 10, 2014 9:14 AM -
A simple (but not very efficient) way to do this is to use functions like
void add_distinct(int val, vector<int>& dest) { if (find(dest.begin(), dest.end(), val) == dest.end()) { dest.push_back(val); } } void add_distinct(const vector<int>& source, vector<int>& dest) { for (size_t i = 0; i < source.size(); i++) { add_distinct(source[i], dest); } }
Then just insert each original vector to the destinationvector<int> dest; add_distinct(it1->second, dest); add_distinct(it2->second, dest);
What is the use of defining two same functions?
Come on Shel88, can you make no effort? The functions are not the same. The first inserts a single value, and the second inserts a vector of values (by calling the first in a loop).
Have you not heard of overloaded functions in C++? If not, you should stop bothering us and go back and start studying C++ from the beginning.
I cannot believe that with any effort at all you could not have understood what the posted code does.
David Wilkinson | Visual C++ MVP
Wednesday, September 10, 2014 11:23 AM -
If vectors are sorted, then consider set_union. An example:
vector<int> v1{ 1, 3, 5, 7 }; vector<int> v2{ 1, 2, 5, 8 }; vector<int> vect; set_union( v1.cbegin(), v1.cend(), v2.cbegin(), v2.cend(), back_inserter( vect ) );
The result will be sorted too.
To adjust, use it1->second instead of v1 etc.
Well,
If two vectors which could be produced using for() loop.
There is another case we cannot define v2 and it depends on the for() loop results.
Then also consider an intermediate set:
vector<int> v1{ 1, 7, 5, 3 }; vector<int> v2{ 8, 1, 2, 5 }; set<int> t; // temporary set // example: insert values from a vector t.insert( v1.cbegin(), v1.cend() ); // example: insert values by some loop for( auto i = 0; i < v2.size(); ++i ) { t.insert( v2[i] ); } // build the final vector (if needed instead of set) vector<int> vect( t.cbegin(), t.cend() ); t.clear();
- Edited by Viorel_MVP Wednesday, September 10, 2014 12:07 PM
- Proposed as answer by chong kyong kim Friday, September 12, 2014 10:49 AM
- Marked as answer by May Wang - MSFT Thursday, September 18, 2014 1:58 AM
Wednesday, September 10, 2014 12:05 PM -
If vectors are sorted, then consider set_union. An example:
vector<int> v1{ 1, 3, 5, 7 }; vector<int> v2{ 1, 2, 5, 8 }; vector<int> vect; set_union( v1.cbegin(), v1.cend(), v2.cbegin(), v2.cend(), back_inserter( vect ) );
The result will be sorted too.
To adjust, use it1->second instead of v1 etc.
- Marked as answer by Shel88 Saturday, November 15, 2014 5:59 PM
Wednesday, September 10, 2014 7:58 AM
All replies
-
If vectors are sorted, then consider set_union. An example:
vector<int> v1{ 1, 3, 5, 7 }; vector<int> v2{ 1, 2, 5, 8 }; vector<int> vect; set_union( v1.cbegin(), v1.cend(), v2.cbegin(), v2.cend(), back_inserter( vect ) );
The result will be sorted too.
To adjust, use it1->second instead of v1 etc.
- Marked as answer by Shel88 Saturday, November 15, 2014 5:59 PM
Wednesday, September 10, 2014 7:58 AM -
If vectors are sorted, then consider set_union. An example:
vector<int> v1{ 1, 3, 5, 7 }; vector<int> v2{ 1, 2, 5, 8 }; vector<int> vect; set_union( v1.cbegin(), v1.cend(), v2.cbegin(), v2.cend(), back_inserter( vect ) );
The result will be sorted too.
To adjust, use it1->second instead of v1 etc.
Well,
If two vectors which could be produced using for() loop.
There is another case we cannot define v2 and it depends on the for() loop results.
Wednesday, September 10, 2014 8:32 AM -
Hi Friends,
I have two vectors (values of maps)
map1: (it1)
1=>1 3 5 7
map2: (it2)
3=>1 2 5 8
How to insert these two vectors (I mean the vectors are it1->second and it2->second) into another vector (vector<int>vect) without any duplicate values, That is vect contains 1 3 5 7 2 8?
A simple (but not very efficient) way to do this is to use functions like
void add_distinct(int val, vector<int>& dest) { if (find(dest.begin(), dest.end(), val) == dest.end()) { dest.push_back(val); } } void add_distinct(const vector<int>& source, vector<int>& dest) { for (size_t i = 0; i < source.size(); i++) { add_distinct(source[i], dest); } }
Then just insert each original vector to the destinationvector<int> dest; add_distinct(it1->second, dest); add_distinct(it2->second, dest);
David Wilkinson | Visual C++ MVP
- Marked as answer by Shel88 Saturday, November 15, 2014 5:59 PM
Wednesday, September 10, 2014 9:14 AM -
Hi Friends,
I have two vectors (values of maps)
map1: (it1)
1=>1 3 5 7
map2: (it2)
3=>1 2 5 8
How to insert these two vectors (I mean the vectors are it1->second and it2->second) into another vector (vector<int>vect) without any duplicate values, That is vect contains 1 3 5 7 2 8?
A simple (but not very efficient) way to do this is to use functions like
void add_distinct(int val, vector<int>& dest) { if (find(dest.begin(), dest.end(), val) == dest.end()) { dest.push_back(val); } } void add_distinct(const vector<int>& source, vector<int>& dest) { for (size_t i = 0; i < source.size(); i++) { add_distinct(source[i], dest); } }
Then just insert each original vector to the destinationvector<int> dest; add_distinct(it1->second, dest); add_distinct(it2->second, dest);
Wednesday, September 10, 2014 10:44 AM -
A simple (but not very efficient) way to do this is to use functions like
void add_distinct(int val, vector<int>& dest) { if (find(dest.begin(), dest.end(), val) == dest.end()) { dest.push_back(val); } } void add_distinct(const vector<int>& source, vector<int>& dest) { for (size_t i = 0; i < source.size(); i++) { add_distinct(source[i], dest); } }
Then just insert each original vector to the destinationvector<int> dest; add_distinct(it1->second, dest); add_distinct(it2->second, dest);
What is the use of defining two same functions?
Come on Shel88, can you make no effort? The functions are not the same. The first inserts a single value, and the second inserts a vector of values (by calling the first in a loop).
Have you not heard of overloaded functions in C++? If not, you should stop bothering us and go back and start studying C++ from the beginning.
I cannot believe that with any effort at all you could not have understood what the posted code does.
David Wilkinson | Visual C++ MVP
Wednesday, September 10, 2014 11:23 AM -
If vectors are sorted, then consider set_union. An example:
vector<int> v1{ 1, 3, 5, 7 }; vector<int> v2{ 1, 2, 5, 8 }; vector<int> vect; set_union( v1.cbegin(), v1.cend(), v2.cbegin(), v2.cend(), back_inserter( vect ) );
The result will be sorted too.
To adjust, use it1->second instead of v1 etc.
Well,
If two vectors which could be produced using for() loop.
There is another case we cannot define v2 and it depends on the for() loop results.
Then also consider an intermediate set:
vector<int> v1{ 1, 7, 5, 3 }; vector<int> v2{ 8, 1, 2, 5 }; set<int> t; // temporary set // example: insert values from a vector t.insert( v1.cbegin(), v1.cend() ); // example: insert values by some loop for( auto i = 0; i < v2.size(); ++i ) { t.insert( v2[i] ); } // build the final vector (if needed instead of set) vector<int> vect( t.cbegin(), t.cend() ); t.clear();
- Edited by Viorel_MVP Wednesday, September 10, 2014 12:07 PM
- Proposed as answer by chong kyong kim Friday, September 12, 2014 10:49 AM
- Marked as answer by May Wang - MSFT Thursday, September 18, 2014 1:58 AM
Wednesday, September 10, 2014 12:05 PM