简体   繁体   English

c ++字符串迭代器

[英]c++ string iterator

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语句吗?

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, eg, '-' . 字符常量使用单引号,例如'-'

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)
if(i != "-")

应该

if(*i != '-')

To get the character just say *i , but this isn't enough. 要让角色只说*i ,但这还不够。 Your loop isn't legal because it's not allowed to decrement before begin . 你的循环不合法,因为它不允许在begin之前递减。 You should use reverse iterators or the remove_if algorithm. 您应该使用反向迭代器或remove_if算法。

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/erase idiom: 这可以通过删除/擦除习惯用法轻松解决:

// 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() );

You have it right above in the previous if statement: i is an iterator, so *i gives 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_iterator with rbegin and rend . 请注意,如果您要向后迭代集合,通常更容易使用带有rbeginrendreverse_iterator I'd probably use a pre-packaged algorithm instead though. 我可能会使用预先打包的算法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM