【发布时间】:2017-03-23 22:39:59
【问题描述】:
所以我有一个值图,例如:
1 3 5 7 9
使用我当前的代码,我目前能够找到 [low, high] 的某个范围之间的出现次数。例如,如果我有 [3, 7] 我会得到值 3。
我当前的代码如下所示:
// map::lower_bound/upper_bound
#include <iostream>
#include <map>
using namespace std;
int main ()
{
map<long,deque<my_struct>> my_map;
deque<my_struct> results;
mymap[12345].push_back(some_obj);
mymap[23456].push_back(some_obj);
mymap[34567].push_back(some_obj);
mymap[45678].push_back(some_obj);
auto low = my_map.lower_bound (12345);
auto high = my_map.upper_bound (34567);
int num = 0;
for (auto it = low; it != high; ++it){
++num;
//how to insert into my results deque?
}
cout << num << " found\n"; //3found
return 0;
}
我的问题是:我如何获得此示例中存在于低和高之间的 3 个对象?我想将这 3 个对象存储在“结果”双端队列中,这样我就知道在该特定范围内找到了什么 3。我知道我想推入 my-struct 的 3 个对象,但我很难弄清楚语法。任何提示或帮助将不胜感激!
编辑:尝试使用新方法
using namespace std;
struct my_struct{
string value;
};
int main ()
{
map<long,deque<my_struct>> my_map;
deque<my_struct> results;
my_struct entrya;
entrya.value = "today is a great day";
my_struct entryb;
entryb.value = "today is an okay day";
my_struct entryc;
entryc.value = "today is a bad day";
my_map[12345].push_back(entrya);
my_map[23456].push_back(entryb);
my_map[34567].push_back(entryc);
auto low = my_map.lower_bound (12345);
auto high = my_map.upper_bound (34567);
int num = 0;
for (auto it = low; it != high; ++it){
++num;
results.insert(results.back(), it->second.begin(), it->second.end());
//how to insert into my results deque?
}
cout << num << " found\n"; //3found
cout << "testing results\n";
for (int i = 0; i < results.size(); i++){
cout << results[i].value << "\n";
}
return 0;
}
【问题讨论】:
标签: c++ dictionary hash mapping