【问题标题】:c++ string iterator "find first of"c ++字符串迭代器“查找第一个”
【发布时间】:2015-05-11 17:16:07
【问题描述】:

字符串迭代器中是否有类似 find_first_of on string 的方法? 比如:

string::iterator it;
string str("  h asdasf ^& saafa");
it = FIND_FIRST_OF("&az^");
std::cout << *it << std::endl;

结果:

一个

【问题讨论】:

  • 简短回答:不。迭代器有一个非常狭窄的接口——主要是取消引用和advance(使用像++-- 这样的成员来进行推进)。您可以考虑使用std::regex,不过它提供了一个迭代器接口。

标签: c++ string iterator


【解决方案1】:

你可以间接做到这一点

auto pos = str.find_first_of("&az^");

然后推进迭代器

if(pos != std::string::npos) // thanks to @Mike Seymour
    std::advance(it, pos);

我猜你也可以用 lambda 做某种std::find,但上面的内容确实更简单简洁。

【讨论】:

  • 你最好先确认不是npos
  • @MikeSeymour 谢谢,这是隐含的假设:) 无论如何修改它。
  • 不应该是str[pos](问题是要一个字符,虽然它提到了'iterator')
  • for( it = line.begin(); it != line.end(); it++ ) ...我有一个解决方案,我想找到一个解决方案来改变 begin()像这样: for( it = line.find_first_of("{[],}\""); it != line.end(); it++ )
  • @DieterLücking 不清楚 OP 是否需要迭代器或位置。如果只是位置,那么是的,str[pos] 就足够了。
【解决方案2】:

我认为std::find_first_of 是您要找的。​​p>

string::iterator it;
string str("  h asdasf ^& saafa");
string find_me ("&az^");
it = std::find_first_of (str.begin(), str.end(), find_me.begin(), find_me.end());
std::cout << *it << std::endl;

如果以任何频率使用此方法,我会编写一个函数来清理构造/使用中间 find_me 变量所涉及的开销。

【讨论】:

  • 这是另一种方式,赞成。不过,您有一些拼写错误,请将; 替换为, 中的find_first_of
【解决方案3】:

试试这个:

std::string::size_type position = example.find_first_of(".");
if (position != std::string::npos)
{
  std::advance(string_iterator, position);
}
else
{
  string_iterator = example.end();
}

【讨论】:

    【解决方案4】:

    std::string除了其他查找方法外,还有自己的方法find_first_offind_last_of

    这是一个演示程序

    #include <iostream>
    #include <string>
    
    int main() 
    {
        std::string s( "  h asdasf ^& saafa" );
        auto pos = s.find_first_of( "&az^" );
    
        if ( pos != std::string::npos ) std::cout << s[pos] << std::endl;
    
        pos = s.find_last_of( "&az^" );
    
        if ( pos != std::string::npos ) std::cout << s[pos] << std::endl;
    
        return 0;
    }
    

    程序输出是

    a
    a
    

    这是另一个演示程序,用于查找字符串中在字符文字中指定的所有字符

    #include <iostream>
    #include <string>
    
    int main() 
    {
        std::string s( "  h asdasf ^& saafa" );
    
        for ( std::string::size_type pos = 0; 
              ( pos = s.find_first_of( "&az^", pos ) ) != std::string::npos;
              ++pos )
        {
            std::cout << pos << ": " << s[pos] << std::endl;
        }        
    
        return 0;
    }
    

    程序输出是

    4: a
    7: a
    11: ^
    12: &
    15: a
    16: a
    18: a
    

    知道找到的位置总能在对象中得到对应的迭代器:

    std::string::iterator it = std::next( s.begin(), pos );
    

    auto it = std::next( s.begin(), pos );
    

    或者干脆

    std::string::iterator it = s.begin() + pos;
    

    还有在标头&lt;algorithm&gt; 中声明的标准算法std::find_first_of 也可以用于std::string 类型的对象。

    【讨论】:

      猜你喜欢
      • 2019-07-23
      • 1970-01-01
      • 2011-07-22
      • 2019-09-22
      • 2018-01-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-02-14
      相关资源
      最近更新 更多