小编man*_*eak的帖子

C++ 11线程:休眠一段时间

我正在尝试使用C++ 11线程为我的小游戏实现更新线程.我已经"尽可能快地"更新周期,但我想限制它说,每秒60次.如何剩下剩余的时间?

Core::Core()
{
    std::thread updateThread(update); // Start update thread
}

void Core::update()
{
    // TODO Get start time
    // Here happens the actual update stuff
    // TODO Get end time
    // double duration = ...; // Get the duration

    // Sleep if necessary
    if(duration < 1.0 / 60.0)
    {
        _sleep(1.0 / 60.0 - duration);
    }
}
Run Code Online (Sandbox Code Playgroud)

c++ multithreading c++11

3
推荐指数
2
解决办法
4369
查看次数

如何找出回收器视图被捕捉到哪个项目?

我有一个回收器视图,用于LinearSnapHelper在用户滚动项目时捕捉项目.现在,我想听一下快照,最好是获取被捕捉的项目的索引.但是,我无法弄清楚是否有办法做到这一点.

起初我以为LinearSnapHelperfindTargetSnapPosition()将返回索引卡(如文档说),但是这是不正确的.它为第一个项随机返回-1或0,当滚动列表时,它会被随机调用.有时,该方法根本没有被调用; 有时,索引不正确,有时它是正确的.似乎尝试使用它找到索引是没用的.

那么:我如何找出回收者视图捕捉到的项目?

java android android-recyclerview

2
推荐指数
1
解决办法
574
查看次数

如何为特征的每个实现自动生成递增的数字标识符?

我有一个Component特征,该特征具有返回索引的方法,如下所示:

trait Component {
    fn index(&self) -> usize;
}
Run Code Online (Sandbox Code Playgroud)

这些索引用于在位集中设置标志。例如,Component返回索引为5 的特征对象将导致在容器中设置第5位。

目前,我为每种实现类型手动返回运行索引:

struct Foo;
struct Bar;

impl Component for Foo {
    fn index(&self) -> usize { 0 }
}

impl Component for Bar {
    fn index(&self) -> usize { 1 }
}
Run Code Online (Sandbox Code Playgroud)

特质对象插入到容器中,该容器使用位集跟踪添加的组件:

struct Container<'a> {
    components: Vec<Component + 'a>,
    bits: BitSet
}

impl<'a> Container<'a> {
    fn add<T: Component + 'a>(&mut self, component: T) {
        self.components.push(component);
        self.bits.set(component.index());
    }
}
Run Code Online (Sandbox Code Playgroud)

这可以正常工作,但是手动返回每种实现类型的索引很麻烦。我怎样才能使每种实现类型都自动获得其索引?

rust rust-macros

2
推荐指数
1
解决办法
121
查看次数

如何根据当前的构建配置指定要编译的.cpp文件?

我的项目中有多个构建配置,我想根据当前选择的配置交换一些.CPP文件.我如何在Visual Studio 2013中执行此操作?

c++ visual-studio

1
推荐指数
1
解决办法
776
查看次数

在尝试为Android编译时,错误"'JNIEXPORT'没有命名类型"

我正在尝试使用NDK编译C++代码以在Android上使用.但是,我在尝试编译时遇到此错误:

error: 'JNIEXPORT' does not name a type (File: JNIApi.h, line: 9)
error: 'JNIEXPORT' does not name a type (File: JNIApi.h, line: 10)
Run Code Online (Sandbox Code Playgroud)

JNIApi.h看起来像这样:

#pragma once

extern "C"
{
    JNIEXPORT void JNICALL Java_com_manabreak_tremortest_TremorLauncher_initTremor(JNIEnv* jenv, jobject obj);
    JNIEXPORT void JNICALL Java_com_manabreak_tremortest_TremorLauncher_setSurface(JNIEnv* jenv, jobject obj, jobject surface);
}
Run Code Online (Sandbox Code Playgroud)

构建过程输出:

Build started 1.8.2014 11:14:17.
   1>Project "E:\Tremor\build\Tremor.vcxproj" on node 2 (Build target(s)).
   1>ClCompile:
       E:\ADT\ndk\toolchains\arm-linux-androideabi-4.8\prebuilt\windows-x86_64\bin\arm-linux-androideabi-g++.exe JNIApi.cpp
       E:\ADT\ndk\toolchains\arm-linux-androideabi-4.8\prebuilt\windows-x86_64\bin\arm-linux-androideabi-g++.exe -o Android/Debug/JNIApi.o -marm -fno-strict-aliasing -funswitch-loops -finline-limit=100 -fomit-frame-pointer -fno-exceptions -fpic -fstack-protector -fno-rtti -fno-short-enums -x c++ -Wno-psabi -IE:/ADT/ndk/platforms/android-19/arch-arm/usr/include -IE:/ADT/ndk/sources/cxx-stl/gnu-libstdc++/4.8/include -IE:/ADT/ndk/sources/cxx-stl/gnu-libstdc++/4.8/libs/armeabi-v7a/include …
