是什么之间的区别的.vcproj和.vcxproj Visual Studio项目文件?是的.vcproj仅在2010年之前的Visual Studio版本格式?
我问,因为我正在阅读日期为10/12的MSDN教程,该教程说明将文本插入.vcproj文件中不存在的.vcproj文件的某个区域.试图将文本放在类似的区域会破坏项目.
我有一个关于使用std :: weak_ptr作为std :: map的键的问题.
#include <map>
#include <memory>
int main()
{
std::map< std::weak_ptr<int>, bool > myMap;
std::shared_ptr<int> sharedptr(new int(5));
std::weak_ptr<int> weakptr = sharedptr;
myMap[weakptr] = true;
return 0;
}
Run Code Online (Sandbox Code Playgroud)
上面的程序没有构建,并且尝试编译会给出许多错误消息,例如:
1>C:\Program Files (x86)\Microsoft Visual Studio 10.0\VC\include\xfunctional(125): error C2784: 'bool std::operator <(const std::_Tree<_Traits> &,const std::_Tree<_Traits> &)' : could not deduce template argument for 'const std::_Tree<_Traits> &' from 'const std::tr1::weak_ptr<_Ty>'
1> with
1> [
1> _Ty=int
1> ]
1> C:\Program Files (x86)\Microsoft Visual Studio 10.0\VC\include\xtree(1885) : see declaration of 'std::operator <' …
Run Code Online (Sandbox Code Playgroud) 我有一个weak_ptrs列表,我用它来跟踪对象.在某个时刻,我想从给定shared_ptr或weak_ptr的列表中删除一个项目.
#include <list>
int main()
{
typedef std::list< std::weak_ptr<int> > intList;
std::shared_ptr<int> sp(new int(5));
std::weak_ptr<int> wp(sp);
intList myList;
myList.push_back(sp);
//myList.remove(sp);
//myList.remove(wp);
}
Run Code Online (Sandbox Code Playgroud)
但是,当我取消注释上述行时,程序将无法构建:
1>c:\program files (x86)\microsoft visual studio 10.0\vc\include\list(1194): error C2678: binary '==' : no operator found which takes a left-hand operand of type 'std::tr1::weak_ptr<_Ty>' (or there is no acceptable conversion)
Run Code Online (Sandbox Code Playgroud)
如何在给定shared_ptr或weak_ptr的情况下从列表中删除项目?
在下面的代码中,pC == pA:
class A
{
};
class B : public A
{
public:
int i;
};
class C : public B
{
public:
char c;
};
int main()
{
C* pC = new C;
A* pA = (A*)pC;
return 0;
}
Run Code Online (Sandbox Code Playgroud)
但是当我向B添加一个纯虚函数并在C中实现它时,pA!= pC:
class A
{
};
class B : public A
{
public:
int i;
virtual void Func() = 0;
};
class C : public B
{
public:
char c;
void Func() {}
};
int main()
{
C* …
Run Code Online (Sandbox Code Playgroud) 我工作的一个游戏,只有一个对象可以在位置存在(x, y)
哪里x
以及y
是ints
.例如,对象可能存在于(0, 0)
或不存在,但是不可能同时存在多个对象.
我正在尝试确定哪个STL容器用于手头的问题以及解决此问题的最佳方法.
基本上,我从一个对象及其(x, y)
位置开始.目标是根据该对象的周围对象确定最高,最大可能的矩形.必须使用当前对象上方和下方的所有对象创建矩形.也就是说,它必须是最高的,它可能基于起始物体位置.
例如,假设以下代表我的对象网格,并且我从位置处的绿色对象开始(3, 4)
:
然后,我正在寻找的矩形将由下面的粉红色方块表示:
因此,假设我开始与该对象在(3, 4)
类似的例子表明,我将需要检查的对象也存在在(2, 4)
,(4, 4)
,(3, 3)
,和(3, 5)
.如果某个对象存在于任何这些位置,我需要重复该对象的过程以找到最大可能的矩形.
这些物品相当罕见,游戏世界庞大.new
由于大多数元素都是空的,因此对于整个游戏世界而言仅仅是2D阵列似乎不实际.但是,我需要索引到任何位置以检查对象是否随时存在.
相反,我想过使用std::map
这样的:
std::map< std::pair<int, int>, ObjectData> m_objects;
Run Code Online (Sandbox Code Playgroud)
然后,当我检查周围的物体时,我可以map::find()
在我的循环中使用,检查是否存在周围的物体:
if(m_objects.find(std::pair<3, 4>) != m_objects.end())
{
//An object exists at (3, 4).
//Add it to the list of surrounding objects.
}
Run Code Online (Sandbox Code Playgroud)
map::find()
如果我决定这样做,我可能会做很多调用,但是地图占用的内存比new
整个世界的2D数组少得多.
有没有人对我可以用来找到我要找的东西的简单算法有任何建议?我应该继续使用 …
MyClass.h
class MyClass
{
public:
static const int cTotalCars;
private:
int m_Cars[cTotalCars];
};
Run Code Online (Sandbox Code Playgroud)
MyClass.cpp
#include "MyClass.h"
const int MyClass::cTotalCars = 5;
Run Code Online (Sandbox Code Playgroud)
上面的代码不起作用,因为它会为m_Cars数组说"预期的常量表达式".
class MyClass
{
public:
static const int cTotalCars = 5;
private:
int m_Cars[cTotalCars];
};
Run Code Online (Sandbox Code Playgroud)
上面的方法是有效的,但我被告知我应该总是在CPP文件中定义静态成员,在类定义之外.我能做什么?
可能重复:
有没有理由在删除之前检查NULL指针?
我经常在代码中看到以下内容:
if(pointer)
delete pointer;
Run Code Online (Sandbox Code Playgroud)
根据我的理解,删除空指针是安全的,那么这个检查有什么意义呢?
Test.h
#ifndef TEST_H
#define TEST_H
#include <memory>
template <class Type>
bool operator==(const std::weak_ptr<Type>& wp1, const std::weak_ptr<Type>& wp2)
{
std::shared_ptr<Type> sp1;
if(!wp1.expired())
sp1 = wp1.lock();
std::shared_ptr<Type> sp2;
if(!wp2.expired())
sp2 = wp2.lock();
return sp1 == sp2;
}
#endif
Run Code Online (Sandbox Code Playgroud)
TEST.CPP
#include "Test.h"
#include <list>
int main()
{
typedef std::list< std::weak_ptr<int> > intList;
std::shared_ptr<int> sp(new int(5));
std::weak_ptr<int> wp(sp);
intList myList;
myList.push_back(wp);
myList.remove(wp); //Problem
}
Run Code Online (Sandbox Code Playgroud)
由于myList.remove(),程序将无法编译:
1> c:\ program files(x86)\ microsoft visual studio 10.0\vc\include\list(1194):错误C2678:二进制'==':找不到运算符,它采用类型为'std ::的左手操作数tr1 :: weak_ptr <_Ty>'(或没有可接受的转换)1>
1> [1> _Ty = int 1>]
但是你可以在Test.h中看到以下定义: …
此问题适用于任何类型的静态数据.我只是int
用来保持示例简单.
我正在读取一个包含整数的大型XML数据文件,并将它们存储在一个vector<int>
.对于我正在使用的特定数据,相同的值连续重复多次是很常见的.
<Node value="4" count="4000">
Run Code Online (Sandbox Code Playgroud)
该count
属性表示该值重复x次:
for(int i = 0; i < 4000; i++)
vec.push_back(4);
Run Code Online (Sandbox Code Playgroud)
当我已经知道它将连续出现4000次时,重复存储相同值似乎是浪费内存.但是,我需要能够在任何时候索引到向量.
对于较大的数据对象,我知道我只能存储一个指针,但仍然需要在上面的例子中存储4000个相同的指针.
是否有任何类型的策略来处理这样的问题?
我有一个带有静态const成员的模板类:
template <class T>
class A
{
public:
A(T val) : t(val) {}
static const int VALUE = 5;
T t;
};
Run Code Online (Sandbox Code Playgroud)
让我们说在我的代码中的某个地方,我使用int,char和long类型实例化它.现在我想访问VALUE:
int main()
{
int i1 = A<int>::VALUE;
int i2 = A<char>::VALUE;
int i3 = A<long>::VALUE;
return 0;
}
Run Code Online (Sandbox Code Playgroud)
是不是所有上述相同的方式都可以访问相同的东西?在这种情况下,其他人只选择随机类型吗?有没有办法避免指定类型?
限制无符号整数的正确方法是什么?
例如,假设我有:
unsigned int ui = 5U;
Run Code Online (Sandbox Code Playgroud)
现在我想从中减去一个值:
ui = Clamp(ui - MAGIC_VALUE, 0, 255);
Run Code Online (Sandbox Code Playgroud)
如果 MAGIC_VALUE >= 5,我希望 ui 包含 0。
但是,我不能只减去 MAGIC_VALUE,因为如果 MAGIC_VALUE > 5,表达式ui - MAGIC_VALUE
将回绕到UINT_MAX
上限,最终被限制为上限 255。这与我想要的相反!
有什么技巧可以解决这个问题吗?