如果我有以下程序:
#include <vector>
#include <set>
template<class T, class U>
void AddToContainer(T& container, U value)
{
container.push_back(value);
}
int main(char**, int)
{
std::vector<int> v;
AddToContainer(v, 1);
std::set<int> s;
AddToContainer(s, 1);
return 0;
}
Run Code Online (Sandbox Code Playgroud)
如何将容器添加为通用?由于std::set没有一个push_back但只有insert,这将无法编译.
在以下情况中我似乎找到了很多代码:
class Thing
{
public:
Thing() = default;
};
class Repo
{
public:
Repo()
{
// Makes things..
mThings.emplace_back( std::make_unique<Thing>() );
}
// I find I need functions like this,
// a function which may return some record,
// or might return nullptr if there is no record.
Thing* GetThing(int id)
{
// Might return nullptr, or might return
return mThings[0].get();
}
private:
std::vector<std::unique_ptr<Thing>> mThings;
};
Run Code Online (Sandbox Code Playgroud)
Repo用于获取a 的对象Thing不拥有它,所以如果在这种情况下使用原始指针是否可接受?
将它强制为a std::shared_ptr并返回a 似乎是错误的,std::weak_ptr因为调用者知道如果GetThing不返回nullptr …
所以我有一些代码:
class Thing
{
public:
Thing() = default;
};
class DbOfThings
{
public:
DbOfThings() = default;
std::weak_ptr<Thing> GetEntry(int someKey) const
{
// returns a weak_ptr from mThings, or null if a Thing
// that has someKey was not found
return std::weak_ptr<Thing>(nullptr);
}
private
// Idea is that this object owns these things, but other objects can grab a thing from here - but the thing can be killed off at any time
std::vector<std::share_ptr<Thing>> mThings;
Run Code Online (Sandbox Code Playgroud)
但这无法编译:
参数1从'std :: nullptr_t'到'const std :: …
如果我使用Qt执行以下操作:
重复步骤2到5会导致最终图像失去颜色 - 它似乎变得更暗和更暗.
这是我的转换功能:
qRgb RGB565ToRGB888( unsigned short int aTextel )
{
unsigned char r = (((aTextel)&0x01F) <<3);
unsigned char g = (((aTextel)&0x03E0) >>2);
unsigned char b = (((aTextel)&0x7C00 )>>7);
return qRgb( r, g, b, 255 );
}
unsigned short int RGB888ToRGB565( QRgb aPixel )
{
int red = ( aPixel >> 16) & 0xFF;
int green = ( aPixel >> 8 ) & 0xFF;
int blue = aPixel & 0xFF;
unsigned …Run Code Online (Sandbox Code Playgroud) 类似的问题:
BOOL bShowLoadingIcon = FALSE;
if (sCurrentLevelId_5C3030 == 0 || sCurrentLevelId_5C3030 == 16 || (bShowLoadingIcon = TRUE, sCurrentLevelId_5C3030 == -1))
{
bShowLoadingIcon = FALSE;
}
Run Code Online (Sandbox Code Playgroud)
在上面的代码示例中,sCurrentLevelId_5C3030的值/范围将导致bShowLoadingIcon设置为TRUE.它是否有可能被设置为TRUE并且也变为真(如果表达式整体)因此也被设置为FALSE?
我不知道(bShowLoadingIcon = TRUE, sCurrentLevelId_5C3030 == -1)实际上在做什么.