小编pau*_*ulm的帖子

C++泛型插入到std容器中?

如果我有以下程序:

#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,这将无法编译.

c++ containers c++11

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

当有一个所有者(std :: unique_ptr)但是其他对象需要对象的"句柄"时,C++ 11应该使用原始指针吗?

在以下情况中我似乎找到了很多代码:

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 …

pointers c++11

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

为什么我不能返回nullptr std :: weak_ptr?

所以我有一些代码:

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 :: …

c++ shared-ptr c++11

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

为什么多次将RGB888转换为RGB565会导致颜色损失?

如果我使用Qt执行以下操作:

  1. 在QImage :: Format_RGB32中加载位图.
  2. 将其像素转换为RGB565(没有Qt格式,所以我必须"手动").
  3. 创建一个与步骤1中加载的位图大小相同的新位图.
  4. 将RGB565缓冲区像素转换回RGB88,进入步骤3中创建的图像.
  5. 从步骤4创建的图像看起来像步骤1中的图像,但是如果比较RGB值,它们就不完全相同.

重复步骤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)

c++ qt

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

C++逗号运算符混淆

类似的问题:

如何在这里使用逗号运算符?

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)实际上在做什么.

c++

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

标签 统计

c++ ×4

c++11 ×3

containers ×1

pointers ×1

qt ×1

shared-ptr ×1