假设我想用vector<int>,测试一些东西vector<bool>,vector<string>.我想写这样的东西:
for(type T in {int, bool, string}){
vector<T> v;
for(int i = 0; i < 3; ++i){
v.push_back(randomValue<T>());
}
assert(v.size() == 3);
}
Run Code Online (Sandbox Code Playgroud)
我知道语言中没有这样的功能,但有可能以某种方式模仿吗?例如,某些库中是否存在此功能boost?
我们假设T是可移动的对象:
vector<T> v;
v.resize(...)
Run Code Online (Sandbox Code Playgroud)
如果需要重新分配,那么该代码是否会调用副本,或者在所有元素上移动构造函数?
如果答案是"移动构造函数",那么编译器如何知道它应该使用这个?
我无法${static_library}使用CMake 将库静态链接.我已尝试对路径进行硬编码,据我所知,TARGET_LINK_LIBRARIES应自动将其链接起来.
ADD_LIBRARY(libraryA STATIC ${source_files})
TARGET_LINK_LIBRARIES(libraryA debug ${static_library})
Run Code Online (Sandbox Code Playgroud)
有一个子项目,libraryA后来在构建中链接
ADD_EXECUTABLE(testA ${test_sources})
TARGET_LINK_LIBRARIES(testA libraryA)
Run Code Online (Sandbox Code Playgroud)
其中$ {static_library}正确链接但我需要${static_library}直接链接到libraryA.
如何在构造函数初始化列表中启用ADL?例如,假设我有一个bignum具有命名空间级abs功能.现在我想编写一个类Foo,用传递给构造函数的实例的绝对值初始化其成员; 它应该使用命名空间级别,abs如果它存在,std::abs否则:
template<typename T>
struct Foo
{
T _data;
Foo(T data):
_data(abs(data)) // I want find abs with ADL here
{}
};
Run Code Online (Sandbox Code Playgroud)
无论如何,在类范围内禁止使用声明,我不想"污染"命名空间.如何启用ADL以使其在构造函数初始化列表中工作?
如何将元素从C++ 11容器移动到容器的后面?
我特意使用列表,但如果需要可以使用向量(或者可能是任何容器;最好获得通用容器解决方案).
我有一个元素列表.我有一个价值.我将此值传递给每个元素; 其中一人会回应.第一个响应我要移动到容器的后面.
我现有的代码如下所示:
std::list<elements> m_elements
...
bool handle(event e)
{
for(auto &element : m_elements)
{
if(element->handle())
return true;
}
return false;
}
Run Code Online (Sandbox Code Playgroud)
但我现在想要做的是将handle()事件的元素移动到结尾m_elements.对于一些上下文,我正在研究Windows的GUI系统.因此,无论哪个窗口捕获鼠标,都需要在绘制时跳到顶部; m_elements正在逐一绘制窗口(因此最后一个元素位于顶部).
值得注意的是元素是std::shared_ptrs.
如果有人遇到这个问题并回答类似的情况.
我实际上需要向后遍历我的容器以处理输入,因为最后一个元素是最后绘制的,即它位于顶部,因此它应该是应该尝试拦截输入的第一个元素.
这很简单.但是,将反向迭代器拼接到容器的末端有点棘手.std::reverse_iterator.base()如果迭代器是前向迭代器,则返回NEXT迭代器,因此需要std::advance迭代器-1才能成功移动"截获"的实际元素.
for(auto itr = m_elements.rbegin(); itr != m_elements.rend(); ++itr)
{
if((*itr)->handle(e))
{
auto itr2 = itr.base();
std::advance(itr2,-1);
m_elements.splice(m_elements.end(), m_elements, itr2);
return true;
}
}
Run Code Online (Sandbox Code Playgroud)
希望这有助于下一个可怜的灵魂.
我有一个大显示屏(大约 1000x2000 像素),我正在执行以下操作将图像绘制到屏幕上:
QImage *pImage = GetImage(); // from wherever
QPainter painter(this);
painter.drawImage((0,0), *pImage); // this line takes over 100ms to complete.
Run Code Online (Sandbox Code Playgroud)
我绘制的屏幕越大,绘制所需的时间就越长。我猜 pImage 正在被 memcpy'd,这就是区别。我怎样才能减少这种开销?我并不是想扩大规模或做任何事情。
谢谢。
由于可变参数模板,我前一段时间实现了C++等效的Python链函数.该函数用于连续迭代许多容器.这是使用名为的生成器的函数的旧工作版本ChainedObject,无论它是什么:
template<typename... Iterables>
auto chain(Iterables&&... iters)
-> ChainObject<Iterables...>
{
return /* ... */;
}
Run Code Online (Sandbox Code Playgroud)
相应的主要内容:
int main()
{
std::vector<int> vec = { 1, 2, 3, 4, 5 };
std::list<int> li = { 6, 7, 8, 9, 10, 11, 12, 13 };
for (auto& i: chain(vec, li))
{
// You can edit a range of iterables
// as if there was only one of them.
i *= 5;
std::cout << i << std::endl;
}
return 0;
}
Run Code Online (Sandbox Code Playgroud)
那个主要工作正常.我们不关心ChainObject中的问题,所以让我们看看它.我尝试使用模板模板来确保使用的不同集合具有相同value_type …
我可以使用我的g ++ 4.4指定-std = c ++ 0x进行编译,初始化程序列表是正确的,我可以使用它们(在c ++ 98中我不能)但在尝试使用auto关键字时仍然会出错:
std::list< std::vector<int> > li2;
li2.push_back({1, 2, 3}); //push_back vector
li2.push_back({4, 2, 6}); //again, vector implicitly
for (auto& vv : li2) {
for (auto &i : v)
printf("element: %d\n", 8);
}
Run Code Online (Sandbox Code Playgroud)
所以我假设我不能在g ++ 4.4中使用C++ 11函数.由于与CUDA的兼容性,我有4.4.
对于我的生活,我无法使用此代码.我试图在XNA框架的弃用之后将我的代码从C#转换为C++,但是一个顽固的方法不希望被转换.在C#中它是:
public Tile GetTileAtPosition(bool screenOrGame, Vector2 position)
{
if (screenOrGame)
{
return Array.Find(tileList, tile => tile.Position == position / 24);
}
else
{
return Array.Find(tileList, tile => tile.Position == position);
}
}
Run Code Online (Sandbox Code Playgroud)
在C++中,我试图用来代替这个的代码是:
Tile Level::GetTileAtPosition(bool screenOrGame, sf::Vector2f position)
{
vector<Tile>::iterator it;
if (screenOrGame)
{
it = find(tileList.begin(), tileList.end(), [position](const Tile &t) { return t.GetPosition() == sf::Vector2f(position.x / 24, position.y / 24); });
return Tile(it->GetID(), it->GetPosition().x, it->GetPosition().y);
}
else
{
it = find(tileList.begin(), tileList.end(), [position](const Tile& t) { return t.GetPosition() == …Run Code Online (Sandbox Code Playgroud)