我在函数中有一个const char*const字符串.我想用它来比较字符串中的元素.
我想迭代字符串,然后与char*进行比较.
#include <iostream>
#include <string>
#include <cstring>
using namespace std;
int main()
{
const char * const pc = "ABC";
string s = "Test ABC Strings";
string::iterator i;
for (i = s.begin(); i != s.end(); ++i)
{
if ((*i).compare(pc) == 0)
{
cout << "found" << endl;
}
}
Run Code Online (Sandbox Code Playgroud)
如何解析char*以解决字符串迭代器?
谢谢..
Ida*_*n K 16
const char* bar = "bar";
std::string s = "foo bar";
if (s.find(bar) != std::string::npos)
cout << "found!";
Run Code Online (Sandbox Code Playgroud)
std::string::iterator it;
char* c;
if (&*it == c)
Run Code Online (Sandbox Code Playgroud)
取消引用迭代器会产生对指向对象的引用.所以取消引用会给你一个指向对象的指针.
编辑
当然,这不是很相关,因为更好的方法是完全放弃比较,并依靠find已经存在的功能来做你想要的.