Intersection of two sorted vectors in C++A pointer vector sorted by its member functionShould I call a method (i.e. size()) multiple times or store it (if I know the value will not change)Is this implementation of Quicksort good?Computing intersection of 2D infinite linesMerge two already sorted linked listPlace integers into a vector, sum each adjacent pair, refill vector with only the sums of each pair i.e remove all the original data from the vectorMerge two sorted lists of numbersFinding the lowest missing integer in a vector containing negative and positive valuesDemonstration of Scale BalancingType-safe Euclidean vectors in C++
Do I have a twin with permutated remainders?
Emailing HOD to enhance faculty application
Is it unprofessional to ask if a job posting on GlassDoor is real?
What's the difference between 'rename' and 'mv'?
Can a rocket refuel on Mars from water?
What is the most common color to indicate the input-field is disabled?
How do conventional missiles fly?
1960's book about a plague that kills all white people
Watching something be written to a file live with tail
Is it possible to run Internet Explorer on OS X El Capitan?
I'm flying to France today and my passport expires in less than 2 months
If human space travel is limited by the G force vulnerability, is there a way to counter G forces?
Western buddy movie with a supernatural twist where a woman turns into an eagle at the end
What is the intuition behind short exact sequences of groups; in particular, what is the intuition behind group extensions?
How to say in German "enjoying home comforts"
How could indestructible materials be used in power generation?
Is it inappropriate for a student to attend their mentor's dissertation defense?
Can one be a co-translator of a book, if he does not know the language that the book is translated into?
A reference to a well-known characterization of scattered compact spaces
Is it legal for company to use my work email to pretend I still work there?
How to model explosives?
If a Gelatinous Cube takes up the entire space of a Pit Trap, what happens when a creature falls into the trap but succeeds on the saving throw?
What is the word for reserving something for yourself before others do?
How can saying a song's name be a copyright violation?
Intersection of two sorted vectors in C++
A pointer vector sorted by its member functionShould I call a method (i.e. size()) multiple times or store it (if I know the value will not change)Is this implementation of Quicksort good?Computing intersection of 2D infinite linesMerge two already sorted linked listPlace integers into a vector, sum each adjacent pair, refill vector with only the sums of each pair i.e remove all the original data from the vectorMerge two sorted lists of numbersFinding the lowest missing integer in a vector containing negative and positive valuesDemonstration of Scale BalancingType-safe Euclidean vectors in C++
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty margin-bottom:0;
$begingroup$
Intersection of two sorted vectors in C++ - can this be written any better?
vector<int> intersection(vector<int>& nums1, vector<int>& nums2)
vector<int> result;
int l = 0, r = 0;
while(l < nums1.size() && r < nums2.size())
int left = nums1[l], right = nums2[r];
if(left == right)
result.push_back(right);
while(l < nums1.size() && nums1[l] == left )l++;
while(r < nums2.size() && nums2[r] == right )r++;
continue;
if(left < right)
while(l < nums1.size() && nums1[l] == left )l++;
else while( r < nums2.size() && nums2[r] == right )r++;
return result;
c++ reinventing-the-wheel
$endgroup$
add a comment |
$begingroup$
Intersection of two sorted vectors in C++ - can this be written any better?
vector<int> intersection(vector<int>& nums1, vector<int>& nums2)
vector<int> result;
int l = 0, r = 0;
while(l < nums1.size() && r < nums2.size())
int left = nums1[l], right = nums2[r];
if(left == right)
result.push_back(right);
while(l < nums1.size() && nums1[l] == left )l++;
while(r < nums2.size() && nums2[r] == right )r++;
continue;
if(left < right)
while(l < nums1.size() && nums1[l] == left )l++;
else while( r < nums2.size() && nums2[r] == right )r++;
return result;
c++ reinventing-the-wheel
$endgroup$
4
$begingroup$
Do you know aboutstd::set_intersection()
? Reference and example implementations: en.cppreference.com/w/cpp/algorithm/set_intersection
$endgroup$
– user673679
12 hours ago
2
$begingroup$
@user673679 yes I did, and didn't want to use it;
$endgroup$
– Rick
12 hours ago
add a comment |
$begingroup$
Intersection of two sorted vectors in C++ - can this be written any better?
vector<int> intersection(vector<int>& nums1, vector<int>& nums2)
vector<int> result;
int l = 0, r = 0;
while(l < nums1.size() && r < nums2.size())
int left = nums1[l], right = nums2[r];
if(left == right)
result.push_back(right);
while(l < nums1.size() && nums1[l] == left )l++;
while(r < nums2.size() && nums2[r] == right )r++;
continue;
if(left < right)
while(l < nums1.size() && nums1[l] == left )l++;
else while( r < nums2.size() && nums2[r] == right )r++;
return result;
c++ reinventing-the-wheel
$endgroup$
Intersection of two sorted vectors in C++ - can this be written any better?
vector<int> intersection(vector<int>& nums1, vector<int>& nums2)
vector<int> result;
int l = 0, r = 0;
while(l < nums1.size() && r < nums2.size())
int left = nums1[l], right = nums2[r];
if(left == right)
result.push_back(right);
while(l < nums1.size() && nums1[l] == left )l++;
while(r < nums2.size() && nums2[r] == right )r++;
continue;
if(left < right)
while(l < nums1.size() && nums1[l] == left )l++;
else while( r < nums2.size() && nums2[r] == right )r++;
return result;
c++ reinventing-the-wheel
c++ reinventing-the-wheel
edited 1 hour ago
Peter Mortensen
25417
25417
asked 14 hours ago
RickRick
308112
308112
4
$begingroup$
Do you know aboutstd::set_intersection()
? Reference and example implementations: en.cppreference.com/w/cpp/algorithm/set_intersection
$endgroup$
– user673679
12 hours ago
2
$begingroup$
@user673679 yes I did, and didn't want to use it;
$endgroup$
– Rick
12 hours ago
add a comment |
4
$begingroup$
Do you know aboutstd::set_intersection()
? Reference and example implementations: en.cppreference.com/w/cpp/algorithm/set_intersection
$endgroup$
– user673679
12 hours ago
2
$begingroup$
@user673679 yes I did, and didn't want to use it;
$endgroup$
– Rick
12 hours ago
4
4
$begingroup$
Do you know about
std::set_intersection()
? Reference and example implementations: en.cppreference.com/w/cpp/algorithm/set_intersection$endgroup$
– user673679
12 hours ago
$begingroup$
Do you know about
std::set_intersection()
? Reference and example implementations: en.cppreference.com/w/cpp/algorithm/set_intersection$endgroup$
– user673679
12 hours ago
2
2
$begingroup$
@user673679 yes I did, and didn't want to use it;
$endgroup$
– Rick
12 hours ago
$begingroup$
@user673679 yes I did, and didn't want to use it;
$endgroup$
– Rick
12 hours ago
add a comment |
2 Answers
2
active
oldest
votes
$begingroup$
Indentation
Your indentation is not consistent. This makes the code hard to read and maintain. It should be fixed so you don't give other people headaches.
if(left < right)
while(l < nums1.size() && nums1[l] == left )l++;
else while( r < nums2.size() && nums2[r] == right )r++;That is basically unreadable giberish (opinion of Martin).
Using namespace
std;
is super badThis is mention in nearly every C++ review. There is a large article on the subject here: Why is “using namespace std” considered bad practice?. The second answer is the best in my opinion (Martin) see
Multiple declarations in one is bad (thanks to terrible syntax binding rules)
The one declaration per line has been written about adnausium in best practice guides. Please for the sake of your reader declare one variable per line with its own exact type.
The syntax binding rules alluded to above is:
int* x, y; // Here x is int* and y in int
// confusing to a reader. Did you really mean to make y an int?
// Avoid this problem be declaring one variable per lineTypically, functions like this would be based on iterators to work on any container
Here your code is limited to only using vectors. But the algorithm you are using could be used by any container type with only small modifications. As a result your function could provide much more utility being written to use iterators.
The standard library was written such that iterators are the glue between algorithms and container.
It would be a lot simpler, if not necessarily more efficient at runtime, to just use some hash sets.
- This function could be generic in T rather than assuming
int
. - The repeated conditions make me feel like there's simplification waiting here, although exactly what that is eludes me in the two minutes I'm spending on this.
- Should take by
const
ref, not ref, so that you can operate on temporaries.
$endgroup$
1
$begingroup$
You caught the problems and I voted you up, but you could improve your answer by explaining what the issue for the first 4 bullet items.
$endgroup$
– pacmaninbw
13 hours ago
2
$begingroup$
@pacmaninbw: Added some context.
$endgroup$
– Martin York
10 hours ago
add a comment |
$begingroup$
I invite you to review @DeadMG's answer.
Rewriting following (most of) his advice, you'd get something like:
#include <cassert>
#include <algorithm>
#include <vector>
std::vector<T> intersection(std::vector<T> const& left_vector, std::vector<T> const& right_vector)
auto left = left_vector.begin();
auto left_end = left_vector.end();
auto right = right_vector.begin();
auto right_end = right_vector.end();
assert(std::is_sorted(left, left_end));
assert(std::is_sorted(right, right_end));
std::vector<T> result;
while (left != left_end && right != right_end)
if (*left == *right)
result.push_back(*left);
++left;
++right;
continue;
if (*left < *right)
++left;
continue;
assert(*left > *right);
++right;
return result;
I've always found taking pairs of iterators awkward, so I would not recommend such an interface. Instead, you could take simply take any "iterable", they need not even have the same value type, so long as they are comparable:
template <typename Left, typename Right>
std::vector<typename Left::value_type> intersection(Left const& left_c, Right const& right_c);
Also, note that I've included some assert
to validate the pre-conditions of the methods (the collections must be sorted) as well as internal invariants (if *left
is neither equal nor strictly less than *right
then it must be strictly greater).
I encourage you to use assert
liberally:
- They document intentions: pre-conditions, invariants, etc...
- They check that those intentions hold.
Documentation & Bug detection rolled in one, with no run-time (Release) cost.
$endgroup$
1
$begingroup$
Why don't you post that as its own questions. There are some improvements even if we don't move to iterators like using template template types.
$endgroup$
– Martin York
10 hours ago
1
$begingroup$
@MartinYork: I generally don't find "template template" to be an improvement, they're quite awkward to use, and tend to constrain the inputs more than intended.
$endgroup$
– Matthieu M.
9 hours ago
$begingroup$
@MatthieuM. You should reconsider the solution you posted.
$endgroup$
– Rick
4 hours ago
add a comment |
Your Answer
StackExchange.ifUsing("editor", function ()
return StackExchange.using("mathjaxEditing", function ()
StackExchange.MarkdownEditor.creationCallbacks.add(function (editor, postfix)
StackExchange.mathjaxEditing.prepareWmdForMathJax(editor, postfix, [["\$", "\$"]]);
);
);
, "mathjax-editing");
StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");
StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "196"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);
else
createEditor();
);
function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: false,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: null,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodereview.stackexchange.com%2fquestions%2f216861%2fintersection-of-two-sorted-vectors-in-c%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
$begingroup$
Indentation
Your indentation is not consistent. This makes the code hard to read and maintain. It should be fixed so you don't give other people headaches.
if(left < right)
while(l < nums1.size() && nums1[l] == left )l++;
else while( r < nums2.size() && nums2[r] == right )r++;That is basically unreadable giberish (opinion of Martin).
Using namespace
std;
is super badThis is mention in nearly every C++ review. There is a large article on the subject here: Why is “using namespace std” considered bad practice?. The second answer is the best in my opinion (Martin) see
Multiple declarations in one is bad (thanks to terrible syntax binding rules)
The one declaration per line has been written about adnausium in best practice guides. Please for the sake of your reader declare one variable per line with its own exact type.
The syntax binding rules alluded to above is:
int* x, y; // Here x is int* and y in int
// confusing to a reader. Did you really mean to make y an int?
// Avoid this problem be declaring one variable per lineTypically, functions like this would be based on iterators to work on any container
Here your code is limited to only using vectors. But the algorithm you are using could be used by any container type with only small modifications. As a result your function could provide much more utility being written to use iterators.
The standard library was written such that iterators are the glue between algorithms and container.
It would be a lot simpler, if not necessarily more efficient at runtime, to just use some hash sets.
- This function could be generic in T rather than assuming
int
. - The repeated conditions make me feel like there's simplification waiting here, although exactly what that is eludes me in the two minutes I'm spending on this.
- Should take by
const
ref, not ref, so that you can operate on temporaries.
$endgroup$
1
$begingroup$
You caught the problems and I voted you up, but you could improve your answer by explaining what the issue for the first 4 bullet items.
$endgroup$
– pacmaninbw
13 hours ago
2
$begingroup$
@pacmaninbw: Added some context.
$endgroup$
– Martin York
10 hours ago
add a comment |
$begingroup$
Indentation
Your indentation is not consistent. This makes the code hard to read and maintain. It should be fixed so you don't give other people headaches.
if(left < right)
while(l < nums1.size() && nums1[l] == left )l++;
else while( r < nums2.size() && nums2[r] == right )r++;That is basically unreadable giberish (opinion of Martin).
Using namespace
std;
is super badThis is mention in nearly every C++ review. There is a large article on the subject here: Why is “using namespace std” considered bad practice?. The second answer is the best in my opinion (Martin) see
Multiple declarations in one is bad (thanks to terrible syntax binding rules)
The one declaration per line has been written about adnausium in best practice guides. Please for the sake of your reader declare one variable per line with its own exact type.
The syntax binding rules alluded to above is:
int* x, y; // Here x is int* and y in int
// confusing to a reader. Did you really mean to make y an int?
// Avoid this problem be declaring one variable per lineTypically, functions like this would be based on iterators to work on any container
Here your code is limited to only using vectors. But the algorithm you are using could be used by any container type with only small modifications. As a result your function could provide much more utility being written to use iterators.
The standard library was written such that iterators are the glue between algorithms and container.
It would be a lot simpler, if not necessarily more efficient at runtime, to just use some hash sets.
- This function could be generic in T rather than assuming
int
. - The repeated conditions make me feel like there's simplification waiting here, although exactly what that is eludes me in the two minutes I'm spending on this.
- Should take by
const
ref, not ref, so that you can operate on temporaries.
$endgroup$
1
$begingroup$
You caught the problems and I voted you up, but you could improve your answer by explaining what the issue for the first 4 bullet items.
$endgroup$
– pacmaninbw
13 hours ago
2
$begingroup$
@pacmaninbw: Added some context.
$endgroup$
– Martin York
10 hours ago
add a comment |
$begingroup$
Indentation
Your indentation is not consistent. This makes the code hard to read and maintain. It should be fixed so you don't give other people headaches.
if(left < right)
while(l < nums1.size() && nums1[l] == left )l++;
else while( r < nums2.size() && nums2[r] == right )r++;That is basically unreadable giberish (opinion of Martin).
Using namespace
std;
is super badThis is mention in nearly every C++ review. There is a large article on the subject here: Why is “using namespace std” considered bad practice?. The second answer is the best in my opinion (Martin) see
Multiple declarations in one is bad (thanks to terrible syntax binding rules)
The one declaration per line has been written about adnausium in best practice guides. Please for the sake of your reader declare one variable per line with its own exact type.
The syntax binding rules alluded to above is:
int* x, y; // Here x is int* and y in int
// confusing to a reader. Did you really mean to make y an int?
// Avoid this problem be declaring one variable per lineTypically, functions like this would be based on iterators to work on any container
Here your code is limited to only using vectors. But the algorithm you are using could be used by any container type with only small modifications. As a result your function could provide much more utility being written to use iterators.
The standard library was written such that iterators are the glue between algorithms and container.
It would be a lot simpler, if not necessarily more efficient at runtime, to just use some hash sets.
- This function could be generic in T rather than assuming
int
. - The repeated conditions make me feel like there's simplification waiting here, although exactly what that is eludes me in the two minutes I'm spending on this.
- Should take by
const
ref, not ref, so that you can operate on temporaries.
$endgroup$
Indentation
Your indentation is not consistent. This makes the code hard to read and maintain. It should be fixed so you don't give other people headaches.
if(left < right)
while(l < nums1.size() && nums1[l] == left )l++;
else while( r < nums2.size() && nums2[r] == right )r++;That is basically unreadable giberish (opinion of Martin).
Using namespace
std;
is super badThis is mention in nearly every C++ review. There is a large article on the subject here: Why is “using namespace std” considered bad practice?. The second answer is the best in my opinion (Martin) see
Multiple declarations in one is bad (thanks to terrible syntax binding rules)
The one declaration per line has been written about adnausium in best practice guides. Please for the sake of your reader declare one variable per line with its own exact type.
The syntax binding rules alluded to above is:
int* x, y; // Here x is int* and y in int
// confusing to a reader. Did you really mean to make y an int?
// Avoid this problem be declaring one variable per lineTypically, functions like this would be based on iterators to work on any container
Here your code is limited to only using vectors. But the algorithm you are using could be used by any container type with only small modifications. As a result your function could provide much more utility being written to use iterators.
The standard library was written such that iterators are the glue between algorithms and container.
It would be a lot simpler, if not necessarily more efficient at runtime, to just use some hash sets.
- This function could be generic in T rather than assuming
int
. - The repeated conditions make me feel like there's simplification waiting here, although exactly what that is eludes me in the two minutes I'm spending on this.
- Should take by
const
ref, not ref, so that you can operate on temporaries.
edited 33 mins ago
Peter Mortensen
25417
25417
answered 14 hours ago
DeadMGDeadMG
759612
759612
1
$begingroup$
You caught the problems and I voted you up, but you could improve your answer by explaining what the issue for the first 4 bullet items.
$endgroup$
– pacmaninbw
13 hours ago
2
$begingroup$
@pacmaninbw: Added some context.
$endgroup$
– Martin York
10 hours ago
add a comment |
1
$begingroup$
You caught the problems and I voted you up, but you could improve your answer by explaining what the issue for the first 4 bullet items.
$endgroup$
– pacmaninbw
13 hours ago
2
$begingroup$
@pacmaninbw: Added some context.
$endgroup$
– Martin York
10 hours ago
1
1
$begingroup$
You caught the problems and I voted you up, but you could improve your answer by explaining what the issue for the first 4 bullet items.
$endgroup$
– pacmaninbw
13 hours ago
$begingroup$
You caught the problems and I voted you up, but you could improve your answer by explaining what the issue for the first 4 bullet items.
$endgroup$
– pacmaninbw
13 hours ago
2
2
$begingroup$
@pacmaninbw: Added some context.
$endgroup$
– Martin York
10 hours ago
$begingroup$
@pacmaninbw: Added some context.
$endgroup$
– Martin York
10 hours ago
add a comment |
$begingroup$
I invite you to review @DeadMG's answer.
Rewriting following (most of) his advice, you'd get something like:
#include <cassert>
#include <algorithm>
#include <vector>
std::vector<T> intersection(std::vector<T> const& left_vector, std::vector<T> const& right_vector)
auto left = left_vector.begin();
auto left_end = left_vector.end();
auto right = right_vector.begin();
auto right_end = right_vector.end();
assert(std::is_sorted(left, left_end));
assert(std::is_sorted(right, right_end));
std::vector<T> result;
while (left != left_end && right != right_end)
if (*left == *right)
result.push_back(*left);
++left;
++right;
continue;
if (*left < *right)
++left;
continue;
assert(*left > *right);
++right;
return result;
I've always found taking pairs of iterators awkward, so I would not recommend such an interface. Instead, you could take simply take any "iterable", they need not even have the same value type, so long as they are comparable:
template <typename Left, typename Right>
std::vector<typename Left::value_type> intersection(Left const& left_c, Right const& right_c);
Also, note that I've included some assert
to validate the pre-conditions of the methods (the collections must be sorted) as well as internal invariants (if *left
is neither equal nor strictly less than *right
then it must be strictly greater).
I encourage you to use assert
liberally:
- They document intentions: pre-conditions, invariants, etc...
- They check that those intentions hold.
Documentation & Bug detection rolled in one, with no run-time (Release) cost.
$endgroup$
1
$begingroup$
Why don't you post that as its own questions. There are some improvements even if we don't move to iterators like using template template types.
$endgroup$
– Martin York
10 hours ago
1
$begingroup$
@MartinYork: I generally don't find "template template" to be an improvement, they're quite awkward to use, and tend to constrain the inputs more than intended.
$endgroup$
– Matthieu M.
9 hours ago
$begingroup$
@MatthieuM. You should reconsider the solution you posted.
$endgroup$
– Rick
4 hours ago
add a comment |
$begingroup$
I invite you to review @DeadMG's answer.
Rewriting following (most of) his advice, you'd get something like:
#include <cassert>
#include <algorithm>
#include <vector>
std::vector<T> intersection(std::vector<T> const& left_vector, std::vector<T> const& right_vector)
auto left = left_vector.begin();
auto left_end = left_vector.end();
auto right = right_vector.begin();
auto right_end = right_vector.end();
assert(std::is_sorted(left, left_end));
assert(std::is_sorted(right, right_end));
std::vector<T> result;
while (left != left_end && right != right_end)
if (*left == *right)
result.push_back(*left);
++left;
++right;
continue;
if (*left < *right)
++left;
continue;
assert(*left > *right);
++right;
return result;
I've always found taking pairs of iterators awkward, so I would not recommend such an interface. Instead, you could take simply take any "iterable", they need not even have the same value type, so long as they are comparable:
template <typename Left, typename Right>
std::vector<typename Left::value_type> intersection(Left const& left_c, Right const& right_c);
Also, note that I've included some assert
to validate the pre-conditions of the methods (the collections must be sorted) as well as internal invariants (if *left
is neither equal nor strictly less than *right
then it must be strictly greater).
I encourage you to use assert
liberally:
- They document intentions: pre-conditions, invariants, etc...
- They check that those intentions hold.
Documentation & Bug detection rolled in one, with no run-time (Release) cost.
$endgroup$
1
$begingroup$
Why don't you post that as its own questions. There are some improvements even if we don't move to iterators like using template template types.
$endgroup$
– Martin York
10 hours ago
1
$begingroup$
@MartinYork: I generally don't find "template template" to be an improvement, they're quite awkward to use, and tend to constrain the inputs more than intended.
$endgroup$
– Matthieu M.
9 hours ago
$begingroup$
@MatthieuM. You should reconsider the solution you posted.
$endgroup$
– Rick
4 hours ago
add a comment |
$begingroup$
I invite you to review @DeadMG's answer.
Rewriting following (most of) his advice, you'd get something like:
#include <cassert>
#include <algorithm>
#include <vector>
std::vector<T> intersection(std::vector<T> const& left_vector, std::vector<T> const& right_vector)
auto left = left_vector.begin();
auto left_end = left_vector.end();
auto right = right_vector.begin();
auto right_end = right_vector.end();
assert(std::is_sorted(left, left_end));
assert(std::is_sorted(right, right_end));
std::vector<T> result;
while (left != left_end && right != right_end)
if (*left == *right)
result.push_back(*left);
++left;
++right;
continue;
if (*left < *right)
++left;
continue;
assert(*left > *right);
++right;
return result;
I've always found taking pairs of iterators awkward, so I would not recommend such an interface. Instead, you could take simply take any "iterable", they need not even have the same value type, so long as they are comparable:
template <typename Left, typename Right>
std::vector<typename Left::value_type> intersection(Left const& left_c, Right const& right_c);
Also, note that I've included some assert
to validate the pre-conditions of the methods (the collections must be sorted) as well as internal invariants (if *left
is neither equal nor strictly less than *right
then it must be strictly greater).
I encourage you to use assert
liberally:
- They document intentions: pre-conditions, invariants, etc...
- They check that those intentions hold.
Documentation & Bug detection rolled in one, with no run-time (Release) cost.
$endgroup$
I invite you to review @DeadMG's answer.
Rewriting following (most of) his advice, you'd get something like:
#include <cassert>
#include <algorithm>
#include <vector>
std::vector<T> intersection(std::vector<T> const& left_vector, std::vector<T> const& right_vector)
auto left = left_vector.begin();
auto left_end = left_vector.end();
auto right = right_vector.begin();
auto right_end = right_vector.end();
assert(std::is_sorted(left, left_end));
assert(std::is_sorted(right, right_end));
std::vector<T> result;
while (left != left_end && right != right_end)
if (*left == *right)
result.push_back(*left);
++left;
++right;
continue;
if (*left < *right)
++left;
continue;
assert(*left > *right);
++right;
return result;
I've always found taking pairs of iterators awkward, so I would not recommend such an interface. Instead, you could take simply take any "iterable", they need not even have the same value type, so long as they are comparable:
template <typename Left, typename Right>
std::vector<typename Left::value_type> intersection(Left const& left_c, Right const& right_c);
Also, note that I've included some assert
to validate the pre-conditions of the methods (the collections must be sorted) as well as internal invariants (if *left
is neither equal nor strictly less than *right
then it must be strictly greater).
I encourage you to use assert
liberally:
- They document intentions: pre-conditions, invariants, etc...
- They check that those intentions hold.
Documentation & Bug detection rolled in one, with no run-time (Release) cost.
answered 10 hours ago
Matthieu M.Matthieu M.
2,1871810
2,1871810
1
$begingroup$
Why don't you post that as its own questions. There are some improvements even if we don't move to iterators like using template template types.
$endgroup$
– Martin York
10 hours ago
1
$begingroup$
@MartinYork: I generally don't find "template template" to be an improvement, they're quite awkward to use, and tend to constrain the inputs more than intended.
$endgroup$
– Matthieu M.
9 hours ago
$begingroup$
@MatthieuM. You should reconsider the solution you posted.
$endgroup$
– Rick
4 hours ago
add a comment |
1
$begingroup$
Why don't you post that as its own questions. There are some improvements even if we don't move to iterators like using template template types.
$endgroup$
– Martin York
10 hours ago
1
$begingroup$
@MartinYork: I generally don't find "template template" to be an improvement, they're quite awkward to use, and tend to constrain the inputs more than intended.
$endgroup$
– Matthieu M.
9 hours ago
$begingroup$
@MatthieuM. You should reconsider the solution you posted.
$endgroup$
– Rick
4 hours ago
1
1
$begingroup$
Why don't you post that as its own questions. There are some improvements even if we don't move to iterators like using template template types.
$endgroup$
– Martin York
10 hours ago
$begingroup$
Why don't you post that as its own questions. There are some improvements even if we don't move to iterators like using template template types.
$endgroup$
– Martin York
10 hours ago
1
1
$begingroup$
@MartinYork: I generally don't find "template template" to be an improvement, they're quite awkward to use, and tend to constrain the inputs more than intended.
$endgroup$
– Matthieu M.
9 hours ago
$begingroup$
@MartinYork: I generally don't find "template template" to be an improvement, they're quite awkward to use, and tend to constrain the inputs more than intended.
$endgroup$
– Matthieu M.
9 hours ago
$begingroup$
@MatthieuM. You should reconsider the solution you posted.
$endgroup$
– Rick
4 hours ago
$begingroup$
@MatthieuM. You should reconsider the solution you posted.
$endgroup$
– Rick
4 hours ago
add a comment |
Thanks for contributing an answer to Code Review Stack Exchange!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
Use MathJax to format equations. MathJax reference.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodereview.stackexchange.com%2fquestions%2f216861%2fintersection-of-two-sorted-vectors-in-c%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
4
$begingroup$
Do you know about
std::set_intersection()
? Reference and example implementations: en.cppreference.com/w/cpp/algorithm/set_intersection$endgroup$
– user673679
12 hours ago
2
$begingroup$
@user673679 yes I did, and didn't want to use it;
$endgroup$
– Rick
12 hours ago