1

I am iterating through a list and through a vector. Both are filled with wstring s.

I need to compare the two strings they are pointing at and find out if "wstring1" exists in another "wstring2".

n and k are the two iterators.

When I try:

   wcscmp(*n,*k);

it fails because a const wchar t is ecpected, if I understand the error message right...

How can I check if *n = "Hello you little fellow" contains *k="little" ?

Case does not matter.

PS: I do not want to use the BOOST lib.

Lightness Races in Orbit
  • 378,754
  • 76
  • 643
  • 1,055
Lumpi
  • 2,697
  • 5
  • 38
  • 47
  • _Why_ do you not want to use boost? – Lightness Races in Orbit Oct 21 '11 at 11:18
  • Because I am not allowed to install it.... sorry – Lumpi Oct 21 '11 at 11:19
  • 2
    @TomalakGeret'kal: why do you care? – sehe Oct 21 '11 at 11:28
  • 2
    @sehe: I don't. I'm leading the OP to the part where I either prove that this is homework, or persuade him/her to cast away the shackles of pointless restrictions based on misunderstandings. Why do you care why I care? – Lightness Races in Orbit Oct 21 '11 at 11:30
  • Im 32, it's NOT homework... ;-) I'm not working on my machine right now.. – Lumpi Oct 21 '11 at 11:35
  • 4
    @TomalakGeret'kal: I care because it doesn't work constructively. I know how you are _trying to_ get it to work constructively, but as it stands it just leads to repeated 'ugly comment streams' - making SO _less_ attractive IMO (besides for this question it is irrelevant, see the answers) – sehe Oct 21 '11 at 11:36
  • @sehe: How ironic that you just took up so much space with this "ugly comment stream" that doesn't even have a constructive end goal. I don't care that the best solution doesn't involve Boost: I want to teach and help language newcomers to improve. That's why I'm here. Why are _you_ here? – Lightness Races in Orbit Oct 21 '11 at 11:41
  • To lead the OP to an answer :) (and learning in the process) – sehe Oct 21 '11 at 11:51

4 Answers4

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

if ( n.find(k) != wstring::npos )
{
...
}
AndersK
  • 35,813
  • 6
  • 60
  • 86
  • 1
    Note that you'd have to convert both strings to the same case to amke this insensitive to case differences (as the OP requested). See [this question](http://stackoverflow.com/questions/313970/stl-string-to-lower-case) for how to do that. – Björn Pollex Oct 21 '11 at 11:24
3

You can use the c_str() method of the std::wstring to access the underlying storage as a null terminated wide-char string.

wcscmp(n->c_str(), k->c_str());
Sylvain Defresne
  • 42,429
  • 12
  • 75
  • 85
1
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
}
IronMensan
  • 6,761
  • 1
  • 26
  • 35
0

I see two possibilities:

1: Use the native find method of wstring:

n->find(*k);

2: If you want to use wcscmp

wcscmp(n->c_str(),k->c_str());
simon
  • 153
  • 2
  • 7