在 std::string 中查找第一个不是空格的字符 [英] Find the first character that is not whitespace in a std::string
本文介绍了在 std::string 中查找第一个不是空格的字符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
假设我有
std::wstring str(L" abc");
字符串的内容可以是任意的.
The contents of the string could be arbitrary.
如何找到该字符串中不是空格的第一个字符,即在这种情况下'a'的位置?
How can I find the first character that is not whitespace in that string, i.e. in this case the position of the 'a'?
推荐答案
这应该可以做到(C++03 兼容,在 C++11 中你可以使用 lambda):
This should do it (C++03 compatible, in C++11 you can use a lambda):
#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()
.
It returns an iterator, subtract str.begin()
from it if you want an index (or use std::distance
).
这篇关于在 std::string 中查找第一个不是空格的字符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文