string-comparison-functions
全部标签 std::string_view::remove_prefix()和std::string_view::remove_suffix()都是c中的constexpr成员函数++17;但是,它们会修改调用它们的变量。如果值是constexpr,它也将是const并且不能修改,那么这些函数如何用于constexpr值?换句话说:constexprstd::string_viewa="asdf";a.remove_prefix(2);//compileerror-aisconst如何在constexprstd::string_view上使用这些函数?如果它们不能在constexprstd::s
我对模板并不陌生,但我遇到了一个相当奇怪的问题,我需要将模板类型分离到它的组件中,以用于我正在处理的数据序列化程序。这很难解释,所以我已经证明了。这是我简化的示例问题,example.cpp。templatevoidfoo(T&arg){}templatevoidfoo(T&arg){}intmain(intargc,char*argv[]){foo(argc);return0;}我得到一个错误,然后是一个警告,这似乎表明它正在尝试实例化两个函数,但只有其中一个是合适的。$g++-Wall-Wexample.cppexample.cpp:2:43:error:‘T’isnotatemp
在名为::foo()的函数中,我不明白语法的用途。如果它是foo::count_all()那么我知道count_all是类或命名空间foo的函数。在::foo()的情况下,::引用的是什么? 最佳答案 ::运算符正在调用namespace或class。在您的情况下,它正在调用全局命名空间,它是不在命名空间中的所有内容。下面的例子说明了为什么namespace很重要。如果您只是调用foo(),您的调用将无法解析,因为有2个foo。您需要使用::foo()解决全局问题。namespaceHidden{intfoo();}intfoo()
我知道std::string_view是对字符串的非拥有引用和std::string_view之间的主要区别和std::string是现在,为什么std::string_view不适用于其他类型?或者为什么这个实现只针对std::string?例如:如果我们有类似的generic_view其中T可以是任何类型,包括自定义类型。有了这个,而不是使用constT&作为函数参数,generic_view可以使用。以及std::string_view的其他优势将很有用,如分配、复制等。 最佳答案 C++20中有一个非拥有类型,用于任意对象的
为什么我在最后两行收到错误?目标是在集合中找到对象,并修改其内容。usingnamespacestd;structmystruct{intid;vectory;mystruct(constintid):id(id){}booloperatorsx;mystructx(1);x.y.push_back(1);x.y.push_back(2);sx.insert(x);//set::iteratori=sx.find(1);constmystruct*x1=&(*i);constmystructx2=*x1;couty)y)y.push_back(4);}好像迭代器返回的是常量对象,不让我
我在我的C++代码中定义了is_string:#includetemplatestructis_string{staticconstboolvalue=false;};templatestructis_string>{staticconstboolvalue=true;};intmain(){std::cout::value::value对于std::string和std::wstring都是如此。但我需要这样的谓词:is_string::value//tobetrueis_string::value//tobefalseis_string::value//tobefalseis_st
我在openSUSELeap15上的Qt5.9.4上使用GCC7。我有以下类(class):classManSuppProps:publicQObject{Q_OBJECTpublic:explicitManSuppProps(QStringparentName);explicitManSuppProps(){}explicitManSuppProps(constManSuppProps&manSuppProps);explicitManSuppProps(ManSuppProps&manSuppProps);~ManSuppProps();private:QVector3Dm_sup
从cppref对capacity()的描述来看并不明显和reserve()是否计算终止空字符。 最佳答案 标准statesthat:Inallcases,size().和size()不包括终止空值。因为有可能size()等于capacity(),在这种情况下,这意味着capacity()也不计算终止空值。请注意,在C++11及更高版本中,mystring.c_str()相当于mystring.data()相当于&mystring[0],和mystring[mystring.size()]保证是'\0'.检查这个Demo.
我正在试验union,并将这个示例设为A类,其中包含匿名union成员。由于union包含一个std::string和一个std::vector我需要为该类定义一个析构函数。但是,当我尝试手动调用~string()时,我得到了union.cpp:Indestructor'A::~A()':union.cpp:14:14:error:expectedclass-namebefore'('tokens_.~string();我不明白这个vector。如果我删除对s._~string();的调用,它可以正常编译。这是编译器错误吗?我正在使用MinGW64。#include#includecl
我有一个看起来像这样的类。classA{public:voiddoSomething();}我有一组这些类。我想对数组中的每个项目调用doSomething()。使用算法header执行此操作的最简单方法是什么? 最佳答案 使用std::mem_fun_ref将成员函数包装为一元函数。#include#includestd::vectorthe_vector;...std::for_each(the_vector.begin(),the_vector.end(),std::mem_fun_ref(&A::doSomething));