Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/157.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ c++;字符串迭代器;查找“第一个”;_C++_String_Iterator - Fatal编程技术网

C++ c++;字符串迭代器;查找“第一个”;

C++ c++;字符串迭代器;查找“第一个”;,c++,string,iterator,C++,String,Iterator,字符串迭代器中是否有像find_first_of on string这样的方法? 比如: string::iterator it; string str(" h asdasf ^& saafa"); it = FIND_FIRST_OF("&az^"); std::cout << *it << std::endl; string::迭代器; 字符串str(“h asdasf^&saafa”); 它=首先找到(“&az^”); 你可以间接地做 auto

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

string::iterator it;
string str("  h asdasf ^& saafa");
it = FIND_FIRST_OF("&az^");
std::cout << *it << std::endl;
string::迭代器;
字符串str(“h asdasf^&saafa”);
它=首先找到(“&az^”);

你可以间接地做

auto pos = str.find_first_of("&az^");
然后推进迭代器

if(pos != std::string::npos) // thanks to @Mike Seymour
    std::advance(it, pos);
我想你也可以用lambda做一些
std::find
,但是上面的方法更简单、更简洁。

试试这个:

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

我认为
std::find_first_of
是您要寻找的

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;
string::迭代器;
字符串str(“h asdasf^&saafa”);
字符串find_me(“&az^”);
it=std::find_first_of(str.begin(),str.end(),find_me.begin(),find_me.end());

std::coutClass
std::string
除了其他查找方法外,还有自己的方法
find_first_of
find_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;
}
下面是另一个演示程序,它查找字符串中在字符文本中指定的所有字符

#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;
}
知道找到的位置后,始终可以在对象中获得相应的迭代器:

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

或者干脆

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

还有一个标准算法
std::find_first_of
在header
中声明,它也可以用于std::string类型的对象。

你最好先检查它不是
npos
。@MikeSeymour谢谢,它被隐式地假定为:)修改了它。它不应该是
str[pos]
(问题是需要一个字符,尽管它提到了‘迭代器’)(it=line.begin();it!=line.end();it++)……我有一个解决方案,可以将begin()更改为smth,比如:for(it=line.find_first_of(“{[],}\”);it!=line.end();it++)@DieterLücking对我来说不清楚OP想要迭代器还是位置。如果只是位置,那么是,
str[pos]
就足够了。这是另一种方式,投票表决。不过,你有一些打字错误,用
替换
中的
首先查找
。简短的回答是:不。迭代器的界面非常狭窄——大部分只是取消引用和
推进
(有
+
这样的成员来推进)您可以考虑使用<代码> STD::ReGeX,它提供迭代器接口。
std::string::iterator it = std::next( s.begin(), pos );
auto it = std::next( s.begin(), pos );
std::string::iterator it = s.begin() + pos;