我想知道删除字典中最旧的元素以控制字典大小的最佳方法.
例如:
MAXSIZE = 4
dict = {}
def add(key,value):
if len(dict) == MAXSIZE:
old = get_oldest_key() # returns the key to the oldest item
del dict[old]
dict[key] = value
add('a','1') # {'a': '1'}
add('b','2') # {'a': '1', 'b': '2'}
add('c','3') # {'a': '1', 'c': '3', 'b': '2'}
add('d','4') # {'a': '1', 'c': '3', 'b': '2', 'd': '4'}
add('e','5') # {'c': '3', 'b': '2', 'e': '5', 'd': '4'}
Run Code Online (Sandbox Code Playgroud)
这个清楚了吗?
编辑:忘了len(dict)落后一项.
在我的代码中测试一些向量操作时,我必须检查具有一些容差值的相等性,因为这些float值可能不完全匹配.
这意味着我的测试断言是这样的:
Assert.That(somevector.EqualWithinTolerance(new Vec3(0f, 1f, 0f)), Is.True);
Run Code Online (Sandbox Code Playgroud)
而不是这个:
Assert.That(somevector, Is.EqualTo(new Vec3(0f, 1f, 0f)));
Run Code Online (Sandbox Code Playgroud)
这意味着我的异常是这样的:
Expected: True
But was: False
Run Code Online (Sandbox Code Playgroud)
而不是这个:
Expected: 0 1 0
But was: 1 0 9,536743E-07
Run Code Online (Sandbox Code Playgroud)
让它稍微难以理解出了什么问题.
我如何使用自定义比较功能仍然得到一个很好的例外?
我似乎无法声明一个函数的泛型指针.
要调用这两个函数:
void myfunc1(std::string str)
{
std::cout << str << std::endl;
}
struct X
{
void f(std::string str){ std::cout<< str << std::endl;}
};
Run Code Online (Sandbox Code Playgroud)
和这两个函数调用者:
typedef void (*userhandler_t) (std::string);
struct example
{
userhandler_t userhandler_;
example(userhandler_t userhandler): userhandler_(userhandler){}
void call(std::string str)
{
userhandler_(str);
}
};
template<typename func_t>
void justfunc(func_t func)
{
func("hello, works!");
}
Run Code Online (Sandbox Code Playgroud)
当我尝试使用它们与boost :: bind来调用成员函数时,它们会给我编译错误.
这工作:
example e1(&myfunc1);
e1.call("hello, world!");
justfunc(&myfunc1);
Run Code Online (Sandbox Code Playgroud)
这不是:
X x;
example e2(boost::bind(&X::f, &x, _1));
e2.call("hello, world2!");
justfunc(boost::bind(&X::f, &x, _1));
Run Code Online (Sandbox Code Playgroud)
应该怎么做?
编辑:只是想提出我更清楚的问题.我几乎无法看到像Matrix.CreateTransformationZ这样的东西不仅在矩阵乘法的情况下工作,更重要的是它对屏幕空间/世界空间的作用,所以我可以得到更清晰的图像.因此,也许有人可以改变代码或给我一个简短的片段来测试我可以使用它来围绕轴旋转和/或绕轴旋转.我也改变了这个例子.
因此,我仍然难以直观地了解矩阵如何与xna屏幕空间一起工作.
我给你举个例子:
public class Game1 : Microsoft.Xna.Framework.Game
{
Texture2D shipTexture, rockTexture;
Vector2 shipPosition = new Vector2(100.0f, 100.0f);
Vector2 rockPosition = new Vector2(100.0f, 29.0f);
int count;
float shipRotation, rockRotation;
float rockSpeed, rockRotationSpeed;
bool move = true;
const int rock = 0;
const int ship = 1;
Color[] rockColor;
Color[] shipColor;
float testRot = 0.0f;
Vector2 shipCenter; int shipWidth, shipHeight;
Vector2 rockCenter; int rockWidth, rockHeight;
GraphicsDeviceManager graphics;
SpriteBatch spriteBatch;
#region maincontent
public Game1()
{
graphics = new GraphicsDeviceManager(this);
Content.RootDirectory = …Run Code Online (Sandbox Code Playgroud) 所以,我在vim中进行搜索:
/\(\(unum\)\|\(player\)=\)\@<!\"1\"
Run Code Online (Sandbox Code Playgroud)
并且正如预期的那样,它与以下行不匹配:
player="1"
Run Code Online (Sandbox Code Playgroud)
但匹配有以下行:
unum="1"
Run Code Online (Sandbox Code Playgroud)
我究竟做错了什么?所有这一切都不是要被否定的原子:\(\(unum\)\|\(player\)=\)
自然只是做:/\(\(unum\)\|\(player\)=\)匹配unum=或player=.
boost-bind ×1
c# ×1
c++ ×1
comparison ×1
dictionary ×1
find ×1
nunit ×1
python ×1
regex ×1
vim ×1
xna ×1