我的regex_replace表达式在替换字符串中的'0'字符前使用组$ 1,如下所示:
#include <iostream>
#include <string>
#include <regex>
using namespace std;
int main() {
regex regex_a( "(.*)bar(.*)" );
cout << regex_replace( "foobar0x1", regex_a, "$10xNUM" ) << endl;
cout << regex_replace( "foobar0x1", regex_a, "$1 0xNUM" ) << endl;
}
Run Code Online (Sandbox Code Playgroud)
输出是:
xNUM
foo 0xNUM
Run Code Online (Sandbox Code Playgroud)
我试图在foo0xNUM没有中间空格的情况下获得输出.
如何保护替换字符串中下一个字符的组名$ 1?
我看到了在C++ 11/14中实现类型列表的两种可能的样式,我很好奇是否有任何理由偏爱另一种.这里概述了第一种技术,并在Boost的MPL库中进行了建模.在这种风格中,您可以定义元"自由函数"(使用声明的顶级),它们接收类型列表并对其进行操作.以下是如何实现std :: transform的元版本,它适用于类型而不是第一种样式中的值:
template <typename... Args>
struct type_list;
namespace impl
{
template <template <typename...> class F, class L>
struct transform_impl;
template <template <typename...> class F, template <typename...> class L, typename... T>
struct transform_impl<F, L<T...>>
{
using type = L<typename F<T>::type...>;
};
}
template <template <typename...> class F, class L>
using transform = typename impl::transform_impl<F, L>::type;
Run Code Online (Sandbox Code Playgroud)
第二种方式是定义元'方法'(使用类型列表结构中的声明).以下是变换在该样式中的外观:
template <typename... Args>
struct type_list {
// ... other 'methods'
template<template<class> class Wrapper>
using transform =
type_list<Wrapper<Args>...>;
// ... …Run Code Online (Sandbox Code Playgroud) 我使用外部库来处理大量数据.数据由原始指针传入,加上长度.该库不声明指针的所有权,但在完成数据时调用提供的回调函数(具有相同的两个参数).
通过使用方便地准备数据std::vector<T>,我宁愿不放弃这种便利.复制数据是完全不可能的.因此,我需要一种方法来"接管"一个拥有的内存缓冲区std::vector<T>,并且(稍后)在回调中释放它.
我目前的解决方案如下:
std::vector<T> input = prepare_input();
T * data = input.data();
size_t size = input.size();
// move the vector to "raw" storage, to prevent deallocation
alignas(std::vector<T>) char temp[sizeof(std::vector<T>)];
new (temp) std::vector<T>(std::move(input));
// invoke the library
lib::startProcesing(data, size);
Run Code Online (Sandbox Code Playgroud)
并且,在回调函数中:
void callback(T * data, size_t size) {
std::allocator<T>().deallocate(data, size);
}
Run Code Online (Sandbox Code Playgroud)
这个解决方案有效,因为标准分配器的deallocate函数忽略了它的第二个参数(元素计数)并且只是调用::operator delete(data).如果没有,可能会发生坏事,因为size输入向量可能比它小得多capacity.
我的问题是:是否有一种可靠的(以及C++标准)接管缓冲区std::vector并在以后"手动"释放它的方式?
考虑:
void g(int&);
void g(int&&);
template<class T>
void f(T&& x)
{
g(std::forward<T>(x));
}
int main()
{
f(10);
}
Run Code Online (Sandbox Code Playgroud)
由于id-expression x是一个左值,并且std::forward对于左值和右值有重载,为什么调用不能绑定到std::forward需要左值的重载?
template<class T>
constexpr T&& forward(std::remove_reference_t<T>& t) noexcept;
Run Code Online (Sandbox Code Playgroud) 标题说明了一切.为什么他们选择没有
namespace std
{
std::string to_string(const std::string&)
}
Run Code Online (Sandbox Code Playgroud)
超载?
我有一个程序,可以按行索引或行名查询一些数据; 非常适合模板.直到我尝试构造一个错误消息,如果正在访问的行丢失:
template <typename T>
int read(T nameOrIndex)
{
if (!present(nameOrIndex))
{
// This does not compile if T is std::string.
throw std::invalid_argument("Missing row: " + std::to_string(nameOrIndex));
}
}
Run Code Online (Sandbox Code Playgroud)
我可以将自己的重载添加到std命名空间,但这并不理想.
参见,例如, http ://en.cppreference.com/w/cpp/container/map/erase
在C++ 03中有三个重载:
void erase( iterator pos );
void erase( iterator first, iterator last );
size_type erase( const key_type& key );
Run Code Online (Sandbox Code Playgroud)
在C++ 11中,第一和第二过载改变为采取const_iterator,使得它们可与任一被调用iterator或const_iterator.第一次重载也通过在删除之后将迭代器返回到元素来改进:
iterator erase( const_iterator pos );
void erase( const_iterator first, const_iterator last );
size_type erase( const key_type& key );
Run Code Online (Sandbox Code Playgroud)
在C++ 17中,重新引入了非const重载:
iterator erase( const_iterator pos );
iterator erase( iterator pos );
void erase( const_iterator first, const_iterator last );
size_type erase( const key_type& key );
Run Code Online (Sandbox Code Playgroud)
为什么需要这个?它不加入为不等erase,也不是为insert …
我有一个PySide GUI应用程序(用Python 3编写,在Windows 7 Pro上运行),我在其中设置应用程序图标如下:
class MyGui(QtGui.QWidget):
def __init__(self):
super(MyGui, self).__init__()
...
self.setWindowIcon(QtGui.QIcon('MyGui.ico'))
if os.name == 'nt':
# This is needed to display the app icon on the taskbar on Windows 7
import ctypes
myappid = 'MyOrganization.MyGui.1.0.0' # arbitrary string
ctypes.windll.shell32.SetCurrentProcessExplicitAppUserModelID(myappid)
...
Run Code Online (Sandbox Code Playgroud)
我ctypes从这个答案得到了那些东西.如果我删除这些行,那么当我执行时,Python图标会显示在任务栏中python MyGui.py.
包含这些行后,一切看起来都很棒,窗口和任务栏上都有正确的图标.但是,当我使用cxfreeze打包gui时,窗口和任务栏图标都会更改为通用windows .exe图标.
我正在使用此处cxfreeze.bat的说明打包应用程序,包括交换机.使用该开关可以使生成的exe在资源管理器中查看时具有正确的图标.但是,应用程序窗口和任务栏在我启动应用程序时不会显示图标.我已经尝试将.ico文件复制到与.exe相同的目录,但这没有帮助.--icon
我在Windows 7和8上都有相同的行为.奇怪的是,如果我将应用程序固定到任务栏,任务栏图标会正确显示,但窗口图标仍然是通用exe图标.
如何正确显示图标?
我尝试用VS2013编译一些C++代码,unique_ptr::reset()似乎没有用make_unique(); 一个小的可编辑的repro代码片段如下:
#include <memory>
using namespace std;
int main() {
unique_ptr<int[]> p = make_unique<int[]>(3);
p.reset(make_unique<int[]>(10));
}
Run Code Online (Sandbox Code Playgroud)
从命令行编译:
Run Code Online (Sandbox Code Playgroud)C:\Temp\CppTests>cl /EHsc /W4 /nologo test.cpp
这些是来自MSVC编译器的错误:
Run Code Online (Sandbox Code Playgroud)test.cpp(6) : error C2280: 'void std::unique_ptr<int [],std::default_delete<_Ty> >::reset<std::unique_ptr<_Ty,std::default_delete<_Ty>>>(_Ptr2)' : attempting to reference a deleted function with [ _Ty=int [] , _Ptr2=std::unique_ptr<int [],std::default_delete<int []>> ] C:\Program Files (x86)\Microsoft Visual Studio 12.0\VC\INCLUDE\memory(16 23) : see declaration of 'std::unique_ptr<int [],std::default_delete<_Ty>>::rese t' with [ _Ty=int [] ]
但是,以下代码似乎编译良好:
p = make_unique<int[]>(10);
Run Code Online (Sandbox Code Playgroud)
这种行为的原因是什么?为什么要unique_ptr::operator=()合作make_unique(),但unique_ptr::reset() …
对于我的比赛,我应该使用原始指针来创建SDL_Window,SDL_Renderer,SDL_Texture等他们有特定的删除的功能
SDL_DestroyTexture(texture);
Run Code Online (Sandbox Code Playgroud)
或者我应该在创建unique_ptror 时添加自定义删除器shared_ptr,如果是这样,我将如何使用SDL类型执行此操作?