问题标签 [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 投票
5 回答
2313 浏览

c++ - 为什么在 std 字符串中间设置 null 没有任何效果

考虑

我希望std::string其行为与char数组 a 相同。就是这样,在字符串中间插入空字符,将“终止”字符串。然而,事实并非如此。我的期望错了吗?

0 投票
3 回答
887 浏览

c++ - 避免在没有原始指针的情况下复制地图的键

每次您在键为 std::string 的 std::map 中插入一对时,它都会生成两个副本。您可以避免使用原始指针,但它是异常不安全的。有没有办法使用智能指针而不是原始指针?

示例代码:

输出:

0 投票
1 回答
211 浏览

c++ - STD::string 作为动态分配对象的成员参数

我希望动态分配一组对象(可以是几百个)。这些对象的一部分是文本字段。因为 std::string 提供了很好的字符串操作,所以我更喜欢std:string类型的对象成员参数。

但是,std::string 是可动态调整大小的 object。对我来说,这与包含 std::string 的动态分配对象背道而驰:如果 std::string 比计划的大,则可能分配内存溢出。

  • 我能否请教 std::string 是否适合作为动态分配对象的参数?
  • 如何用std::string进行操作,不会出现内存溢出?
  • 即使将动态分配数百个对象,std::string 是否适合作为成员参数?
0 投票
3 回答
38540 浏览

c++ - 如何使用参数集合格式化 std::string?

是否可以格式化std::string传递一组参数?

目前我正在以这种方式格式化字符串:

但是向量的大小是在运行时确定的。是否有任何类似的函数sprintf_s接受参数集合并格式化 std::string/char*?我的开发环境是 MS Visual C++ 2010 Express。

编辑: 我想实现类似的东西:

0 投票
4 回答
63896 浏览

c++ - 将 cout 输出到 std::string

我有以下cout声明。我使用 char 数组,因为我必须传递vsnprintf给以转换变量参数列表并存储在Msg.

有什么办法可以cout输出到 C++std::string吗?

0 投票
1 回答
5098 浏览

c++ - STL basic_string 长度为空字符

为什么你可以在 std::basic_string 中插入一个 '\0' 字符并且 .length() 方法不受影响,但是如果你调用char_traits<char>::length(str.c_str())你会得到字符串的长度直到第一个 '\0' 字符?

例如

0 投票
1 回答
13814 浏览

c++ - STL Character Traits 的意义何在?

我注意到在我的 SGI STL 参考副本中,有一个关于 Character Traits 的页面,但我看不到这些是如何使用的?它们是否替换了 string.h 函数?它们似乎没有被 使用std::string,例如length()on 方法std::string没有使用 Character Traitslength()方法。为什么存在性格特征,它们是否曾在实践中使用过?

0 投票
4 回答
47470 浏览

c++ - 如何使用“”初始化 std::string?

我在使用""(即空字符串)初始化 std::string 变量时遇到问题。它导致以前工作的代码出现奇怪的行为。下面的说法有错吗?

当我使用以下代码时,一切正常:

我相信字符串文字存储在依赖于编译器的单独内存位置。我看到的问题实际上是否表明该存储已损坏?如果是这样,它会被我对该clear() 函数的使用隐藏起来。

谢谢。

0 投票
5 回答
16412 浏览

java - 用户输入忽略大小写

我正在阅读用户输入。我想知道如何应用equalsIgnoreCase到用户输入?

0 投票
4 回答
6300 浏览

c++ - std::string with no free store memory allocation

I have a question very similar to

How do I allocate a std::string on the stack using glibc's string implementation?

but I think it's worth asking again.

I want an std::string with local storage that overflows into the free store. std::basic_string provides an allocator as a template parameter, so it seems like the thing to do is to write an allocator with local storage and use it to parameterize the basic_string, like so:

I tried to write the inline_allocator class that would work the way you'd expect: it reserves 10 bytes for storage, and if the basic_string needs more than 10 bytes, then it calls ::operator new(). I couldn't get it to work. In the course of executing the above line of code, my GCC 4.5 standard string library calls the copy constructor for inline_allocator 4 times. It's not clear to me that there's a sensible way to write the copy constructor for inline_allocator.

In the other StackOverflow thread, Eric Melski provided this link to a class in Chromium:

http://src.chromium.org/svn/trunk/src/base/stack_container.h

which is interesting, but it's not a drop-in replacement for std::string, because it wraps the std::basic_string in a container so that you have to call an overloaded operator->() to get at the std::basic_string.

I can't find any other solutions to this problem. Could it be that there is no good solution? And if that's true, then are the std::basic_string and std::allocator concepts badly flawed? I mean, it seems like this should be a very basic and simple use case for std::basic_string and std::allocator. I suppose the std::allocator concept is designed primarily for pools, but I think it ought to cover this as well.

It seems like the rvalue-reference move semantics in C++0x might make it possible to write inline_allocator, if the string library is re-written so that basic_string uses the move constructor of its allocator instead of the copy constructor. Does anyone know what the prospect is for that outcome?

My application needs to construct a million tiny ASCII strings per second, so I ended up writing my own fixed-length string class based on Boost.Array, which works fine, but this is still bothering me.