문제

I try to get the position of the string "-a" with this code

#include <iostream>
#include <string>

using namespace std;

int main(void)
{
string command("test –a string");
size_t pos = command.find("-a");
cout << "position found = " << pos << endl;
}

this produce this output:

position found = 4294967295

If I remove the '-' it's work as expected. 8 is returned.

도움이 되었습니까?

해결책

You get the string::npos value returned, because the library thinks that it cannot find -a in the string.

The reason for this is that you use different dashes a long dash in the string and a short dash - in the search string.

Once you replace the character with the correct one in both places, your code starts working fine (demo).

다른 팁

It means that there are different the first characters.

You can check this using the first characters and placing them in statement

std::cout << ( '–' == '-' ) << std::endl; 

As they are different function find returns value std::string::npos that is defined as std::string::size_type( -1 ) or equal to 4294967295

If you look really close you will find the '–' is no '-'.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top