C++字符串迭代器

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5422835/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-28 18:07:59  来源:igfitidea点击:

c++ string iterator

c++stringiterator

提问by Joe

I'm trying to do an if statement inside a loop with an iterator over a string, but can't figure out how to get the current character for the if statement:

我正在尝试在循环中使用迭代器在字符串上执行 if 语句,但无法弄清楚如何获取 if 语句的当前字符:

for (std::string::iterator i=buffer.end()-1; i>=buffer.begin(); --i) {
    if (!isalpha(*i) && !isdigit(*i)) {
        if(i != "-") { // obviously this is wrong
            buffer.erase(i);
        }
    }
}

Can someone help me get the current character so I can do some additional if statements?

有人可以帮我获取当前角色,以便我可以做一些额外的 if 语句吗?

回答by James McNellis

I can't figure out how to get the current character

我不知道如何获得当前角色

You do it twice here:

你在这里做两次:

if (!isalpha(*i) && !isdigit(*i))

When you dereference an iterator (*i), you get the element to which it points.

当您取消引用迭代器 ( *i) 时,您将获得它指向的元素。

"-"

This is a string literal, not a character. Character constants use single quotes, e.g., '-'.

这是一个字符串文字,而不是一个字符。字符常量使用单引号,例如,'-'

for (std::string::iterator i=buffer.end()-1; i>=buffer.begin(); --i)

This would be much simpler with reverse iterators:

使用反向迭代器会简单得多:

for (std::string::reverse_iterator i = buffer.rbegin(); i != buffer.rend(); ++i)

回答by Luzik

if(i != "-")

should be

应该

if(*i != '-')

回答by David Rodríguez - dribeas

Other answers have solved the particular issue that you have, but you should be aware that there are different approaches to solve your actual problem: erase elements that fullfill a condition. That can be easily solved with the remove/eraseidiom:

其他答案已经解决了您遇到的特定问题,但您应该意识到有不同的方法可以解决您的实际问题:擦除满足条件的元素。这可以使用remove/erase成语轻松解决:

// C++0x enabled compiler
str.erase( 
    std::remove_if( str.begin(), str.end(), 
                  [](char ch) { return !isalpha(ch) && !isdigit(ch) && ch != '-' } ),
    str.end() );

While this might look cumbersome at first, once you have seen it a couple of times it will no longer be surprising, and it is an effective way of deleting elements from a vector or string.

虽然一开始这看起来很麻烦,但一旦你看过几次就不会再感到惊讶了,它是一种从向量或字符串中删除元素的有效方法。

If your compiler does not have lambda support, then you can create a functor and pass it as the third argument to remove_if:

如果您的编译器不支持 lambda,那么您可以创建一个函子并将其作为第三个参数传递给remove_if

// at namespace level, sadly c++03 does not allow you to use local classes in templates
struct mycondition {
   bool operator()( char ch ) const {
      return !isalpha(ch) && !isdigit(ch) && ch != '-';
   }
};
// call:
str.erase( 
    std::remove_if( str.begin(), str.end(), mycondition() ),
    str.end() );

回答by Mark B

To get the character just say *i, but this isn't enough. Your loop isn't legal because it's not allowed to decrement before begin. You should use reverse iterators or the remove_ifalgorithm.

要获得角色只需说*i,但这还不够。您的循环不合法,因为它不允许在begin. 您应该使用反向迭代器或remove_if算法。

回答by Jerry Coffin

You have it right above in the previous ifstatement: iis an iterator, so *igives the character referred to by the iterator.

在前面的if语句中,它就在上面:i是一个迭代器,因此*i给出了迭代器所引用的字符。

Note that if you're going to iterate through a collection backwards, it's generally easier to use a reverse_iteratorwith rbeginand rend. I'd probably use a pre-packaged algorithm instead though.

请注意,如果您要向后遍历集合,通常使用reverse_iteratorwithrbegin和更容易rend。不过,我可能会使用预先打包的算法。