我正在开发一个自定义库.到目前为止,我已将std :: tuple和std :: tie包装到我自己的myOwn :: Tuple和myOwn :: tie中.它们的功能与std :: tuple和std :: tie相同.我想用std :: ignore做类似的事情.
到目前为止,我写了以下内容:
namespace myOwn
{
auto
tilde()
-> decltype(std::ignore)
{
return std::ignore;
}
}
Run Code Online (Sandbox Code Playgroud)
我唯一的问题是我现在必须使用带圆括号的myOwn :: tilde().我希望能够将其用作myOwn :: tilde.到目前为止,我在std :: ignore上读到的只是如何使用它,
在这里:C++:使用std :: ignore返回std :: tie的类型
我试过用了
typedef std::ignore tilde
Run Code Online (Sandbox Code Playgroud)
但这没有成功.任何帮助,将不胜感激.这个问题可能对试图包装对象的其他人有用.
我有一个对象的矢量,我想删除一些对象,而无需重新排序.
我在这里找到了一些解决方案,但这些解决方案基于将vector元素与值进行比较.[ 如何从排序的向量中有效地擦除值?
但是,我需要根据条件语句进行擦除,所以我不认为我可以按照它们的方式使用这些函数.
在这个例子中,我有一个3D矢量矢量我需要删除所有Z值小于0的元素;
我现在拥有的是另一个从原始矢量创建的矢量:
for (int i = 0; i < original_vectors.size(); i++)
if (original_vectors[i].z > 0)
new_vectors.push_back(original_vectors[i]);
Run Code Online (Sandbox Code Playgroud)
如果简单地删除没有Z> 0的元素,我该怎么做?
我运行Mac OSX El Capitan,我已通过Homebrew gcc版本5.3.0安装.
我想安装pyopencl(但据我所知并不重要)并且在运行以下命令时:
gcc -fno-strict-aliasing -fwrapv -Wall -O3 -DNDEBUG -DPYGPU_PACKAGE = pyopencl -DPYGPU_PYOPENCL = 1 -Isrc/c_wrapper/-I/Users/earendilllock/anaconda/include/python2.7 -c build/temp.macosx- 10.5-x86_64-2.7/pyopencl._cffi.cpp -o build/temp.macosx-10.5-x86_64-2.7/build/temp.macosx-10.5-x86_64-2.7/pyopencl._cffi.o -std = c ++ 0x - stdlib = libc ++ -mmacosx-version-min = 10.7 -arch i386 -arch x86_64 -isysroot /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.11.sdk
我收到了以下错误:
gcc: error: unrecognized command line option '-stdlib=libc++'
error: command 'gcc' failed with exit status 1
make: *** [all] Error 1
Run Code Online (Sandbox Code Playgroud)
我找不到通过谷歌解决这个问题的解决方案,但我希望它存在.
我有一个向量,其中包含按排序顺序的无符号整数.现在我想对向量的二进制搜索应用从向量的特定位置到它的末尾.
例如,我的矢量是:
vector<unsigned> vec;
vec.push_back(1); vector.push_back(2), vector.push_back(3);
vector.push_back(4);
vector.push_back(5); vector.push_back(6); vector.push_back(7); vector.push_back(8);
vector.push_back(9); vector.push_back(10); vector.push_back(11); vector.push_back(12);
Run Code Online (Sandbox Code Playgroud)
现在,我想在"vec"上应用二进制搜索,从第6个位置开始到向量的大小,即我想仅对向量进行二分搜索:从pos:6开始到pos:11.
我知道我可以在整个向量上应用std:lower_bound(vec.begin(),vec.end(),9),但是我无法理解如何将它应用于向量的一部分.我想在矢量vec中找到"9"的位置.我非常感谢你对此有任何帮助
我有一个编译的代码,它看起来像:
void test1(const std::string &name)................
test1("myName");
Run Code Online (Sandbox Code Playgroud)
但是,如果我const从声明中删除:
void test2(std::string &name)................
test2("myName");
Run Code Online (Sandbox Code Playgroud)
代码只是不编译.
为什么要const std::string编译?据我所知,将调用相同的隐式构造函数将char *其转换为a std::string.
我正在使用Visual Studio 2013,以防这可能与答案有关.
以下代码
#include <vector>
void main() {
std::vector<int> testing;
testing.push_back(45);
}
Run Code Online (Sandbox Code Playgroud)
在我构建之前就向我吐出一堆错误,奇怪的是,我的文件main.cpp没有任何错误,这些错误来自vector.h内部(以及我尝试使用的几乎所有其他std库) )。此代码在Mac上的xcode中工作正常,但在Windows上的Visual Studio 2015中则无法正常工作。我对Windows上的编码不太熟悉,所以我遗漏了一些明显的东西吗?
这是大多数错误的样子(有成百上千个这样的错误)
Error (active) the global scope has no "float_t" Test c:\Program Files (x86)\Microsoft Visual Studio 14.0\VC\include\cmath 688
Error (active) the global scope has no "double_t" Test c:\Program Files (x86)\Microsoft Visual Studio 14.0\VC\include\cmath 688
Error (active) the global scope has no "nan" Test c:\Program Files (x86)\Microsoft Visual Studio 14.0\VC\include\cmath 700
Error (active) the global scope has no "acoshf" Test c:\Program Files (x86)\Microsoft Visual Studio 14.0\VC\include\cmath 704
Error …Run Code Online (Sandbox Code Playgroud) 是否可以使用std::find()1次呼叫搜索多个元素?例:std::find(vector.begin(), vector.end(), 0&&1&&D)
谢谢
我使用的是第三方API,其中包含一个包含一组typedef的头文件.在过去的4年中,对某些typedef进行了微小的更改(例如,在unsigned/signed之间切换,从int更改为long等).
我想在我的代码中添加编译时检查,以便我知道特定的typedef是否已更改.我想添加如下内容:
#if !std::is_same<::ApiType, int>::value
#error Type has changed
#endif
Run Code Online (Sandbox Code Playgroud)
当我在各种typedef中尝试这个时,我发现总是抛出编译错误.
我设置了一个小的控制台程序,它显示了同样的问题(即对于预处理器使用总是假的)但在预处理器之外很好:
#include "stdafx.h"
#include <Windows.h>
#include <type_traits>
int main()
{
#if std::is_same<int, int>::value
const auto aa = 14; // omitted
#else
const auto bb = 17;
#endif
#if std::is_same<::DWORD, int>::value
const auto cc = 14; // omitted
#else
const auto dd = 17;
#endif
const auto a = std::is_same<int, int>::value; // true
const auto b = std::is_same<::DWORD, int>::value; // false
const auto c = std::is_same<::DWORD, unsigned long>::value; // true …Run Code Online (Sandbox Code Playgroud) 是否有任何特殊原因std::vector没有会员功能find?相反,你必须打电话给std::find(和#include <algorithm>).
我之所以要问的是,我认为能够在某些实现中更改容器类是一件好事,而无需在访问容器的任何地方更改代码.假设我将std::vector实现使用的位置替换std::find为std::map.然后我还必须std::find用调用成员替换调用find,除非我想保持线性复杂性std::find.为什么不只是find为所有容器类都有一个成员,它找到一个最适合每个容器的算法的元素?
我有一些std :: thread的类warper.这是构造函数:
template <typename Function, typename... Args>
InterruptibleThread(Function&& fun, Args&&... args)
{
_thread = std::thread([](std::atomic_bool * f, Function&& function, Args&&... arguments)
{
_flag_ref = f;
(function)(std::forward<Args>(arguments)...);
},
&_flag,
std::forward<Function>(fun)
, std::forward<Args>(args)...
);
}
Run Code Online (Sandbox Code Playgroud)
然后我正在使用它(例子):InterruptibleThread(&SourceImageList :: StartFrameProcessingStatic,this,std :: ref(it))
编译器成功构建此代码.但现在我想制作一个这样的对象的矢量:
std::vector<InterruptibleThread> grp;
Run Code Online (Sandbox Code Playgroud)
我想在堆栈上分配它,所以我正在做的是:
grp.emplace_back(&SourceImageList::StartFrameProcessingStatic, this, std::ref(it));
Run Code Online (Sandbox Code Playgroud)
我收到这个错误:
C2064 term does not evaluate to a function taking 0 arguments
Run Code Online (Sandbox Code Playgroud)
以下是编译器验证的选项:
1) grp.push_back(new InterruptibleThread(&SourceImageList::StartFrameProcessingStatic, this, std::ref(it)));
2) grp.push_back(InterruptibleThread(&SourceImageList::StartFrameProcessingStatic, this, std::ref(it)));
Run Code Online (Sandbox Code Playgroud)
但第一个是在堆上分配一个对象,所以我需要手动释放它,第二个是对象的副本.
我可以emplace_back在这里使用(编译器是MSVC 2015更新3)吗?
更新
好的,我根据答案做了一些修复.这是这个类的最终版本:
#pragma once
#include <exception>
#include …Run Code Online (Sandbox Code Playgroud)