本文主要是介绍c++ utf8 string iterator,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
一边学习一遍糊了一个,代码质量不高,but it works
#include <windows.h>#include <iostream>
#include <string>using namespace std;class stringIter {private:string &str;public:class iterator {private:string &str;string::iterator iter;public:iterator(string &str, string::iterator iter) : str(str), iter(iter) {}bool operator!=(const iterator &other) {return this->iter != other.iter;}const string operator*() {return str.substr(iter - str.begin(), step());}iterator operator++() {iter += step();return *this;}int step() {unsigned char c = *iter;if (c <= 0x7F)return 1;else if ((c & 0xE0) == 0xC0)return 2;else if ((c & 0xF0) == 0xE0)return 3;elsereturn 4;}};stringIter(string &str) : str(str) {}iterator begin() {return iterator(str, str.begin());}iterator end() {return iterator(str, str.end());}
};int main() {string a("abc你好Русский");for (auto &c : stringIter(a)) {cout << c << endl;}return 0;
}
这篇关于c++ utf8 string iterator的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!