问题标签 [stdstring]

For questions regarding programming in ECMAScript (JavaScript/JS) and its various dialects/implementations (excluding ActionScript). Note JavaScript is NOT the same as Java! Please include all relevant tags on your question; e.g., [node.js], [jquery], [json], [reactjs], [angular], [ember.js], [vue.js], [typescript], [svelte], etc.

0 投票
3 回答
12347 浏览

c++ - 用于 std::string 的 C++ 位运算符

我的问题是关于如何在 C++ 上使用位运算符std::string。通过重载或作为函数没有关系。

^的工作 XOR/函数示例std::string

我现在缺少的是 NOT/ ~, AND/&加上 OR/的替代品|。示例 C++ 代码(最后两行想要它... :-P):

在 PHP/Perl 我可以做一些讨厌的事情:-P 像这样的事情

...我想念它;-)。但是 XOR 有效。我怎样才能得到 AND/OR/NOT?

0 投票
3 回答
975 浏览

c++ - 如何在 C++ 中获得 std::string 的基本 preg_match_all 替换?

带有“基本”的意思是:只有运算符“+”(->following..)和“|” (-> 或) 是必需的。

原型:

使用示例:

比赛之后应该可以通过matches[n]. 有人在使用boost和/或 PCRE 的情况下得到了提示?如果没有,我是如何通过 boost 实现这一点的?

0 投票
4 回答
15175 浏览

c++ - C++ 程序在执行 std::string 分配时总是崩溃

我一直在尝试调试崩溃的应用程序中的崩溃(即断言 * glibc 检测到 * free():无效指针:0x000000000070f0c0 ***),而我正在尝试对字符串进行简单的分配。请注意,我正在使用 gcc 4.2.4 的 linux 系统上编译,优化级别设置为 -O2。使用 -O0 应用程序不再崩溃。

例如

但是如果我按如下方式更改代码它不再崩溃

所以我又挠了挠头!但有趣的模式是崩溃在应用程序中稍后移动,再次出现在另一个字符串中。我发现应用程序在字符串分配上不断崩溃,这很奇怪。典型的崩溃回溯如下所示:

这是非常奇怪的行为,所以我开始在我的应用程序中进一步查看是否存在某种可能导致这种奇怪行为的内存损坏(堆或堆栈)错误。我什至检查了 ptr 损坏并空手而归。除了对代码进行视觉检查之外,我还尝试了以下工具:

  • Valgrind 同时使用 memcheck 和 exp-ptrcheck
  • 电围栏
  • 安全库
  • 我在 gcc 中使用 -fstack-protector-all 编译
  • 我尝试将 MALLOC_CHECK_ 设置为 2
  • 我通过 lint 检查和 cppcheck 运行我的代码(检查错误)
  • 我使用 gdb 逐步完成了代码

所以我尝试了很多东西,但仍然空手而归。所以我想知道它是否可能是链接器问题或某种可能导致此问题的库问题。std::string 是否存在任何已知问题,使得 make 容易在 -O2 中崩溃,或者它可能与优化级别无关?但是到目前为止,我在我的问题中看到的唯一模式是它似乎总是在字符串上崩溃,所以我想知道是否有人知道我导致这种行为的任何问题。

非常感谢!

0 投票
2 回答
3090 浏览

c++ - Invalid free while performing a std::string assign with -O2 set in g++

Before I get blasted on opening another question, this question is related to another question that I opened a few days ago:

C++ Program Always Crashes While doing a std::string assign

After investigating further based on some of the answers I came up with more questions and more information that may help debug the issue.

So here goes...

The Code in Question:

The GDB backtrace:

So to explain the above, the crash is occuring on line 261 (see comments in code for line numbers) during the string assign. For some reason it tries to free invalid memory, but I'm not sure why? The address it tries to free is not the address of what I thought was the empty string assigned on line 217 (_M_p = 0x3c52020 gets set to _M_p = 0x2abbad098e78 i.e. the empty string) but some other address not too far off.

Any ideas what could be causing this issue? I was wondering if this could be an optimization issue since it does not occur in -O0 and I have ran the code through a battery of tests to try and find a whole slew of memory corruption issues that may cause this weird behavior and ....came up empty handed!

Another interesting note is that if I set std::string lStreamName = ""; instead of std::string lStreamName; the program no longer crashes??? Any Ideas what could cause this? Why would a simple initialization of this string eliminate the crash?

Thanks!

0 投票
3 回答
1107 浏览

destructor - 将 std::string 与 Qt 一起使用会导致破坏时出现运行时错误

我有一个 Qt 应用程序,它使用另一个库,其中函数输出是 std::string 而不是 QString。

所以在我的程序中我有一个方法

当我的外部库返回非空 std::string 时,一切正常。但是当返回一个空的 std::string 时,应用程序会在范围结束时崩溃。我猜这与 std::string 对象(?)的破坏有关。

到 QString 的转换工作正常,即使是空的 std::string。

有人能告诉我为什么会发生这种情况,以及如何避免这个运行时错误吗?

(在其他线程中,有些人讨论了调试和发布库的混合,但我认为我没有这样做。如何找出 btw?)

0 投票
41 回答
1072307 浏览

c++ - std::string 格式化,如 sprintf

我必须格式化std::string并将sprintf其发送到文件流中。我怎样才能做到这一点?

0 投票
2 回答
1986 浏览

c++ - 使用 std::string 迭代器来查找它的字符串的开始和结束

仅给定一个 std::string 迭代器,是否可以确定字符串的起点和终点?假设我无权访问字符串对象,因此无法调用 string.begin() 和 string.end(),我所能做的就是递增或递减迭代器并测试值。

谢谢,菲尔

0 投票
3 回答
6647 浏览

c++ - 使用 g++ 使用 NULL const char* 避免不正确的 std::string 初始化

是否有任何 g++ 选项可以检测到 std::string 与 NULL const char* 的不正确初始化?

我正在将一些 int 字段转换为 std::string 字段,即:

...转换成:

我完全忽略了使用 0 和 g++ 进行的糟糕的“id”初始化,根本没有给我任何警告。在运行时检测到此错误(std::string 构造函数引发异常),但我真的很想在编译时检测到这些东西。有什么办法吗?

0 投票
1 回答
607 浏览

iphone - Convert std::basic_string to NSString in Objective-C / Cocoa

How do I convert a std::basic_string to a NSString ?

0 投票
1 回答
1096 浏览

c++ - 托管 c++ std::string 在非托管 c++ 中不可访问

在非托管 c++ dll 中,我有一个以常量std::string为参数的函数

我通过传递一个std::string.
当我调试非托管 c++ 代码时,参数imageSpec_正确显示值,但不允许我将该值复制到其他变量中。

它显示长度imageSpec_ 为 0(零)。
如果我尝试像std::string sTempFileName(imageSpec_);这个语句字符串一样复制新字符串是一个空字符串。

但是对于std::string sTempFileName(imageSpec_.c_str());这个语句字符串被正确复制。即带有字符指针的字符串被正确复制。

以这种方式复制将需要对非托管 c++ 代码进行重大更改。
我在 Visual Studio 6.0 中构建非托管代码,在 Visual Studio 2008 中构建托管 c++。托管 c++ 中
是否有任何特定设置或代码更改可以解决该问题?