此代码编译并执行.我知道在第一种情况下我们有未定义的行为.但在第二种情况下究竟发生了什么?
#include <string>
#include <iostream>
#include <cstdio>
std::string foo() {
return "HELLO";
}
void bar(const char *p) {
std::printf("%s\n", p);
}
int main() {
// FIRST CASE:
// I know this is bad, because after the assignment
// the variable returned by foo() is destroyed and we
// have a bad reference.
const std::string &s = foo();
bar(s.c_str());
// SECOND CASE:
// But what about that ? I don't know exactly if the
// object is alive after the call …Run Code Online (Sandbox Code Playgroud) std::unique_ptr有2个模板参数,第二个是要使用的删除器.由于这个事实,可以通过以下方式轻松地unique_ptr将a替换为类型,这需要自定义删除器(例如SDL_Texture):
using SDL_TexturePtr = unique_ptr<SDL_Texture, SDL2PtrDeleter>;
Run Code Online (Sandbox Code Playgroud)
...哪里SDL2PtrDeleter是一个用作删除器的仿函数.
鉴于这个别名,程序员能够构建和重置,SDL_TexturePtr而无需关心或甚至不知道自定义删除器:
SDL_TexturePtr ptexture(SDL_CreateTexture(/*args*/));
//...
ptexture.reset(SDL_CreateTexture(/*args*/));
Run Code Online (Sandbox Code Playgroud)
std::shared_ptr另一方面,没有模板参数,这将允许将删除器指定为类型的一部分,因此以下是非法的:
// error: wrong number of template arguments (2, should be 1)
using SDL_TextureSharedPtr = shared_ptr<SDL_Texture, SDL2PtrDeleter>;
Run Code Online (Sandbox Code Playgroud)
因此,对类型别名最好的方法是:
using SDL_TextureSharedPtr = shared_ptr<SDL_Texture>;
Run Code Online (Sandbox Code Playgroud)
但是与shared_ptr<SDL_Texture>明确使用相比,这几乎没有什么优势,因为用户必须知道要使用的删除函数,并在每次构造或重置时指定它SDL_TextureSharedPtr:
SDL_TextureSharedPtr ptexture(SDL_CreateTexture(/*args*/), SDL_DestroyTexture);
//...
ptexture.reset(SDL_CreateTexture(/*args*/), SDL_DestroyTexture);
Run Code Online (Sandbox Code Playgroud)
从上面的示例中可以看出,用户需要知道要删除的正确函数SDL_Texture(即SDL_DestroyTexture()),并且每次都将指针传递给它.除了不方便之外,这也会产生程序员通过将错误的函数指定为删除器而引入错误的可能性较小的概率.
我想以某种方式将删除器封装在共享指针本身的类型中.由于我无法通过使用类型别名来实现这一点,因此我考虑了3个选项:
创建一个包装类,std::shared_ptr<T>它将复制接口,shared_ptr但允许通过自己的模板参数指定删除器仿函数.然后,operator()当从其自己的构造函数或方法调用reset()其底层std::shared_ptr<T>实例的构造函数或reset()方法时,此包装器将提供指向其删除器实例的指针.当然,缺点是std::shared_ptr必须在这个包装类中重复整个相当大的接口,即WET.
创建一个子类std::shared_ptr<T>,允许通过自己的模板参数指定一个删除函子.假设public …
这是一个玩具示例。我想在其中搜索a并提取b. 即使颜色不是以大写字母开头,我也想提取它。但是,输出应该告诉我颜色在a.
所以我想得到的答案是#"Red" NA "blue。
a <- "She has Red hair and blue eyes"
b <- c("Red", "Yellow", "Blue")
str_extract(a, b)#"Red" NA NA
Run Code Online (Sandbox Code Playgroud)
我str_extract从 'stringr' 开始使用,但很乐意使用另一个函数/包(例如,grep)。
我做了这样的事情,它有效:
let s = " \"".as_bytes();
let (space, quote) = (s[0], s[1]);
Run Code Online (Sandbox Code Playgroud)
我想做这样的事情
&[space, quote] = " \"".as_bytes();
Run Code Online (Sandbox Code Playgroud)
但它给了我错误
let s = " \"".as_bytes();
let (space, quote) = (s[0], s[1]);
Run Code Online (Sandbox Code Playgroud)
有没有可能做类似的事情?
我有一个对象 A 和 B。当我点击对象 B 时,A 旋转到对象 B(A 面对 B)。B不面对A。我需要做的是:当A面对B时,我需要A面对相反的方向。我有 A 旋转看 B 的代码。之后如何旋转到相反的方向?
Vector3 targetDirection = target - transform.position;
float step = speed * Time.deltaTime;
Vector3 newDirection = Vector3.RotateTowards (turretDome.transform.forward, targetDirection, step, 0.0F);
turretDome.transform.rotation = Quaternion.LookRotation (newDirection);
Run Code Online (Sandbox Code Playgroud) 当使用集合时,我想传递我自己的比较函数,它也需要一个参数.如何做到这一点以及如何将该集作为参考传递给其他函数?
例如,我有Comparator类(带有operator()重载和私有默认构造函数),它在构造函数中接受一个参数,在进行比较时使用.这与排序算法,如,
sort(myVector.begin(), myVector.end(), Comparator(10));
Run Code Online (Sandbox Code Playgroud)
但是如何使用带参数的比较器对象声明一个集合.
我试过这些语法,
std::set< MyObj, bool(*)(const MyObj&, const MyObj&)> myObjSet(Compatrator(100));
Run Code Online (Sandbox Code Playgroud)
现在当我插入时myObjSet.insert(MyObj(0)),它会给出错误Error: "left of '.insert' must have class/struct/union"
现在,当我宣布设置为,
std::set< MyObj, Comparator(int)> myObjSet;
Run Code Online (Sandbox Code Playgroud)
它给出了"函数返回函数"的错误.
class Comparator
{
int m_cmpParameter;
Comparator();
public:
~Comparator();
Comparator(int pCmpParam):m_cmpParameter(pCmpParam){}
bool operator()(const MyObj& pObjA, const MyObj& pObjB);
};
Run Code Online (Sandbox Code Playgroud)
在我的项目中,我有一个类用于STL容器所需的所有比较.如何将此Comparator类与Set一起使用并将参数传递给比较对象?我如何将此集作为对其他函数的引用传递,更具体地说,函数签名应该是什么?
谢谢.
c++ ×3
c++11 ×2
c++14 ×1
containers ×1
direction ×1
extract ×1
matching ×1
r ×1
rotation ×1
rust ×1
shared-ptr ×1
stl ×1
string ×1
unique-ptr ×1
vector ×1