2014-03-26 28 views

回答

3

這應該這樣做(C++ 03兼容,在C + +11你可以使用拉姆達):

#include <cwctype> 
#include <functional> 

typedef int(*Pred)(std::wint_t); 
std::string::iterator it = 
    std::find_if(str.begin(), str.end(), std::not1<Pred>(std::iswspace)); 

如果您需要索引(或使用std::distance),它將返回一個迭代器,從中減去str.begin()

+0

+1,但它不是與'auto'兼容的C++ 03。 – Angew

+0

@Angew哦,好吧:)有一些tersity。 – jrok

2

使用[std::basic_string::find_first_not_of][1]功能

std::wstring::size_type pos = str.find_first_not_of(' '); 

pos是3

更新:找到任何其他字符

const wstring delims(L" \t,.;"); 
std::wstring::size_type pos = str.find_first_not_of(delims); 
+0

這是否適用於所有空白字符,如'\ n'? – Christian

+0

@Christian是的,find_first_not_of有一些重載。看到我的更新,希望它有助於 – billz

相關問題