我昨天开始学习正则表达式,在学习时,我看到 \s 用于空白字符。但是,由于某种原因,每当我输入空格时,C++ 中都不会检测到它。
代码:
#include <iostream>
#include <regex>
using namespace std;
int main() {
string str;
cin>>str;
regex e("a+\\s+b+");
bool found = regex_match(str,e);
if (found)
{
cout<<"Matched";
}
else
{
cout<<"No Match";
}
return 0;
}
Run Code Online (Sandbox Code Playgroud)
输入:ab
输出:不匹配
如果我用上面的代码替换\\s
并\\w
输入如下内容:
输入:azb
输出:匹配
我不明白为什么 \s 根本拒绝工作。我在网上浏览了这个问题的答案,但无法找到到底是什么原因造成的。
我使用带有 GNU/GCC 编译器的 CodeBlocks 16 IDE,在 Windows 上启用了 C++11 支持,在 IDEONE 上启用了 C++14 (GCC 5.1)。
任何帮助将非常感激。谢谢。
我正在尝试编译此代码:
#include <sstream>
std::stringstream foo() {
std::stringstream log;
log << "Hello there\n";
return log;
}
Run Code Online (Sandbox Code Playgroud)
GCC 4.9.2
给我以下错误(带-std=c++11
):
[x86-64 gcc 4.9.2] error: use of deleted function
'std::basic_stringstream<char>::basic_stringstream(const std::basic_stringstream<char>&)'
Run Code Online (Sandbox Code Playgroud)
这是一个例子.
既然std::stringstream
有move constructor
,为什么要调用复制构造函数,而不是移动构造函数?
注意:从GCC 5
代码编译正确:见这里.
假设我在编译时要执行一些操作:
enum class Action {A, B};
Run Code Online (Sandbox Code Playgroud)
现在我编写一个模板可变参数函数,它按顺序执行一个可能的动作组合:
template <Action a>
void applyAction();
template <typename = void>
void applyActions() {}
template <Action a, Action... as>
void applyActions() {
applyAction<a>();
applyActions<as...>();
}
Run Code Online (Sandbox Code Playgroud)
这段代码很好.确实:
void foo() {
applyActions<Action::A, Action::B>();
}
Run Code Online (Sandbox Code Playgroud)
正确生成:
call void applyAction<(Action)0>()
call void applyAction<(Action)1>()
Run Code Online (Sandbox Code Playgroud)
为了实现扩展包终止,我不得不声明虚函数:
template <typename = void> void applyActions() {}
Run Code Online (Sandbox Code Playgroud)
这对我来说非常"难看",因为它提供了调用泛型类型的可能性.
在C++ 11中,有没有办法声明一个接受空参数包的可变参数函数?
当然,它的声明不必带来所需函数的模糊性:
template <Action a, Action... as>
void applyActions();
Run Code Online (Sandbox Code Playgroud)
就像是:
template <Action.. = {}>
void applyActions() {}
Run Code Online (Sandbox Code Playgroud)
这是一个不可编译的例子,因为调用含糊不清.但它给出了我想要实现的目标.
c++ templates template-meta-programming variadic-templates c++11
根据valgrind
下面的代码并没有包含内存泄漏:
#include <memory>
#include <stdexcept>
namespace {
class Foo {
public:
Foo();
};
Foo::Foo() { throw std::runtime_error("This is an error"); }
} // anonymous namespace
int main(int argc, char* argv[]) {
try {
new Foo();
} catch (const std::exception& aError) {
return -1;
}
return 0;
}
Run Code Online (Sandbox Code Playgroud)
确实.结果valgrind --leak-check=full
是:
==25913== Memcheck, a memory error detector
==25913== Copyright (C) 2002-2017, and GNU GPL'd, by Julian Seward et al.
==25913== Using Valgrind-3.13.0 and LibVEX; rerun with …
Run Code Online (Sandbox Code Playgroud) 我有以下 Rust 模块。
test.rs
:
pub fn foo(xs: &[f32]) -> Vec<f32> {
xs.iter()
.flat_map(|x| xs.iter().map(|y| *x - y))
.collect()
}
Run Code Online (Sandbox Code Playgroud)
如果我编译这个模块(作为rlib),我会得到一个错误:
$ rustc -o test.s --crate-type rlib test.rs
error[E0373]: closure may outlive the current function, but it borrows `x`, which is owned by the current function
[...]
Run Code Online (Sandbox Code Playgroud)
也可以使用godbolt进行同样的实验(它的作用几乎相同)。
但是,如果我在二进制包中编译相同的函数。编译器接受此代码。
例如,通过使用rust-playground。
(请注意,我们在这里使用相同的编译器版本;即 1.63.0)
为什么编译器在这两种情况下表现不同?
我怀疑链接器可能会在这里启动,但我想得到更多的技术确认。
假设我有以下功能界面:
void giveme(void (*p)());
Run Code Online (Sandbox Code Playgroud)
该函数只接受指向函数的指针,该函数没有返回类型和参数.
我想知道是否存在一种方式(不改变界面)传递类方法作为该函数的参数.
我将尝试用一个例子更好地解释.我有一节课,比如:
class Foo {
public:
template<typename T>
void bar();
};
Run Code Online (Sandbox Code Playgroud)
我想传递bar<T>
(该类的可寻址实例)作为函数的参数giveme
.
我想用方法绑定方法,并获取函数目标.
就像是:
int main(int argc, char *argv[]) {
Foo foo;
std::function<void()> f = std::bind(&Foo::bar<int>, &foo);
giveme(f.target<void()>());
return 0;
}
Run Code Online (Sandbox Code Playgroud)
它编译,但显然不起作用,因为,从这里:
TargetType
应匹配目标类型,以便typeid(TargetType)==target_type()
.否则,该函数始终返回空指针.
那么,如果存在,有什么方法可以实现它?