简体   繁体   English

比较两个wstring时使用迭代器

[英]Use of an iterator when comparing two wstring

I am iterating through a list and through a vector. 我正在遍历列表和向量。 Both are filled with wstring s. 两者都用wstring填充。

I need to compare the two strings they are pointing at and find out if "wstring1" exists in another "wstring2". 我需要比较它们所指向的两个字符串,并找出“ wstring1”是否存在于另一个“ wstring2”中。

n and k are the two iterators. n和k是两个迭代器。

When I try: 当我尝试:

   wcscmp(*n,*k);

it fails because a const wchar t is ecpected, if I understand the error message right... 如果我理解错误消息,它将失败,因为可以使用const wchar t。

How can I check if *n = "Hello you little fellow" contains *k="little" ? 如何检查* n =“你好小伙伴”是否包含* k =“ little”?

Case does not matter. 大小写无所谓。

PS: I do not want to use the BOOST lib. PS:我不想使用BOOST库。

std::wstring n = L"Hello you little fello";
std::wstring k = L"little";

if ( n.find(k) != wstring::npos )
{
...
}

您可以使用std::wstringc_str()方法以空终止的宽字符字符串访问基础存储。

wcscmp(n->c_str(), k->c_str());
wstring lowN(*n);
wstring lowK(*k);

std::transform(lowN.begin(), lowN.end(), lowN.begin(), ::tolower); 
std::transform(lowK.begin(), lowK.end(), lowK.begin(), ::tolower); 

if ( wstring::npos != lowN->find(lowK) ) {
    // n contains k
}

I see two possibilities: 我看到两种可能性:

1: Use the native find method of wstring: 1:使用wstring的本机查找方法:

n->find(*k);

2: If you want to use wcscmp 2:如果要使用wcscmp

wcscmp(n->c_str(),k->c_str());

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

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