English 中文(简体)
如何终止 C++ 中的 std:: string?
原标题:How to terminate a std::string in C++?
  • 时间:2012-05-28 05:33:23
  •  标签:
  • c++

我试图从 std:::string in C++. 中删除所有标点字符。 我目前的代码 :

string str_in;
string::size_type i, j;

cout << "please input string with punctuation character..." << endl;
cin >> str_in;

for (i = 0, j = 0; i != str_in.size(); ++i)
    if (!ispunct(str_in[i]))
         str_in[j++] = str_in[i];

str_in[j] =   ;

cout << str_in << endl;

str_in[j] = ; 错误吗?

最佳回答

如果您想要将 str_ in 切换到第一个 j 字符, 您可以说 str_in. resize(j)

如果您想要使用标准库, 您可以应用 < code> erase- remove 语系这样的程序 :

#include <algorithm>
#include <iostream>
#include <string>

int main()
{
    std::string str_in;
    std::getline(std::cin, str_in);

    // Here is where the magic happens...
    str_in.erase(std::remove_if(str_in.begin(), str_in.end(), ::ispunct), str_in.end());

    std::cout << str_in <<  
 ;

    return 0;
}
问题回答

C++字符串类型未被执行为无效终止(尽管一个 c_str () call 会给您一个无效终止字符串 。 )

所以是, str_in[j] = 是错误的,至少有两个原因:

  1. The str_in.length() will not reflect the size of the string you expect with the punctuation removed.
  2. The null charatcter is an extra charter which will be sent to any output stream,
    such as cout << str_in;

使用 std ::string class 你可能不应该使用相同的缓冲, 但可能使用 str_out 缓冲, 在复制所有通缉犯( 排除标点字符) 后, 缓冲长度将保持正确长度, 或者您应该调整 < code>str_ in 的长度, 而不是添加空号 。

我认为,当字符串没有任何标点时, str_in[j] = * 是错的 。

与其修改同一字符串,不如创建一个新的字符串(例如 str_out ),并附加到该字符串上:

str_out += str_in[i];




相关问题
Undefined reference

I m getting this linker error. I know a way around it, but it s bugging me because another part of the project s linking fine and it s designed almost identically. First, I have namespace LCD. Then I ...

C++ Equivalent of Tidy

Is there an equivalent to tidy for HTML code for C++? I have searched on the internet, but I find nothing but C++ wrappers for tidy, etc... I think the keyword tidy is what has me hung up. I am ...

Template Classes in C++ ... a required skill set?

I m new to C++ and am wondering how much time I should invest in learning how to implement template classes. Are they widely used in industry, or is this something I should move through quickly?

Print possible strings created from a Number

Given a 10 digit Telephone Number, we have to print all possible strings created from that. The mapping of the numbers is the one as exactly on a phone s keypad. i.e. for 1,0-> No Letter for 2->...

typedef ing STL wstring

Why is it when i do the following i get errors when relating to with wchar_t? namespace Foo { typedef std::wstring String; } Now i declare all my strings as Foo::String through out the program, ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

Window iconification status via Xlib

Is it possible to check with the means of pure X11/Xlib only whether the given window is iconified/minimized, and, if it is, how?