gpt4 book ai didi

c++ - 比较字符串迭代器和字符指针

转载 作者:塔克拉玛干 更新时间:2023-11-02 23:50:30 33 4
gpt4 key购买 nike

我在函数中有一个 const char * const string。我想用它来比较字符串中的元素。

我想遍历字符串,然后与 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;
}
}

如何解析 char* 以解析字符串迭代器?

谢谢..

最佳答案

std::string::find :

const char* bar = "bar";
std::string s = "foo bar";

if (s.find(bar) != std::string::npos)
cout << "found!";

关于c++ - 比较字符串迭代器和字符指针,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2058271/

33 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com