空白可以說我有找到的第一個字符是不是在爲std :: string
std::wstring str(L" abc");
字符串的內容可以是任意的。
我該如何找到該字符串中不是空格的第一個字符,即在這種情況下,'a'的位置?
空白可以說我有找到的第一個字符是不是在爲std :: string
std::wstring str(L" abc");
字符串的內容可以是任意的。
我該如何找到該字符串中不是空格的第一個字符,即在這種情況下,'a'的位置?
這應該這樣做(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()
。
使用[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);
+1,但它不是與'auto'兼容的C++ 03。 – Angew
@Angew哦,好吧:)有一些tersity。 – jrok