如何在 C++ 中定义具有最大大小的值的键值对
How to define a key value pair with values having a max size in c++
我想制作一个字典(可能类似于 std::multimap
),其中包含同一个键的多个值。这里的主要内容是我希望值具有最大大小 (n),如果出现第 (n+1) 个值,则应删除第一个值(如 boost::circular_buffer
或其他)。更具体地说,
我有一个结构
Struct A{
double id; // key
double x, y, z; // value
}
那我定义
std::vector<A> a{};
while (condition)
{
a = getFromSomeWhere();
////////////
// I DONT KNOW HOW TO STORE THE VALUES in `a` //
// THIS IS WHAT I AM LOOKING FOR //
////////////
}
调用getFromSomeWhere
时,假设a
在条件变为假之前得到以下值
{
a1, a2, a3, a4
a1, a3, a4, a5
a2, a4, a6
}
我要找的答案应该是
{ a1.id : a1, a1 // can go to a maximum size of n before removing the 1st
a2.id : a2, a2 // can go to a maximum size of n before removing the 1st
a3.id : a3, a3 // can go to a maximum size of n before removing the 1st
a4.id : a4, a4, a4 // can go to a maximum size of n before removing the 1st
a5.id : a5 // can go to a maximum size of n before removing the 1st
a6.id : a6 // can go to a maximum size of n before removing the 1st
}
感谢大家的回复。我在 fCaponetto.
的帮助下解决了这个问题
#include<iostream>
#include <boost/circular_buffer.hpp>
#include <map>
#include <vector>
struct Vis
{
double x;
double y;
double z;
bool seen;
uint id;
};
typedef boost::circular_buffer<Vis> VisBuffer;
typedef std::shared_ptr<VisBuffer> VisBufferPtr;
void makeVisMap(const std::vector<Vis> &b, std::map<uint, VisBufferPtr> &visMap)
{
unsigned int const max = 500;
for (const auto &v : b)
{
if (!visMap[v.id])
visMap[v.id] = std::make_shared<VisBuffer>(max);
visMap[v.id]->push_back(v);
}
}
int main ()
{
Vis s1{}, s2{}, s3{}, s4{}, s5{};
s1.id = 1; s2.id = 2; s3.id = 3; s4.id = 4; s5.id = 5;
s1.x = s1.y = s1.z = 1; s2.x = s2.y = s2.z = 2; s3.x = s3.y = s3.z = 3; s4.x = s4.y = s4.z = 4; s5.x = s5.y = s5.z = 5;
std::vector<Vis> sv {s1, s2, s3, s4, s5, s2, s3, s4, s4, s4, s4};
std::map<uint, VisBufferPtr> visMap{};
makeVisMap(sv, visMap);
return 0;
}
我想制作一个字典(可能类似于 std::multimap
),其中包含同一个键的多个值。这里的主要内容是我希望值具有最大大小 (n),如果出现第 (n+1) 个值,则应删除第一个值(如 boost::circular_buffer
或其他)。更具体地说,
我有一个结构
Struct A{
double id; // key
double x, y, z; // value
}
那我定义
std::vector<A> a{};
while (condition)
{
a = getFromSomeWhere();
////////////
// I DONT KNOW HOW TO STORE THE VALUES in `a` //
// THIS IS WHAT I AM LOOKING FOR //
////////////
}
调用getFromSomeWhere
时,假设a
在条件变为假之前得到以下值
{
a1, a2, a3, a4
a1, a3, a4, a5
a2, a4, a6
}
我要找的答案应该是
{ a1.id : a1, a1 // can go to a maximum size of n before removing the 1st
a2.id : a2, a2 // can go to a maximum size of n before removing the 1st
a3.id : a3, a3 // can go to a maximum size of n before removing the 1st
a4.id : a4, a4, a4 // can go to a maximum size of n before removing the 1st
a5.id : a5 // can go to a maximum size of n before removing the 1st
a6.id : a6 // can go to a maximum size of n before removing the 1st
}
感谢大家的回复。我在 fCaponetto.
的帮助下解决了这个问题#include<iostream>
#include <boost/circular_buffer.hpp>
#include <map>
#include <vector>
struct Vis
{
double x;
double y;
double z;
bool seen;
uint id;
};
typedef boost::circular_buffer<Vis> VisBuffer;
typedef std::shared_ptr<VisBuffer> VisBufferPtr;
void makeVisMap(const std::vector<Vis> &b, std::map<uint, VisBufferPtr> &visMap)
{
unsigned int const max = 500;
for (const auto &v : b)
{
if (!visMap[v.id])
visMap[v.id] = std::make_shared<VisBuffer>(max);
visMap[v.id]->push_back(v);
}
}
int main ()
{
Vis s1{}, s2{}, s3{}, s4{}, s5{};
s1.id = 1; s2.id = 2; s3.id = 3; s4.id = 4; s5.id = 5;
s1.x = s1.y = s1.z = 1; s2.x = s2.y = s2.z = 2; s3.x = s3.y = s3.z = 3; s4.x = s4.y = s4.z = 4; s5.x = s5.y = s5.z = 5;
std::vector<Vis> sv {s1, s2, s3, s4, s5, s2, s3, s4, s4, s4, s4};
std::map<uint, VisBufferPtr> visMap{};
makeVisMap(sv, visMap);
return 0;
}