草庐IT

overloaded-strings

全部标签

c++ - 为什么 std::string_view::data 不包含空终止符?

这段代码有未定义的行为:#include#includeusingnamespacestd::string_view_literals;voidfoo(std::string_viewmsg){std::cout原因是std::string_view可以存储非空终止字符串,并且在调用data时不包含空终止符。这真的很有限,为了让上面的代码定义行为,我必须从中构造一个std::string:std::stringstr{msg};std::cout在这种情况下,这确实使std::string_view变得不必要了,我仍然必须复制传递给foo的字符串,所以为什么不使用移动语义并更改msg到

c++ - 如何构造一个 std::string 具有可变数量的空格?

如果我有以下代码:std::stringname="Michael";std::stringspaces="";我将如何以编程方式创建spaces字符串(一个包含所有空格、长度与名称变量匹配的字符串)? 最佳答案 您可以将一个字符和一个长度传递给一个字符串,它会用给定的字符填充该长度的字符串:std::stringspaces(7,'');您可以使用std::string的.size()属性来查找您姓名的长度;结合以上内容:std::stringname="Michael";std::stringspaces(name.size()

报错:JSON parse error: Cannot deserialize value of type `long` from String “1,2“: not a valid `long` v

详细报错信息JSON parse error: Cannot deserialize value of type `long` from String "1,2": not a valid `long` value; nested exception is com.fasterxml.jackson.databind.exc.InvalidFormatException: Cannot deserialize value of type `long` from String "1,2": not a valid `long` value at [Source: (org.springframe

c++ - 将 std::string 解释为 char_type 的 std::vector?

我有一个template接受constvector&的函数.在所述函数中,我有vectorcbegin(),cend(),size(),和operator[].据我了解,string和vector使用连续空间,所以我想知道我是否可以以一种优雅的方式为两种数据类型重用该函数。可以std::string被重新解释为std::vector的(适当的)char_type?如果可以,限制是什么? 最佳答案 如果你只为constT&类型制作你的模板,并使用begin()、end()等,这两个函数vector和string共享,那么您的代码将适用

c++ - 错误 : call of overloaded ‘max(int, int)’ is ambiguous

#includeusingnamespacestd;templateTmax(Tlhs,Trhs){returnlhsintmax(intlhs,intrhs){returnlhs(4,5)如何更正此错误? 最佳答案 这都是因为你的usingnamespacestd;。删除该行。通过该using指令,您将std::max(必须通过iostream以某种方式包含)带入全局范围。因此,编译器不知道调用哪个max-::max或std::max。我希望这个例子对于那些认为使用指令是免费的的人来说是一个很好的稻草人。奇怪的错误是一种副作用。

c++ - double 到 string 到 double 的转换抛出异常

以下代码在VisualStudio2013中引发了std::out_of_range异常,我认为它不应该:#include#includeintmain(intargc,char**argv){doublemaxDbl=std::stod(std::to_string(std::numeric_limits::max()));return0;}我也用gcc4.9.2测试了代码,它没有抛出异常。该问题似乎是由转换为字符串后不准确的字符串表示引起的。在VisualStudio中std::to_string(std::numeric_limits::max())产量1797693134862

c++ - 包含 Map(具有对象值)和 Multimap(具有 std::string 值)的对象的 boost 序列化:需要什么?

请参阅下面的main()和两个非常简单的类。然后根据Boost序列化(以及显示的内容)我的问题是:1)B类是否需要定义正常的重载流插入运算符“>”?目前在我的真实代码中没有这些。2)store()和load()方法中的类A是否需要显式地遍历map和multimap容器,显式地存储/加载它们的键:值对?例如像这样的东西:voidA::store(constchar*filename){std::ofstreamofs(filename);boost::archive::text_oarchiveoa(ofs);std::map::iteratorit;BMap.size();oafirs

c++ - 在限制之间搜索 std::string

如果您知道从哪里开始和结束搜索的字符串中的开始和结束位置。例如-strings=StringStringString|S|t|r|i|n|g|S|t|r|i|n|g|S|t|r|i|n|g01234567891011121314151617如何在字符串中找到“tr”,指定开始搜索的位置在索引6处,结束搜索的位置在索引9处。我正在尝试设置一个搜索限制,这样它就不会超出它。 最佳答案 如果你真的想限制被遍历的序列的长度(大概是因为字符串比感兴趣的区域长),使用std::search并将相应的迭代器传递给字符串。

tablenameattribute.tablenameattribute(String)之间的模棱两可的错误

我只是升级了.Netv4.0至4.6.1并最终在应用程序中使用的SQL相关属性(tableName,primarykey,列)的歧义错误。我试图弄清楚哪个DLL导致此错误。我正在看到错误tablenameattribute.tablenameattribute(字符串)首要的关键。columnAttribute...等我想知道哪个dll我必须删除看答案歧义是在实体框架和system.com.ponentmodel.dataannotations之间

c++ - 为什么编译器不能优化 std::string concat?

我尝试这样简单的代码:#include#includeintmain(){conststd::strings=std::string("a")+"b";puts(s.c_str());return0;}我希望编译器(gcc4.8.2/clang3.5.0)优化这样的代码intmain(){puts("ab");return0;}但我得不到这样的结果,我尝试了不同的选项,如“-Ofast”、“-flto”、“-static-libstdc++”,但总是在反汇编输出中看到三个函数调用:...callq0x4017e0...callq0x401690...callq0x401490第一个是调