草庐IT

string-comparison-functions

全部标签

C++:是否建议创建自己的 String 类?

我正在阅读一本C++教程书,作者说大多数C++编译器都包含一个String类库,但即使有,也建议创建您自己的String类。不幸的是,作者没有说明原因。有没有人不同意/同意,为什么?我正在使用Xcode,所提供的String类对我来说似乎很好,但话又说回来,我只使用它几个小时,所以我不知道它的局限性。提前致谢。 最佳答案 我觉得你有一本旧书。C++标准库包括std::string类,您应该使用它而不是自己发明并修复已经在广泛使用的库中遇到和修复的错误。作为初学者,是的,您应该尝试实现自己的方法,因为它会教给您很多东西。只是不要在任何

C++ : friend declaration ‘declares a non-template function

我在重载时遇到问题流运算符(operator),我找不到解决方案:templateclassNVector{inlinefriendstd::ostream&operator&rhs);};templateinlinestd::ostream&NVector::operator&rhs){/*SOMETHING*/returnlhs;};它产生以下错误信息:warning:frienddeclaration‘std::ostream&operatorerror:‘std::ostream&NVector::operator如何解决这个问题?非常感谢。 最佳答

c++ - 将 '\0' 添加到 std::string 时出现意外行为

为什么C++标准允许以下内容?#include#includeintmain(){std::strings(10,'\0');//s.length()nowis10std::cout为什么要加0算呢?它看起来像是破坏了字符串的完整性,不是吗?但我检查了标准,这是正确的行为。 最佳答案 Whydoesstandardallowsfollowing?因为设计C++字符串的人决定允许这样的事情发生。我不确定设计C++字符串的团队中是否有人在SO上……但是既然你自己说标准允许它,那就是它的方式,我怀疑它是否会改变。有时拥有一个可以包含“任何

c++ - 一种使用 STL 计算 std::string vector 中字符的方法?

虽然查找std::stringvector中的字符数量很简单,但我想知道是否有办法使用STL为您完成所有工作,而不是使用两个for循环,一个循环遍历vector,另一个循环遍历vector每个索引中的字符串。我尝试过使用其他STL函数(例如尝试以几种独特的方式使用std::for_each),但我所有的尝试都没有成功。intmain(void){intchars=0;std::vectorstr;str.push_back("Vector");str.push_back("of");str.push_back("four");str.push_back("words");for(int

c++ - 错误 : expected primary-expression before ‘>’ : templated function that try to uses a template method of the class for which is templated

这个问题在这里已经有了答案:WhereandwhydoIhavetoputthe"template"and"typename"keywords?(8个答案)关闭8年前。在使用模板和仿函数(未出现在这个问题中)时,我最终遇到了以下简化的问题。以下代码(也可用here)classA{public:templateboolisGood(intin)const{constTf;returninbooltryEvaluator(T&evaluator,intvalue){returnevaluator.isGood(value);}intmain(intargc,constchar*argv[]

c++ - 为什么在取消引用的函数指针上使用时 std::is_function 的计算结果为 false?

我正在尝试使用std::is_function来确定变量是否为函数指针。运行以下代码时#include#includeusingnamespacestd;intmain(){typedefint(*functionpointer)();functionpointerpmain=main;cout::value::value::value::value输出是PFivE0PFivE0FivE1FivE0任何有见识的人都可以解释为什么std::is_function的最后一个表达式的计算结果为false吗?(代码在g++4.7、g++4.8和clang++3.2下测试)

c++ - 为什么 std::function 不能接受推导类型作为其模板参数?

#includeusingnamespacestd;templatevoidf1(CharType*str,functionfn_filter){}templatevoidf2(CharType*str,functionfn_filter){}voidf3(char*str,charc){autofn_filter=[=](chare)->bool{returne==c;};f1(str,fn_filter);//errorC2784f2(str,fn_filter);//OK}intmain(){f3("ok",'k');}//errorC2784:'voidf1(CharType*

c++ - std::string 和 placement new

我发现了这个在C++中使用placementnew的例子,它对我来说没有意义。我认为这段代码容易出现异常,因为可能会使用比分配的内存更多的内存。char*buf=newchar[sizeof(string)];string*p=new(buf)string("hi");如果“string”是C++STD::string类,那么buf将得到一个分配空字符串对象的大小(我的编译器给出了28个字节),然后我看到它的方式如果你用更多的字符初始化你的字符串你可能超过分配的内存。例如:string*p=new(buf)string("hiiiiiiiiiiiiiiiiiiiiiiiiiiiiiii

c++ - string[] 不是类型吗?

一位用户发布了一个answer其中说:So,taketheHashbyreferencevoidtopWords(Hashconst&t,std::stringconst&word,std::string*topA);Also,string[]isnotatypeinC++...提问者贴出的原函数是这样的:voidtopWords(Hasht,stringword,stringtopA[]);我知道在Java中,声明数组的语法是:int[]anArray;而在C++中它是:intanArray[someNumber];我怀疑这就是回答者所指的,但也许他们指的是别的意思。于是咨询了n33

c++ - 为什么 std::unary_function 不包含虚拟析构函数

我遇到了类模板std::unary_function和std::binary_function。templatestructunary_function{typedefArgargument_type;typedefResultresult_type;};templatestructbinary_function{typedefArg1first_argument_type;typedefArg2second_argument_type;typedefResultresult_type;};这两个都可以用作特定用途的基类。但是其中仍然没有虚拟析构函数。我猜的原因之一是这些并不意味着要进