Run Code Online (Sandbox Code Playgroud)

c++ android-ndk

1
推荐指数
1
解决办法
5428
查看次数

将对象推送到向量时,为什么会破坏对象?

我想知道为什么我推回到向量中的元素会在这种情况下调用它的析构函数:

#include <iostream>
#include <vector>

class Foo
{
public:
    Foo(int a)
     : m_a(a)
    {
        std::cout << "Foo ctor() " << m_a << std::endl;
    }
    ~Foo()
    {
        std::cout << "Foo dtor() " << m_a << std::endl;
    }
private:
    int m_a;
};

class FooStorage
{
public:
    static void createFoo(int a)
    {
        m_foos.push_back(Foo(a));
    }

    static std::vector<Foo> m_foos;
};

std::vector<Foo> FooStorage::m_foos;

int main()
{
    std::cout << "Before: " << FooStorage::m_foos.size() << std::endl;
    FooStorage::createFoo(53);
    std::cout << "After: " << FooStorage::m_foos.size() << std::endl;

    return 0;
} …
Run Code Online (Sandbox Code Playgroud)

c++

1
推荐指数
1
解决办法
324
查看次数

Java Regex:只允许某些字符,但是不允许某些字符开始字符串?

我正在摆弄Java正则表达式,我试图提出一种模式,允许在任何其他地方使用某些字符集,但它不能以允许集中的某些字符开头.

例如,假设允许的字符是从A到Z,但字符串不能以X或Z开头.我该怎么做?我想出来了^[XZ][^A-Z]+,虽然它起作用,但它允许字符串以不在集合中的其他字母开头(例如用标点符号).

java regex string

1
推荐指数
1
解决办法
671
查看次数

foo :: bar :: baz()在C++中的含义是什么?

一个简单的问题:如果我有这样的一行:

int foo::bar::baz() {...
Run Code Online (Sandbox Code Playgroud)

这怎么解释?baz()是函数名,但是'foo'是类还是名称空间?'bar'是一个类还是一个子类还是什么?

c++

0
推荐指数
1
解决办法
713
查看次数

分号内的函数括号?

我正在浏览Psycle源代码,这条线引起了我的注意:

bool user_choose_dialog(HWnd const window_handle,
                        format const * const source_format = 0,
                        format const * const proposed_format = 0;
                        std::string const & caption = "");
Run Code Online (Sandbox Code Playgroud)

如您所见,参数列表中有分号.我注意到有一个老问题,答案是分号用于前向声明.但是,在这种情况下,我看不到前瞻性声明.那个分号的含义是什么?它是有意义的和功能性的,还是来源中的拼写错误?

c++ parameters function

0
推荐指数
1
解决办法
194
查看次数

指针类型是"阻止"在声明中调用构造函数的唯一方法吗?

我试图搜索这个,但我没有找到任何答案.我们来看看这个:

class Foo
{
    Foo();
    Bar a; // 'a', the object, gets created (even if I don't want to!)
    Bar* b; // 'b', the pointer, gets created, but the object doesn't
}

Foo::Foo()
{
    a = a(); // I'd like to create 'a' here instead. This just "replaces" it
    b = new Bar(); // 'b', the object, gets created
}
Run Code Online (Sandbox Code Playgroud)

我的问题是:我可以声明一个对象而不创建它吗?或者我总是要使用指针?

c++ pointers

0
推荐指数
1
解决办法
38
查看次数

is_authenticated对于注销的用户返回True

我正在使用Django,Django REST框架,Django-rest-auth和Django-allauth编写服务器应用程序。我有一种用于在用户之间传递消息的方法,只有在接收者登录后才应该发生这种情况。

但是,is_authenticated()即使用户已注销(似乎叫rest-auth/logout/,该对象又应调用Django的注销),该用户对象的方法似乎仍返回True 。是什么原因造成的?我在这里想念什么吗?

这是我的代码:

class SendMessage(generics.CreateAPIView):
    permission_classes = (permissions.IsAuthenticated,)
    serializer_class = MessageSerializer

    def perform_create(self, serializer):
        m = self.request.data['msg']
        targetUser = User.objects.get(pk = self.request.data['user'])

        if targetUser.is_authenticated():
            # Send message
        else:
            # Don't send message
Run Code Online (Sandbox Code Playgroud)

authentication django django-rest-framework django-allauth django-rest-auth

0
推荐指数
1
解决办法
895
查看次数

使用const char*初始化std :: string导致乱码

我使用rapidjson来读取JSON文件,其中一些值是字符串.现在,rapidjson的GetString()方法返回一个const char *.不过,我想把它存储起来std::string.我试过这个:

const char* foo = d["foo"].GetString();
printf("Foo: %s\n", foo); // Prints correctly

std::string fooStr(foo);
printf("FooString: %s\n", fooStr); // Gibberish
Run Code Online (Sandbox Code Playgroud)

我如何得到正确的std::string

c++ string

-4
推荐指数
1
解决办法
860
查看次数