如果正确实施,A*路径查找算法是否可以保证找到100%的最短路径或时间?
int Graph::FindPath(Node *start, Node *finish, list< vec2f > &path)
{
list<NodeRecord*> open;
list<NodeRecord*> closed;
list<NodeRecord*>::iterator openIt;
list<NodeRecord*>::iterator closedIt;
// add the starting node to the open list
open.push_back( new NodeRecord(start, NULL, 0.0f, 0.0f + start->pos.DistanceSq(finish->pos) ) );
// NodeRecord(Node *node, Node *from, float cost, float totalCost)
while(!open.empty())
{
// find the node record with the lowest cost
NodeRecord *currentRecord = open.front();
openIt = ++open.begin();
while(openIt != open.end())
{
if((*openIt)->total < currentRecord->total)
currentRecord = (*openIt);
openIt++;
}
// get a …Run Code Online (Sandbox Code Playgroud) 我已经制作了一个用于分色图像的功能.
// =(
#define ARGB_COLOR(a, r, g, b) (((a) << 24) | ((r) << 16) | ((g) << 8) | (b))
inline UINT PosterizeColor(const UINT &color, const float &nColors)
{
__m128 clr = _mm_cvtepi32_ps( _mm_cvtepu8_epi32((__m128i&)color) );
clr = _mm_mul_ps(clr, _mm_set_ps1(nColors / 255.0f) );
clr = _mm_round_ps(clr, _MM_FROUND_TO_NEAREST_INT);
clr = _mm_mul_ps(clr, _mm_set_ps1(255.0f / nColors) );
__m128i iClr = _mm_cvttps_epi32(clr);
return ARGB_COLOR(iClr.m128i_u8[12],
iClr.m128i_u8[8],
iClr.m128i_u8[4],
iClr.m128i_u8[0]);
}
Run Code Online (Sandbox Code Playgroud)
在第一行,我将颜色打包成4个浮点数,但我找不到正确的反向方法.
我搜索了SSE文档,找不到相反的结果 _mm_cvtepu8_epi32
一个存在吗?
我知道有关于此的一百万个帖子,但我仍然无法弄清楚为什么这不起作用= /
这一行:
test = new Test2<Test>;
Run Code Online (Sandbox Code Playgroud)
给我这个错误:
error C2512: 'Test2<PARENT>' : no appropriate default constructor available
with
[
PARENT=Test
]
Run Code Online (Sandbox Code Playgroud)
码:
template<class PARENT>
class Test2;
////////////////////////////
class Test
{
public:
Test2<Test> *test;
Test()
{
test = new Test2<Test>;
}
};
/////////////////////////////
template<class PARENT>
class Test2
{
public:
PARENT *parent;
};
////////////////////////////
Run Code Online (Sandbox Code Playgroud)
有人可以帮我吗?
我知道有很多关于这个主题的帖子,但我很难找到这个问题的答案.
对于更快的函数调用,纯虚拟接口还是pimpl?
乍一看,在我看来,纯虚拟接口会更快,因为使用pimpl会花费两个函数调用而不是一个......或者在这种情况下某些聪明的编译器技巧会接管吗?
编辑:我正在尝试决定我应该使用哪些来抽象出几个对象的系统相关部分,这些部分可能最终必须经常产生,而且数量很多.
编辑:
我想在这一点上值得一提,我的问题的根源在于我错误地将抽象工厂设计模式误认为是使我的代码在多个平台上工作的方法,当它的真正目的是切换给定接口的实现时在运行时.
c++ performance virtual-functions cross-platform pimpl-idiom
我需要在一个坚实的内存块中分配这个结构的数组."char*extension"和"char*type"的长度在编译时是未知的.
struct MIMETYPE
{
char *extension;
char *type;
};
Run Code Online (Sandbox Code Playgroud)
如果我使用"new"运算符来自己初始化每个元素,则内存可能会分散.这就是我尝试为它分配一个连续的内存块的方法:
//numTypes = total elements of array
//maxExtension and maxType are the needed lengths for the (char*) in the struct
//std::string ext, type;
unsigned int size = (maxExtension+1 + maxType+1) * numTypes;
mimeTypes = (MIMETYPE*)HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, size);
Run Code Online (Sandbox Code Playgroud)
但是,当我尝试像这样加载数据时,当我稍后尝试访问数据时,数据全部乱序并且分散.
for(unsigned int i = 0; i < numTypes; i++)
{
//get data from file
getline(fin, line);
stringstream parser.str(line);
parser >> ext >> type;
//point the pointers at a spot in the memory that …Run Code Online (Sandbox Code Playgroud) 在C++中,我可以这样做:
void func(void *something) {
cout << "something" << endl;
}
void func(nullptr_t) {
cout << "nullptr_t" << endl;
}
int main() {
int nothing = 5;
func(¬hing);
func(nullptr);
return 0;
}
Run Code Online (Sandbox Code Playgroud)
哪个输出:
东西
nullptr_t
所以我可以特殊情况nullptr_t.
在D中,我有两个功能:
void func(void* pod) {
cout << "pod" << endl;
}
void func(Object obj) {
cout << "Object" << endl;
}
Run Code Online (Sandbox Code Playgroud)
那么如何解决调用时的歧义func(null)?
我在这里错过了什么吗?还是有理由不允许这样做?
// the class declaration
class MapImage : public MapEntity, public Vector2D {};
// the variable declaration
std::vector<MapImage> healthpacks;
// the function
void DrawItems(SDL_Surface *dest, std::vector<Vector2D> &items, SDL_Surface *image);
// the implementation
DrawItems(dest, healthpacks, healthpack_image);
Run Code Online (Sandbox Code Playgroud)
因为healthpacks是一个MapImage类的std :: vector,而MapImage有基类Vector2D,所以不应该"std :: vector healthpacks"与"std :: vector&items"兼容,因为它们具有相同的基类?
如果我有课:
Object.h
class Object
{
public:
static int number;
};
Run Code Online (Sandbox Code Playgroud)
Object.cpp
int Object::number = 5;
Run Code Online (Sandbox Code Playgroud)
Object::number保证的范围是否超出了Object创建的任何实例的范围?即使它在另一个源文件中全局声明?
编辑更新的代码:
class Any
{
public:
Any()
{
}
Any(const Any &other)
{
}
Any(Any &other) // added per Ben's answer
{
}
Any(Any &&other)
{
}
Any(const char *value)
{
}
template<typename T>
Any(const T &value)
{
}
template<typename T>
Any(T &&value)
{
cout << "move ctor" << endl;
}
template<typename T>
Any(const vector<T> &value)
{
}
template<typename T>
Any(vector<T> &&value)
{
}
};
int main(int argc, char *argv[])
{
vector<string> numbers;
numbers.push_back("one");
numbers.push_back("two");
numbers.push_back("three");
numbers.push_back("four");
Any anyNumbers(numbers);
Any …Run Code Online (Sandbox Code Playgroud) 可能重复:
c ++中UUID生成的示例
我需要一个算法来生成24个字符的UUID,但我能找到的只是生成标准32个字符的生成器,如下所示:
550e8400-e29b-41d4-a716-446655440000
如何生成24个字符的UUID?
我在Google上进行了快速搜索,但无法找到与此确切主题相关的任何内容.
随着C++继续朝着更现代化的语言方向发展,包括lambdas,基于循环的范围等,似乎这个问题最终可能会出现,如果还没有的话.
我不知道这怎么可能只是一件好事,并提供在C++/CLI,C#,Java等中证明有用的相同好处.如果有人不想要这种行为,它可能只是使用可编程设置,并使用编译器设置关闭,与禁用标准异常或RTTI的方式相同.
此外,有人建议,但不鼓励人们可以为SIGSEGV创建信号处理程序并从那里抛出异常,以模拟建议的行为.现在,虽然这有点像黑客攻击,并且不能保证在所有平台上都可以工作,但如果可以模拟(非标准)大约10行的相同基本行为,那么在C++中实现空引用异常真的有多难代码?
那么,是否存在任何技术或其他原因导致错误指针访问的异常最终无法成为标准的一部分?
c++ ×10
class ×2
templates ×2
algorithm ×1
ambiguity ×1
arguments ×1
constructor ×1
d ×1
guid ×1
hash ×1
inheritance ×1
null ×1
path-finding ×1
performance ×1
pimpl-idiom ×1
simd ×1
sse ×1
standards ×1
static ×1
uuid ×1
vector ×1