草庐IT

string-comparison-functions

全部标签

c++ 何时返回 const char* 而不是 std :string

在下面的类中,speak()返回constchar*而不是std::string有什么好处或原因吗?classAnimal{protected:std::stringm_name;Animal(std::stringname):m_name(name){}public:std::stringgetName(){returnm_name;}constchar*speak(){return"???";}}; 最佳答案 std::string带有许多可能不需要和未使用的功能。如果您不想要所有这些功能,您应该考虑使用成本。传递std::st

c++ 17将string_view与字符串进行比较时的歧义

我看到std::string_view和std::string都有对称的operator==()和std::string它具有接受std::string_view的构造函数和将自身转换为std::string_view的运算符。所以当我们尝试使用operator==()比较std::string_view和std::string时,它是否应该是有歧义的?我想我的想法一定有问题。谁能解释一下?例子:std::strings1="123";std::string_views2="123";//inthefollowingcomparison,wills1usetheconvertopera

c++ - 为什么我们应该为字符串数据类型导入#include <string> 而不是为其他类型导入?

我是C++的新手,我注意到在处理字符串时您应该包括:#include我的问题是为什么这是必要的,而不是像intfloat等基本类型?谢谢 最佳答案 看来您来自Python或Javascript背景,其中String是一种原始数据类型。在C++中并非如此,原始类型(在C++中称为基本类型)中没有String。但是int,float属于基本类型。在C++中,string是属于复合类型(相对于基本类型)类别的类类型。有关C++类型系统的概述,您可以阅读此referenceontypes. 关于

c++ - 是否保证在 std::string 之前初始化指向字符串文字的指针?

//file1.cppexternconstchar*foo;std::stringbar=foo;//file2.cppconstchar*foo="foo";标准保证bar被初始化为"foo"吗?或者它是否可以在foo被设置并在构造函数中出现段错误之前初始化,即SIOF的情况? 最佳答案 常量初始化保证首先发生(在这种情况下为foo)。所以Isbarguaranteedbythestandardtobeinitializedto"foo"?是的。Orcoulditbeinitializedbeforefoogetssetands

c++ - 如果 new_size 不大于旧的,C++ 标准是否保证 std::string::resize(new_size) 不会导致分配?

这个问题在这里已经有了答案:Doesthestandardguarantee,thatstd::string::resizewillnotdoreallocatememory,ifthenewsizeislessthanorequaltoastheoldone?(1个回答)关闭3年前。#include#includeintmain(){autos="hello"s;autop=&s[0];s.resize(3);assert('h'==*p);//alwaysok?}如果new_size不大于旧的,C++标准是否保证std::string::resize(new_size)不会导致分配

c++ - 消除对采用 std::functions 的函数的调用的歧义

下面的代码不能在gcc4.5上编译,因为对foo的调用不明确。消除歧义的正确方法是什么?#include#includeusingnamespacestd;voidfoo(std::functiont){t(1,2);}voidfoo(std::functiont){t(2);}intmain(){foo([](inta,intb){cout 最佳答案 最好的方法是显式创建一个std::function正确类型的对象,然后将该对象传递给函数:std::functionfunc=[](inta,intb){cout或内联:foo(st

c++ - 在 C++ 中将 uint8_t* 转换为 std::string?

这个问题在这里已经有了答案:关闭12年前。PossibleDuplicate:HowtoConvertByte*tostd::stringinC++?我在嵌入式设备上尝试接收消息。此消息由constuint8_t*data及其长度size_tlen给出。现在我需要一个std::string来输出我的数据。

c++ - 将 vector<string> 转换为 vector<double> 的便捷方法

除了使用变换算法和istringstream之外,C++中是否有任何方便的方法将vector转换为vector?非常感谢您的帮助! 最佳答案 lexical_cast相当“方便”。for(size_ti=0;i(vec[i]));}当然,使用std::transform会变得更加有趣:std::transform(strings.begin(),strings.end(),std::back_inserter(doubles),boost::lexical_cast);//Notethetwotemplatearguments!at

c++ - 如何在 Visual Studio C++ 2010 中将 BSTR 转换为 std::string?

我正在处理COMdll。我希望将BSTR转换为std::string以传递给采用常量引用参数的方法。似乎使用_com_util::ConvertBSTRToString()来获取BSTR的char*等价物是一种合适的方法。但是,API文档很少,而且实现可能存在问题:http://msdn.microsoft.com/en-us/library/ewezf1f6(v=vs.100).aspxhttp://www.codeproject.com/Articles/1969/BUG-in-_com_util-ConvertStringToBSTR-and-_com_util例子:#inclu

c++ - std::function 和 std::bind 行为

我有这个代码:#include#include#includevoidfun(){std::cout>vec;vec.push_back(std::bind(fun));vec.push_back(gun);vec[0](1);vec[1](2);}您能解释一下std::bind是如何实现的吗?返回std::function绑定(bind)时void()功能?如何调用void()使用void(int)的功能仿函数? 最佳答案 作为function的模板参数传递的签名仅确定将绑定(bind)多少个占位符(_1)以及类型。实际函数的调用