我何时应该使用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)
哪个更快?
我正在尝试使用 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)
它在我运行应用程序时有效,但是当我运行测试时, …
使用之间有什么不同:
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)
哪个更好?
我想做一个以弧度或度数初始化的角度类,但我不知道这是不是一个好主意.我在考虑这样的事情:

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++ 11反向范围的for循环
是否有一个反基于范围for的C++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)
有可能做这样的事情做一个反向循环吗?
如果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) 采用这种结构:
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)
我认为第二种情况更快,但我不确定,因为我不知道编译器将如何处理它.你能告诉我什么吗?
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++ ×6
android ×2
c++11 ×1
casting ×1
compilation ×1
cursor ×1
dagger ×1
dagger-hilt ×1
iostream ×1
kotlin ×1
math ×1
performance ×1
sizeof ×1
stl ×1
string ×1
stringstream ×1
trigonometry ×1