草庐IT

string-comparison-functions

全部标签

c++ - char * 和 const_cast<char*>(string.c_str()) 有什么区别

我使用外部库来处理两个应用程序之间的udp(OSC)通信。为了格式化将要发送的消息,库需要一个char*但我从UI中得到一个字符串,我必须转换它。当我处理代码的其他部分时,udp部分是硬编码的:char*endofMess="fromsetEndMess";并且工作正常。我认为使用我的字符串很容易让它工作并写道:std::strings="fromsetEndMess";char*endofMess=const_cast(s.c_str());但与第一个例子不同,我收到的消息格式正确,现在我只收到乱码。有人知道它可能来自哪里吗?谢谢!马修编辑:我使用的代码:每次OSCVal发送消息的方

c++ - 如何使用 lambda 在 std::function 参数中推导模板类型?

我有一个boost::variant并且我只想在变体是特殊类型时才执行一个仿函数,所以我编写了这个函数:templatevoidif_init(Variant&opt_variant,std::functionfunctor){if(auto*ptr=boost::get(&opt_variant)){functor(*ptr);}}这很好用,但我希望推导出类型T,这样我就可以这样写:if_init(b,[](doublevar){std::cout但是没有推导出类型:type_inference.cpp:19:5:error:nomatchingfunctionforcallto'i

c++ - 将抽象仿函数分配给 std::function - 为什么 std::ref 是一个解决方案?

我想将仿函数对std::function的赋值封装到一个方法中。我必须传递从通用抽象类Slot继承的仿函数,而不是传递std::function或指向std::function的指针(即这些槽提供额外的功能)。我以不同的形式偶然发现了这个问题here.例如。在那里,使用通用槽指针而不是std:functions的动机是仿函数的生命周期管理。下面的代码说明了这个问题。请参阅assignFunctorPtr(...)方法。#include#includetemplateclassSlot;templateclassSlot{public:typedefRRet_type;public:vi

c++ - 显式默认和删除的构造函数 : is there any similar functionality available in VS2012?

在VS2012中,“显式默认和删除特殊成员函数”功能(http://en.wikipedia.org/wiki/C++0x#Explicitly_defaulted_and_deleted_special_member_functions、http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2007/n2346.htm)尚不可用(http://msdn.microsoft.com/en-us/library/vstudio/hh567368.aspx)。是否有任何解决方法来使用此类功能,即使非常冗长?在实践中,我可以翻译这个吗struc

c++ - 错误 : no instance of overloaded function "std::make_shared" matches the argument list

查看ApreviousstackQuestionstd:make_sharedvsstd::shared_ptr,我试图在一个uni项目中实现它。这是之前的“问题”:Ican'tthinkofanysituationwherestd::shared_ptrobj(newObject("foo",1));wouldbepreferredtoautoobj=std::make_shared("foo",1);因此我采用了这段代码:std::shared_ptrpT1(newTriangle(pCanvas,30,30,30,60,60,30,255,0,0));并将其修改为这段代码:aut

c++ - 读取 std::string,从 std::string 中删除所有特殊字符

我是这个论坛和c++的新手。所以请原谅我的疑惑/问题。我正在尝试读取std::string。我知道我可以使用at或[int]运算符访问元素。我有2个问题:1)删除或删除字符串中的所有特殊字符(包括空格)2)只读取该字符串的前4个字符或字母对于1),我正在检查std::erase和std::remove_if,但我也需要消除所有特殊字符和空格。这意味着我需要包括isspace()/isalpha()等所有条件。没有单一的方法可以一次全部删除吗?对于2),我可以像访问数组一样访问字符串,我的意思是string[0]、string[1]、string[2]、string[3]。但是我不能将它

c++ - std::map 中的 std::string 导致 Valgrind 内存泄漏

这是Valgrind的输出:==6519==at0x4C25885:operatornew(unsignedlong)(vg_replace_malloc.c:319)==6519==by0x4EE65D8:std::string::_Rep::_S_create(unsignedlong,unsignedlong,std::allocatorconst&)(new_allocator.h:104)==6519==by0x4EE7CE0:char*std::string::_S_construct(charconst*,charconst*,std::allocatorconst&,s

c++ - 复制初始化期间不会发生 std::string 的隐式构造

我正尝试在main()函数中复制初始化我的CObj类:#include#includeclassCObj{public:CObj(std::stringconst&str):m_str(str){std::cout但是,即使std::string是从charconst*隐式构造的,CObjobj="hello"行也无法编译>。根据我在这里的理解,这应该有效。有什么理由不这样做吗?如果我这样做,它会起作用:CObjobj=std::string("hello"); 最佳答案 文字"Hello"的类型为constchar[6]:为了调用

c++ - 使用尽可能少的代码将非静态方法包装到带有 "this"参数绑定(bind)的 std::function

这是我正在尝试做的事情:templateclassCSignal{public:voidconnect(std::functiontarget){m_connections.emplace_back(target);}private:mutablestd::vector>m_connections;};connect非常适合静态方法或全局函数。现在,如果我想传递一个成员方法怎么办?看来这是我唯一的选择:structMyStruct{voidprint(floata,intb){std::cout如果我不必指定非常麻烦的占位符,它会适合我。所以我尝试另一种方法。我为成员方法添加了一个新的

c++ - 在嵌套的 lambda 中应用 function_traits 时编译失败

首先,我有这样的东西,一个重命名的function_traits来获取lambda的返回类型templatestructFuncAnalyzer{};templatestructFuncAnalyzer{usingTReturn=TRet;};templatestructFunctionAnalyzer:publicFuncAnalyzer{};然后当我在一个方法中有这个时,那个compi:autoa=[](constint&key)->QString{returnQString::number(key);};usingb=FunctionAnalyzer::TReturn;bx;但是