小编Luc*_*ima的帖子

什么时候应该使用string而不是stringstream?

我何时应该使用stringstream而不是string::append()?假设我要安排字符串.

stringstream ss;
ss << str1 << "str2" << ...
Write(ss.str());
Run Code Online (Sandbox Code Playgroud)

要么:

string str;
str.reserve(10000);
str.append(str1);
str.append("str2");
...
Write(str);
Run Code Online (Sandbox Code Playgroud)

哪个更快?

c++ string performance iostream stringstream

26
推荐指数
2
解决办法
2万
查看次数

Dagger Hilt 未在测试中注入

我正在尝试使用 Dagger Hilt 和 Robolectric 运行测试:

@RunWith(RobolectricTestRunner::class)
@UninstallModules(LevelModule::class, AppModule::class)
@Config(sdk = [16, 28], application = HiltTestApplication::class)
@LooperMode(LooperMode.Mode.PAUSED)
@HiltAndroidTest
class LevelFragmentTest {

    @get:Rule
    var rule = HiltAndroidRule(this)

    @Test
    fun testShowGameOverWhenTapAMine() {
        launchActivity<GameActivity>().onActivity { activity ->
            ShadowLooper.runUiThreadTasks()
            ...
        }
    }
Run Code Online (Sandbox Code Playgroud)

测试失败,GameActivity.onCreate因为带有 @Inject 的 GameActivity 的所有字段都为空。

游戏活动是:

@AndroidEntryPoint
class GameActivity : AppCompatActivity()
Run Code Online (Sandbox Code Playgroud)

这些模块是:

@Module
@InstallIn(ActivityComponent::class)
open class LevelModule { ... } 
Run Code Online (Sandbox Code Playgroud)
@Module
@InstallIn(ActivityComponent::class)
class TestLevelModule {
Run Code Online (Sandbox Code Playgroud)
@Module
@InstallIn(ApplicationComponent::class)
class AppModule() { ... } 
Run Code Online (Sandbox Code Playgroud)
@Module
@InstallIn(ApplicationComponent::class)
class TestAppModule() { ... } 
Run Code Online (Sandbox Code Playgroud)

它在我运行应用程序时有效,但是当我运行测试时, …

android kotlin dagger dagger-hilt

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

全局静态变量vs函数中的静态变量?

使用之间有什么不同:

static Foo foo;
// ...
foo.func();
Run Code Online (Sandbox Code Playgroud)

和:

Foo& GetFoo(void) 
{
    static Foo foo;
    return foo;
}

// ...

GetFoo().func();
Run Code Online (Sandbox Code Playgroud)

哪个更好?

c++

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

弧度和度数的角度等级

我想做一个以弧度或度数初始化的角度类,但我不知道这是不是一个好主意.我在考虑这样的事情:

类图

class Radian;
class Degree;

/**
  * My angle class.
  **/
class Angle
{
    private:
        float m_radian;

    public:
        explicit Angle(const Radian& rad);

        explicit Angle(const Degree& deg);

        float GetRadian(void) const
        {
            return this->m_radian;
        }

        float GetDegree(void) const
        {
            return ToDegree(this->m_radian);
        }

        bool IsEqual(const Angle& angle, float epsilon = 0.001f) const
        {
            return Abs<float>(this->m_radian - angle.m_radian) < epsilon;
        }

        void Set(const Angle& ang);

    protected:
        Angle(void) : m_radian(0.0f)
        {}

        Angle(float rad) : m_radian(rad)
        {}
};

class Radian : public Angle
{
    public:
        Radian(void) …
Run Code Online (Sandbox Code Playgroud)

c++ math trigonometry

6
推荐指数
3
解决办法
5039
查看次数

在C++ 11中有一个基于逆范围的?

可能重复:
基于C++ 11反向范围的for循环

是否有一个反基于范围forC++11

我想做这样的事情:

for(int value : vec)
{
    cout << value << endl;
}
Run Code Online (Sandbox Code Playgroud)

去做这个:

for(auto it = vec.rbegin(); it != vec.rend(); ++it)
{
    cout << *it << endl;
}
Run Code Online (Sandbox Code Playgroud)

例如:

for(int value : -vec)
{
    cout << value << endl;
}    
Run Code Online (Sandbox Code Playgroud)

有可能做这样的事情做一个反向循环吗?

c++ stl c++11

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

如果moveToFirst()为false,是否需要关闭Cursor?

如果cursor.moveToFirst()返回false,我还需要关闭吗?因为当光标为空时它返回false.

我怀疑正确的方法:

if (cursor != null && cursor.moveToFirst()) {
    // some code
    cursor.close();
}
Run Code Online (Sandbox Code Playgroud)

要么:

if(cursor != null) {
    if (cursor.moveToFirst()) {
        // some code
    }

    cursor.close();
}
Run Code Online (Sandbox Code Playgroud)

android cursor

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

reinterpret_cast一个对象

采用这种结构:

struct Foo
{
    float m_foo;
    // no other member
};

// A Foo object.
Foo f;
Run Code Online (Sandbox Code Playgroud)

哪个更贵?

float result = std::sin(f.m_foo);
Run Code Online (Sandbox Code Playgroud)

要么

float result = std::sin(*(reinterpret_cast<float*>(&f)));
// f can be interpreted like float in this case
Run Code Online (Sandbox Code Playgroud)

我认为第二种情况更快,但我不确定,因为我不知道编译器将如何处理它.你能告诉我什么吗?

c++ casting compilation reinterpret-cast

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

8 bool vs 1 int

A bool在C++中占用1个字节.但是,为什么bool[8]需要8个字节而不是1个字节?一个字节有足够的空间容纳8位.

我使用-Os标志用GCC编译了这个:

#include <iostream>

using namespace std;

class Foo
{
    public:
        bool m_bool[8];
};

int main ()
{
    cout << "Size: " << sizeof(Foo) << " byte(s) " << endl;

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

它返回"Size:8 byte(s)".

有没有办法优化它?

c++ sizeof

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