Using std::set container for range items2019 Community Moderator ElectionHow do you set, clear, and toggle a...
Does Garmin Oregon 700 have Strava integration?
Whom do I have to contact for a ticket refund in case of denied boarding (in the EU)?
How to reorder street address on checkout page in magento 2?
Skis versus snow shoes - when to choose which for travelling the backcountry?
A "strange" unit radio astronomy
Six real numbers so that product of any five is the sixth one
Alameda and Belisario throwing a fair die.
Second-rate spelling
Why is working on the same position for more than 15 years not a red flag?
What to do when being responsible for data protection in your lab, yet advice is ignored?
Hacker Rank: Array left rotation
chrony vs. systemd-timesyncd – What are the differences and use cases as NTP clients?
Where is this triangular-shaped space station from?
It took me a lot of time to make this, pls like. (YouTube Comments #1)
Real life puzzle: Unknown alphabet or shorthand
Called into a meeting and told we are being made redundant (laid off) and "not to share outside". Can I tell my partner?
What am I? I am in theaters and computer programs
Infrastructure damage from sudden thickening of water
Rationale to prefer local variables over instance variables?
What are these green text/line displays shown during the livestream of Crew Dragon's approach to dock with the ISS?
Logistics of a hovering watercraft in a fantasy setting
Can we carry rice to Japan?
How can atoms be electrically neutral when there is a difference in the positions of the charges?
Did 5.25" floppies undergo a change in magnetic coating?
Using std::set container for range items
2019 Community Moderator ElectionHow do you set, clear, and toggle a single bit?What's the best way to trim std::string?How to convert a std::string to const char* or char*?std::wstring VS std::stringHow to find out if an item is present in a std::vector?Why is “using namespace std” considered bad practice?How to check that an element is in a std::set?How to convert an Array to a Set in JavaEfficient mapping of ranges to groups of valuesComparing std::vector or std::set in this case for time complexity - More efficent?
I'd like to store a bunch of range items in std::set
container.
This data structure should provide fast decision whether a specific input range contained by one of the ranges that the set currently holds, by overloading the comparison of std::set
in order use the set::find
method to check one of the items in set contain the input range argument.
It should also support range item that represents a single point (start_range == end_range).
Here's my implementation :
#include <iostream>
#include <map>
#include <set>
using std::set;
using std::map;
class range : public std::pair<int,int>
{
public:
range(int lower, int upper)
{
if (upper < lower)
{
first = upper;
second = lower;
}
else
{
first = lower;
second = upper;
}
}
range(int val)
{
first = second = val;
}
bool operator<(range const & b) const
{
if (second < b.first)
{
return true;
}
return false;
}
};
And here's how I test my data structure:
int main(int argc, const char * argv[])
{
std::map<int, std::set<range>> n;
n[1].insert(range(-50,-40));
n[1].insert(range(40,50));
n[2].insert(range(-30,-20));
n[2].insert(range(20,30));
n[3].insert(range(-20,-10));
n[3].insert(range(10,20));
range v[] = {range(-50,-41), range(30,45), range(-45,-45), range(25,25)};
int j[] = {1,2,3};
for (int l : j)
{
for (range i : v)
{
if (n[l].find(i) != n[l].end())
{
std::cout << l << "," << i.first << "," << i.second << " : "
<< n[l].find(range(i))->first << " "
<< n[l].find(range(i))->second << std::endl;
}
}
}
}
and here are the results I get:
1,-50,-41 : -50 -40 --> good
1,30,45 : 40 50 --> bad
1,-45,-45 : -50 -40 --> good
2,30,45 : 20 30 --> bad
2,25,25 : 20 30 --> good
So as you can see, my code does support perfectly well single point range (-45 is contained by range (-50,-40) and 25 is contained by by range (20,30))
However, as for wider ranges, my current operator <
is capable of finding the contained
relationship which is equal
for the set
terminology (meaning that for ranges a and b a<b && a<b
.
Is there anyway to change this operator to make it work ?
c++ data-structures set std
|
show 5 more comments
I'd like to store a bunch of range items in std::set
container.
This data structure should provide fast decision whether a specific input range contained by one of the ranges that the set currently holds, by overloading the comparison of std::set
in order use the set::find
method to check one of the items in set contain the input range argument.
It should also support range item that represents a single point (start_range == end_range).
Here's my implementation :
#include <iostream>
#include <map>
#include <set>
using std::set;
using std::map;
class range : public std::pair<int,int>
{
public:
range(int lower, int upper)
{
if (upper < lower)
{
first = upper;
second = lower;
}
else
{
first = lower;
second = upper;
}
}
range(int val)
{
first = second = val;
}
bool operator<(range const & b) const
{
if (second < b.first)
{
return true;
}
return false;
}
};
And here's how I test my data structure:
int main(int argc, const char * argv[])
{
std::map<int, std::set<range>> n;
n[1].insert(range(-50,-40));
n[1].insert(range(40,50));
n[2].insert(range(-30,-20));
n[2].insert(range(20,30));
n[3].insert(range(-20,-10));
n[3].insert(range(10,20));
range v[] = {range(-50,-41), range(30,45), range(-45,-45), range(25,25)};
int j[] = {1,2,3};
for (int l : j)
{
for (range i : v)
{
if (n[l].find(i) != n[l].end())
{
std::cout << l << "," << i.first << "," << i.second << " : "
<< n[l].find(range(i))->first << " "
<< n[l].find(range(i))->second << std::endl;
}
}
}
}
and here are the results I get:
1,-50,-41 : -50 -40 --> good
1,30,45 : 40 50 --> bad
1,-45,-45 : -50 -40 --> good
2,30,45 : 20 30 --> bad
2,25,25 : 20 30 --> good
So as you can see, my code does support perfectly well single point range (-45 is contained by range (-50,-40) and 25 is contained by by range (20,30))
However, as for wider ranges, my current operator <
is capable of finding the contained
relationship which is equal
for the set
terminology (meaning that for ranges a and b a<b && a<b
.
Is there anyway to change this operator to make it work ?
c++ data-structures set std
Your second to last paragraph is not clear to me. Does it look like you intend it to?
– Yunnosch
4 hours ago
I fixed the obvious markdown problem, but the paragraph still is unclear to me. Please rephrase.
– Yunnosch
4 hours ago
The boolean expressiona<b && b<a
cannot ever evaluate to true. That is part of what confuses me.
– Yunnosch
4 hours ago
Please explain the intendet semantic and use of your operator overload.
– Yunnosch
4 hours ago
2
interval_tree might interest you.
– Jarod42
4 hours ago
|
show 5 more comments
I'd like to store a bunch of range items in std::set
container.
This data structure should provide fast decision whether a specific input range contained by one of the ranges that the set currently holds, by overloading the comparison of std::set
in order use the set::find
method to check one of the items in set contain the input range argument.
It should also support range item that represents a single point (start_range == end_range).
Here's my implementation :
#include <iostream>
#include <map>
#include <set>
using std::set;
using std::map;
class range : public std::pair<int,int>
{
public:
range(int lower, int upper)
{
if (upper < lower)
{
first = upper;
second = lower;
}
else
{
first = lower;
second = upper;
}
}
range(int val)
{
first = second = val;
}
bool operator<(range const & b) const
{
if (second < b.first)
{
return true;
}
return false;
}
};
And here's how I test my data structure:
int main(int argc, const char * argv[])
{
std::map<int, std::set<range>> n;
n[1].insert(range(-50,-40));
n[1].insert(range(40,50));
n[2].insert(range(-30,-20));
n[2].insert(range(20,30));
n[3].insert(range(-20,-10));
n[3].insert(range(10,20));
range v[] = {range(-50,-41), range(30,45), range(-45,-45), range(25,25)};
int j[] = {1,2,3};
for (int l : j)
{
for (range i : v)
{
if (n[l].find(i) != n[l].end())
{
std::cout << l << "," << i.first << "," << i.second << " : "
<< n[l].find(range(i))->first << " "
<< n[l].find(range(i))->second << std::endl;
}
}
}
}
and here are the results I get:
1,-50,-41 : -50 -40 --> good
1,30,45 : 40 50 --> bad
1,-45,-45 : -50 -40 --> good
2,30,45 : 20 30 --> bad
2,25,25 : 20 30 --> good
So as you can see, my code does support perfectly well single point range (-45 is contained by range (-50,-40) and 25 is contained by by range (20,30))
However, as for wider ranges, my current operator <
is capable of finding the contained
relationship which is equal
for the set
terminology (meaning that for ranges a and b a<b && a<b
.
Is there anyway to change this operator to make it work ?
c++ data-structures set std
I'd like to store a bunch of range items in std::set
container.
This data structure should provide fast decision whether a specific input range contained by one of the ranges that the set currently holds, by overloading the comparison of std::set
in order use the set::find
method to check one of the items in set contain the input range argument.
It should also support range item that represents a single point (start_range == end_range).
Here's my implementation :
#include <iostream>
#include <map>
#include <set>
using std::set;
using std::map;
class range : public std::pair<int,int>
{
public:
range(int lower, int upper)
{
if (upper < lower)
{
first = upper;
second = lower;
}
else
{
first = lower;
second = upper;
}
}
range(int val)
{
first = second = val;
}
bool operator<(range const & b) const
{
if (second < b.first)
{
return true;
}
return false;
}
};
And here's how I test my data structure:
int main(int argc, const char * argv[])
{
std::map<int, std::set<range>> n;
n[1].insert(range(-50,-40));
n[1].insert(range(40,50));
n[2].insert(range(-30,-20));
n[2].insert(range(20,30));
n[3].insert(range(-20,-10));
n[3].insert(range(10,20));
range v[] = {range(-50,-41), range(30,45), range(-45,-45), range(25,25)};
int j[] = {1,2,3};
for (int l : j)
{
for (range i : v)
{
if (n[l].find(i) != n[l].end())
{
std::cout << l << "," << i.first << "," << i.second << " : "
<< n[l].find(range(i))->first << " "
<< n[l].find(range(i))->second << std::endl;
}
}
}
}
and here are the results I get:
1,-50,-41 : -50 -40 --> good
1,30,45 : 40 50 --> bad
1,-45,-45 : -50 -40 --> good
2,30,45 : 20 30 --> bad
2,25,25 : 20 30 --> good
So as you can see, my code does support perfectly well single point range (-45 is contained by range (-50,-40) and 25 is contained by by range (20,30))
However, as for wider ranges, my current operator <
is capable of finding the contained
relationship which is equal
for the set
terminology (meaning that for ranges a and b a<b && a<b
.
Is there anyway to change this operator to make it work ?
c++ data-structures set std
c++ data-structures set std
edited 4 hours ago
Irad K
asked 4 hours ago
Irad KIrad K
434210
434210
Your second to last paragraph is not clear to me. Does it look like you intend it to?
– Yunnosch
4 hours ago
I fixed the obvious markdown problem, but the paragraph still is unclear to me. Please rephrase.
– Yunnosch
4 hours ago
The boolean expressiona<b && b<a
cannot ever evaluate to true. That is part of what confuses me.
– Yunnosch
4 hours ago
Please explain the intendet semantic and use of your operator overload.
– Yunnosch
4 hours ago
2
interval_tree might interest you.
– Jarod42
4 hours ago
|
show 5 more comments
Your second to last paragraph is not clear to me. Does it look like you intend it to?
– Yunnosch
4 hours ago
I fixed the obvious markdown problem, but the paragraph still is unclear to me. Please rephrase.
– Yunnosch
4 hours ago
The boolean expressiona<b && b<a
cannot ever evaluate to true. That is part of what confuses me.
– Yunnosch
4 hours ago
Please explain the intendet semantic and use of your operator overload.
– Yunnosch
4 hours ago
2
interval_tree might interest you.
– Jarod42
4 hours ago
Your second to last paragraph is not clear to me. Does it look like you intend it to?
– Yunnosch
4 hours ago
Your second to last paragraph is not clear to me. Does it look like you intend it to?
– Yunnosch
4 hours ago
I fixed the obvious markdown problem, but the paragraph still is unclear to me. Please rephrase.
– Yunnosch
4 hours ago
I fixed the obvious markdown problem, but the paragraph still is unclear to me. Please rephrase.
– Yunnosch
4 hours ago
The boolean expression
a<b && b<a
cannot ever evaluate to true. That is part of what confuses me.– Yunnosch
4 hours ago
The boolean expression
a<b && b<a
cannot ever evaluate to true. That is part of what confuses me.– Yunnosch
4 hours ago
Please explain the intendet semantic and use of your operator overload.
– Yunnosch
4 hours ago
Please explain the intendet semantic and use of your operator overload.
– Yunnosch
4 hours ago
2
2
interval_tree might interest you.
– Jarod42
4 hours ago
interval_tree might interest you.
– Jarod42
4 hours ago
|
show 5 more comments
2 Answers
2
active
oldest
votes
Your operator <
defines partial order:
(30,45) < (40, 50) == false
and simultaneously (40, 50) < (30, 45) == false
so in terms of std::set
and std::map
they are equal. That is why you got these results.
There is a paper about partial order: https://en.wikipedia.org/wiki/Partially_ordered_set
You might want use std::unordered_map
or define somehow total order for your ranges.
I suggest operator <
that compares the arithmetical mean of range bounds, i.e.
(a, b) < (c, d) if and only if (a+b)/2 < (c+d)/2 for total order. Note that you might want use float for arithmetical mean.
For testing I suggest the following code draft (I write here from scratch and didn't tested it). -1 meanst that are no range that contains this
int range::firstContainsMe(const std::vector<range> rangesVec)
{
for (size_t i = 0; i < rangesVec; i++) {
if (lower >= rangesVec[i].lower && upper <= rangesVec[i].upper) {
return i;
}
}
return -1;
}
Hi and thanks for you response, perhaps you can modify my code to make it work ? thanks
– Irad K
4 hours ago
@IradK : Added in answer
– Michael Lukin
4 hours ago
@michaellukin, but how does arithmetical mean help me to detect that one range is contained inside the other ? I need this contained relation as well as ordering (which the mean provide).
– Irad K
4 hours ago
@Yunnosch The explanation requeres rather long post so I added a link to Wikipedia.
– Michael Lukin
4 hours ago
@IradK std::map is not appropriate for your purpose.
– Michael Lukin
4 hours ago
|
show 2 more comments
Sounds like a perfect match for using Boost Interval Container Library. In short, you can
#include <boost/icl/interval_set.hpp>
// Helper function template to reduce explicit typing:
template <class T>
auto closed(T&& lower, T&& upper)
{
return boost::icl::discrete_interval<T>::closed(std::forward<T>(lower),
std::forward<T>(upper));
}
boost::icl::interval_set<int> ranges;
ranges.insert(closed(1, 2));
ranges.insert(closed(42, 50));
std::cout << contains(ranges, closed(43, 46)) << "n"; // true
std::cout << contains(ranges, closed(42, 54)) << "n"; // false
This should easily be pluggable into your std::map
and be usable without further adjustments.
add a comment |
Your Answer
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: "1"
};
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: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
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%2fstackoverflow.com%2fquestions%2f54998388%2fusing-stdset-container-for-range-items%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
Your operator <
defines partial order:
(30,45) < (40, 50) == false
and simultaneously (40, 50) < (30, 45) == false
so in terms of std::set
and std::map
they are equal. That is why you got these results.
There is a paper about partial order: https://en.wikipedia.org/wiki/Partially_ordered_set
You might want use std::unordered_map
or define somehow total order for your ranges.
I suggest operator <
that compares the arithmetical mean of range bounds, i.e.
(a, b) < (c, d) if and only if (a+b)/2 < (c+d)/2 for total order. Note that you might want use float for arithmetical mean.
For testing I suggest the following code draft (I write here from scratch and didn't tested it). -1 meanst that are no range that contains this
int range::firstContainsMe(const std::vector<range> rangesVec)
{
for (size_t i = 0; i < rangesVec; i++) {
if (lower >= rangesVec[i].lower && upper <= rangesVec[i].upper) {
return i;
}
}
return -1;
}
Hi and thanks for you response, perhaps you can modify my code to make it work ? thanks
– Irad K
4 hours ago
@IradK : Added in answer
– Michael Lukin
4 hours ago
@michaellukin, but how does arithmetical mean help me to detect that one range is contained inside the other ? I need this contained relation as well as ordering (which the mean provide).
– Irad K
4 hours ago
@Yunnosch The explanation requeres rather long post so I added a link to Wikipedia.
– Michael Lukin
4 hours ago
@IradK std::map is not appropriate for your purpose.
– Michael Lukin
4 hours ago
|
show 2 more comments
Your operator <
defines partial order:
(30,45) < (40, 50) == false
and simultaneously (40, 50) < (30, 45) == false
so in terms of std::set
and std::map
they are equal. That is why you got these results.
There is a paper about partial order: https://en.wikipedia.org/wiki/Partially_ordered_set
You might want use std::unordered_map
or define somehow total order for your ranges.
I suggest operator <
that compares the arithmetical mean of range bounds, i.e.
(a, b) < (c, d) if and only if (a+b)/2 < (c+d)/2 for total order. Note that you might want use float for arithmetical mean.
For testing I suggest the following code draft (I write here from scratch and didn't tested it). -1 meanst that are no range that contains this
int range::firstContainsMe(const std::vector<range> rangesVec)
{
for (size_t i = 0; i < rangesVec; i++) {
if (lower >= rangesVec[i].lower && upper <= rangesVec[i].upper) {
return i;
}
}
return -1;
}
Hi and thanks for you response, perhaps you can modify my code to make it work ? thanks
– Irad K
4 hours ago
@IradK : Added in answer
– Michael Lukin
4 hours ago
@michaellukin, but how does arithmetical mean help me to detect that one range is contained inside the other ? I need this contained relation as well as ordering (which the mean provide).
– Irad K
4 hours ago
@Yunnosch The explanation requeres rather long post so I added a link to Wikipedia.
– Michael Lukin
4 hours ago
@IradK std::map is not appropriate for your purpose.
– Michael Lukin
4 hours ago
|
show 2 more comments
Your operator <
defines partial order:
(30,45) < (40, 50) == false
and simultaneously (40, 50) < (30, 45) == false
so in terms of std::set
and std::map
they are equal. That is why you got these results.
There is a paper about partial order: https://en.wikipedia.org/wiki/Partially_ordered_set
You might want use std::unordered_map
or define somehow total order for your ranges.
I suggest operator <
that compares the arithmetical mean of range bounds, i.e.
(a, b) < (c, d) if and only if (a+b)/2 < (c+d)/2 for total order. Note that you might want use float for arithmetical mean.
For testing I suggest the following code draft (I write here from scratch and didn't tested it). -1 meanst that are no range that contains this
int range::firstContainsMe(const std::vector<range> rangesVec)
{
for (size_t i = 0; i < rangesVec; i++) {
if (lower >= rangesVec[i].lower && upper <= rangesVec[i].upper) {
return i;
}
}
return -1;
}
Your operator <
defines partial order:
(30,45) < (40, 50) == false
and simultaneously (40, 50) < (30, 45) == false
so in terms of std::set
and std::map
they are equal. That is why you got these results.
There is a paper about partial order: https://en.wikipedia.org/wiki/Partially_ordered_set
You might want use std::unordered_map
or define somehow total order for your ranges.
I suggest operator <
that compares the arithmetical mean of range bounds, i.e.
(a, b) < (c, d) if and only if (a+b)/2 < (c+d)/2 for total order. Note that you might want use float for arithmetical mean.
For testing I suggest the following code draft (I write here from scratch and didn't tested it). -1 meanst that are no range that contains this
int range::firstContainsMe(const std::vector<range> rangesVec)
{
for (size_t i = 0; i < rangesVec; i++) {
if (lower >= rangesVec[i].lower && upper <= rangesVec[i].upper) {
return i;
}
}
return -1;
}
edited 4 hours ago
answered 4 hours ago
Michael LukinMichael Lukin
1461112
1461112
Hi and thanks for you response, perhaps you can modify my code to make it work ? thanks
– Irad K
4 hours ago
@IradK : Added in answer
– Michael Lukin
4 hours ago
@michaellukin, but how does arithmetical mean help me to detect that one range is contained inside the other ? I need this contained relation as well as ordering (which the mean provide).
– Irad K
4 hours ago
@Yunnosch The explanation requeres rather long post so I added a link to Wikipedia.
– Michael Lukin
4 hours ago
@IradK std::map is not appropriate for your purpose.
– Michael Lukin
4 hours ago
|
show 2 more comments
Hi and thanks for you response, perhaps you can modify my code to make it work ? thanks
– Irad K
4 hours ago
@IradK : Added in answer
– Michael Lukin
4 hours ago
@michaellukin, but how does arithmetical mean help me to detect that one range is contained inside the other ? I need this contained relation as well as ordering (which the mean provide).
– Irad K
4 hours ago
@Yunnosch The explanation requeres rather long post so I added a link to Wikipedia.
– Michael Lukin
4 hours ago
@IradK std::map is not appropriate for your purpose.
– Michael Lukin
4 hours ago
Hi and thanks for you response, perhaps you can modify my code to make it work ? thanks
– Irad K
4 hours ago
Hi and thanks for you response, perhaps you can modify my code to make it work ? thanks
– Irad K
4 hours ago
@IradK : Added in answer
– Michael Lukin
4 hours ago
@IradK : Added in answer
– Michael Lukin
4 hours ago
@michaellukin, but how does arithmetical mean help me to detect that one range is contained inside the other ? I need this contained relation as well as ordering (which the mean provide).
– Irad K
4 hours ago
@michaellukin, but how does arithmetical mean help me to detect that one range is contained inside the other ? I need this contained relation as well as ordering (which the mean provide).
– Irad K
4 hours ago
@Yunnosch The explanation requeres rather long post so I added a link to Wikipedia.
– Michael Lukin
4 hours ago
@Yunnosch The explanation requeres rather long post so I added a link to Wikipedia.
– Michael Lukin
4 hours ago
@IradK std::map is not appropriate for your purpose.
– Michael Lukin
4 hours ago
@IradK std::map is not appropriate for your purpose.
– Michael Lukin
4 hours ago
|
show 2 more comments
Sounds like a perfect match for using Boost Interval Container Library. In short, you can
#include <boost/icl/interval_set.hpp>
// Helper function template to reduce explicit typing:
template <class T>
auto closed(T&& lower, T&& upper)
{
return boost::icl::discrete_interval<T>::closed(std::forward<T>(lower),
std::forward<T>(upper));
}
boost::icl::interval_set<int> ranges;
ranges.insert(closed(1, 2));
ranges.insert(closed(42, 50));
std::cout << contains(ranges, closed(43, 46)) << "n"; // true
std::cout << contains(ranges, closed(42, 54)) << "n"; // false
This should easily be pluggable into your std::map
and be usable without further adjustments.
add a comment |
Sounds like a perfect match for using Boost Interval Container Library. In short, you can
#include <boost/icl/interval_set.hpp>
// Helper function template to reduce explicit typing:
template <class T>
auto closed(T&& lower, T&& upper)
{
return boost::icl::discrete_interval<T>::closed(std::forward<T>(lower),
std::forward<T>(upper));
}
boost::icl::interval_set<int> ranges;
ranges.insert(closed(1, 2));
ranges.insert(closed(42, 50));
std::cout << contains(ranges, closed(43, 46)) << "n"; // true
std::cout << contains(ranges, closed(42, 54)) << "n"; // false
This should easily be pluggable into your std::map
and be usable without further adjustments.
add a comment |
Sounds like a perfect match for using Boost Interval Container Library. In short, you can
#include <boost/icl/interval_set.hpp>
// Helper function template to reduce explicit typing:
template <class T>
auto closed(T&& lower, T&& upper)
{
return boost::icl::discrete_interval<T>::closed(std::forward<T>(lower),
std::forward<T>(upper));
}
boost::icl::interval_set<int> ranges;
ranges.insert(closed(1, 2));
ranges.insert(closed(42, 50));
std::cout << contains(ranges, closed(43, 46)) << "n"; // true
std::cout << contains(ranges, closed(42, 54)) << "n"; // false
This should easily be pluggable into your std::map
and be usable without further adjustments.
Sounds like a perfect match for using Boost Interval Container Library. In short, you can
#include <boost/icl/interval_set.hpp>
// Helper function template to reduce explicit typing:
template <class T>
auto closed(T&& lower, T&& upper)
{
return boost::icl::discrete_interval<T>::closed(std::forward<T>(lower),
std::forward<T>(upper));
}
boost::icl::interval_set<int> ranges;
ranges.insert(closed(1, 2));
ranges.insert(closed(42, 50));
std::cout << contains(ranges, closed(43, 46)) << "n"; // true
std::cout << contains(ranges, closed(42, 54)) << "n"; // false
This should easily be pluggable into your std::map
and be usable without further adjustments.
answered 2 hours ago
lubgrlubgr
12.9k31849
12.9k31849
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- 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.
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%2fstackoverflow.com%2fquestions%2f54998388%2fusing-stdset-container-for-range-items%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
Your second to last paragraph is not clear to me. Does it look like you intend it to?
– Yunnosch
4 hours ago
I fixed the obvious markdown problem, but the paragraph still is unclear to me. Please rephrase.
– Yunnosch
4 hours ago
The boolean expression
a<b && b<a
cannot ever evaluate to true. That is part of what confuses me.– Yunnosch
4 hours ago
Please explain the intendet semantic and use of your operator overload.
– Yunnosch
4 hours ago
2
interval_tree might interest you.
– Jarod42
4 hours ago