带有中文的字符串各个字符的获取c++程序
简单易懂,上代码:
#include <iostream>
#include <cstring>
#include <string>
#include <cstdlib>
#include <vector>
using namespace std;class CStr{char *c;typedef struct {int start;bool isChinese;} counter;int cLen;vector<counter> cs;void calculate(){cLen = sizeof("你") - 1;int size = 0;for(int i = 0; i < getlen();){counter ic;ic.start = i;//根据第一个字符是否为负数判断是否为中文。。这个不知道是不是万金油,但是大多数情况应该可以应付。if(c[i] < 0){ic.isChinese = true;i += cLen;}else{ic.isChinese = false;i++;}cs.push_back(ic);}}
public:CStr(const char *cc){cout<<"构造"<<endl;c = new char[strlen(cc) + 1];strcpy(c, cc);calculate();}CStr(const CStr& s){//copycout<<"copy"<<endl;c = new char[s.getlen() + 1];strcpy(c, s.getstr());calculate();}const CStr& operator =(const CStr& s){cout<<"= operator"<<endl;if(&s == this) return s;cs.clear();delete []c;c = new char[s.getlen() +1];strcpy(c, s.getstr());calculate();return *this;}const char *getstr() const{return c;}int getlen() const{return strlen(c);}int getsize() const{return cs.size();}string substr(int start, int len) const{if(start + len > cs.size()){len = cs.size() - start;}int realStart = 0, realLen = 0;for(int j = 0; j < start;j++){if(cs[j].isChinese){realStart += cLen;}else{realStart += 1;}}for(int i = start; i < start + len; i++){if(cs[i].isChinese){realLen += cLen;}else{realLen += 1;}}return string(c).substr(realStart, realLen);}string get(int i)const{if(i > getsize()){cout<<"error in get "<<endl;return string("error");}const counter &cc = cs[i];char *r = NULL;int len = 0;if(cc.isChinese){r = new char[cLen + 1];len = cLen;}else{r = new char[2];len = 1;}memcpy(r, c + cc.start, len);r[len] = '\0';string rs = r;delete[]r;return rs;}void printBytes(const char *start, const char *end) const{//debugint i = 0;do{cout<<(int)*(start+i)<<"\t";}while(start+(++i) < end);cout<<endl;}void traversal() const {//debugfor(int i = 0; i < getsize(); i++){cout<<get(i)<<" ";//printBytes(get(i), get(i) + strlen(get(i)));}cout<<endl;}virtual ~CStr(){delete []c;}
};int main(){CStr str = "中所节快乐阿ajskd~123。1~。31kla加咖啡";str.traversal();cout<<"str.substr(2,3) = "<<str.substr(2,3)<<endl;CStr s1 = str;//copys1.traversal();cout<<"str.substr(2,4) ="<<str.substr(2,4)<<endl;s1 = str;// =s1.traversal();cout<<"str.substr(2,10) ="<<str.substr(2,10)<<endl;
}
相关文章:

C#时间格式化(Datetime)用法详解
Datetime.ToString(String, IFormatProvider) 参数format格式详细用法: 格式字符关联属性/说明dShortDatePatternDLongDatePatternf完整日期和时间(长日期和短时间)FFullDateTimePattern(长日期和长时间)g常规…

python添加数组元素_Python列表附录–如何向数组添加元素,并附带示例说明
python添加数组元素欢迎 (Welcome) Hi! If you want to learn how to use the append() method, then this article is for you. This is a powerful list method that you will definitely use in your Python projects.嗨! 如果您想学习如何使用append()方法&…

学习进度条--第七周
第七周 所花时间(包括上课时间) 10小时(包括上课2小时) 代码量(行) 152 博客量(篇) 2篇(包括团队博客) 了解到的知识点 对组内开发的软件进行讨论&am…

Mybatis获取插入记录的自增长ID
转自:http://blog.csdn.net/tolcf/article/details/39035259 1.在Mybatis Mapper文件中添加属性“useGeneratedKeys”和“keyProperty”,其中keyProperty是Java对象的属性名,而不是表格的字段名。 <insert id"insert" parameter…

android中一种不支持的lua操作
今天写了一段lua代码,在win32中正常运行,在android中运行无效。 大概是这样的: ------file1.lua----- local t {} t.str "this is file1.t" return t ---------------------- -----file2.lua------ local t require &quo…

23岁一无所有怎么办_我搬到国外去创业,然后一无所有。
23岁一无所有怎么办以我的名字还不到一美元,它仍然感觉不像是最低点。 (With not even a dollar to my name, it still didn’t feel like rock bottom.) When you tell someone you’re working for a startup, they’ll either think you’re gonna be really ric…

正则表达式的基本入门
一、正则表达式基本语法 1. 两个特殊的符号‘^’和‘$’。他们的作用分别指出一个字符串的开始和结束。 2. 其他还有‘*’,‘’,‘?’这三个符号,表示一个或一序列字符重复出现的次数 "ab{2}" ---表示一个字符串有一个…

多继承中虚基类构造函数的一种调用规则
规则:如果父类中有虚基类(A),且有一个直接基类(B)是虚基类的子类,那么子类(C或D)若不显式调用虚基类的有参数构造函数,它的直接基类(B)即使在构造列表中调用了非默认构造函数,那么也会直接调用虚基类的默认构造函数。 …

Android 常见异常及解决办法
前言 本文主要记录 Android 的常见异常及解决办法,以备以后遇到相同问题时可以快速解决。 1. java.lang.NullPointerException: Attempt to invoke virtual method void android.widget.TextView.setText(java.lang.CharSequence) on a null object reference 1) …
aws s3 静态网站_如何将静态网站或JAMstack应用托管并部署到AWS S3和CloudFront
aws s3 静态网站S3 and CloudFront are AWS cloud services that make serving static assets powerful and cheap. How can we host a simple static website or JAMstack app on it?S3和CloudFront是AWS云服务,使服务静态资产功能强大且价格便宜。 我们如何在其上…

图像预处理第7步:标准归一化
图像预处理第7步:标准归一化将分割出来的各个不同宽、高的数字字符宽、高统一 //图像预处理第7步:标准归一化 //将分割出来的各个不同宽、高的数字字符宽、高统一 void CChildView::OnImgprcStandarize() {StdDIBbyRect(m_hDIB,w_sample,h_sample);//在…

8. 进制转化的函数
一,表示进制的单词 bin:二进制 oct:八进制 dec:十进制 hex:十六进制二,四种进制的数据表示方式 $bin0b1010; //二进制数字写法(暂时不学 ) …

二叉树广度优先遍历
#include <iostream> using namespace std;struct Node{//二叉树节点int value;Node *left;Node *right; };struct queue{//辅助队列int head;int tail;int len;//队列长度,遍历时用Node ** list;//队列内容void push(Node *n){list[tail] n;len;}Node * pop…

phaser.min.js_如何使用Phaser 3,Express和Socket.IO构建多人纸牌游戏
phaser.min.jsIm a tabletop game developer, and am continually looking for ways to digitize game experiences. In this tutorial, were going to build a multiplayer card game using Phaser 3, Express, and Socket.IO.我是桌面游戏开发人员,并且一直在寻找…

VirtualBox - RTR3InitEx failed with rc=-1912 (rc=-1912)
有一天重启电脑后虚拟机virtual box突然打不开了,提示类似 https://askubuntu.com/questions/900794/virtualbox-rtr3initex-failed-with-rc-1912-rc-1912 参考帖子中查看了一下包的情况dpkg --list virtualbox-* | grep ii 结果:ii virtualbox-dkms …

边工作边刷题:70天一遍leetcode: day 27
Permutation Sequence 原理:一个permutation是n位,在第i位的值取决于有多少个i-1位的组合。这i-1位的组合是在高位pick完之后剩下的数中 细节: 不同于decimal,位数是固定的,所以不能用k>0作为循环条件(这…

基本数据结构(图: 基本结构,DFS,prim算法, kruskal算法)
#include <iostream> using namespace std; //约定: //1. 图是由很多节点(VERTEX)构成的, 因此图结构是由一个VERTEX的链表构成的, 每个VERTEX则需要有一个id,也就是start, 取start是为了跟LINE更直观地结合。 //2. 每个节点关联着很多(LINE)构成,因此每个VER…
gatsby_如何使用Gatsby和Leaflet创建夏季公路旅行地图绘制应用程序
gatsbyGet ready for the summer by building your own road trip mapping app with this step-by-step guide!通过此逐步指南,构建自己的公路旅行地图应用,为夏天做好准备! What are we going to build? 我们要建造什么? What …

NEFU 1146 又见A+B
又见ab Problem:1146 Time Limit:1000ms Memory Limit:65535K Description 给定两个非负整数A,B,求他们的和。 Input 多组输入,每组输入两个非负整数A和B(0<A,B<10^3000),可能会有前缀0,但保证总长度不超过3000…

图的最短路径dijkstra算法
想法是这样的: 1. 最开始要建立4个list,分别存储 a. 所有的Vertex: allVertex[] b. 一个空的Vertex list: emptyVertex[] c. 一个前缀表 previous list(用来回溯路径用): previous[] d. 一个表示最短距离的表(就是表示某个点与0点的最短距离)࿱…

JDBC数据源连接池(1)---DBCP
何为数据源呢?也就是数据的来源。我在前面的一篇文章《JDBC原生数据库连接》中,采用了mysql数据库,数据来源于mysql,那么mysql就是一种数据源。在实际工作中,除了mysql,往往还会有Oracle,sql se…
如果成为一名高级安卓开发_什么是高级开发人员,我如何成为一名开发人员?
如果成为一名高级安卓开发Becoming a Senior Developer is something many of us strive for as we continue our code journey and build our career. But what does it actually mean to be a "Senior" Developer?成为一名高级开发人员是我们许多人在继续我们的代…

拍牌神器是怎样炼成的(三)---注册全局热键
要想在上海拍牌的超低中标率中把握机会、占得先机,您不仅需要事先准备好最优的竞拍策略,还要制定若干套应急预案,应对不时之需。既定策略交给计算机自动执行,没有问题。可是谁来召唤应急预案呢?使用全局热键应该是个不…

eclipse 变成中文
官方下载 http://www.eclipse.org/babel/downloads.php 按照自己的eclipse版本下载对应的 复制链接 到eclipse ->help->Install New Software 勾选自己的语言包 如: 等待 安装完成 ,无过不好用 更改 右键 属性 更改位置 加后缀 D:\xinle_eclips…

框架模式与设计模式之区别
http://my.oschina.net/u/991183/blog/109854 有很多程序员往往把框架模式和设计模式混淆,认为MVC是一种设计模式。实际上它们完全是不同的概念。框架、设计模式这两个概念总容易被混淆,其实它们之间还是有区别的。框架通常是代码重用,而设计…
村上春树 开始写作_如何克服对写作的恐惧并找到开始的动力
村上春树 开始写作Writing about our work is one of those things that most of us have on our to-do list. But whether its due to procrastination or fear, we never actually get to it. Heres some more motivation and reasons why you should give it a shot!撰写我们…

一个基于组件的动态对象系统
http://hulefei29.iteye.com/blog/1490889 一、静态的痛苦 作为一个项目经验丰富的程序员,你经常会遇到游戏开发过程中的“反复”(iterations):今天美术将一个静态的模型改为骨骼模型并添加了动画;明天企划会议上决定把所有未拾取武器由…

Lua生成Guid(uuid)
全局唯一标识符(GUID,Globally Unique Identifier)也称作 UUID(Universally Unique IDentifier) 。GUID是一种由算法生成的二进制长度为128位的数字标识符。GUID主要用于在拥有多个节点、多台计算机的网络或系统中。在理想情况下,…

c:if标签的使用
1、标签的基本介绍 <c:if> 标签必须要有test属性,当test中的表达式结果为true时,则会执行本体内容;如果为false,则不会执行。例如:${requestScope.username admin},如果requestScope.username等adm…

ecs和eks 比较_如何使用Kubernetes,EKS和NGINX为网站设置DNS
ecs和eks 比较As the creator of Foo, a platform for website quality monitoring, I recently endeavored in a migration to Kubernetes and EKS (an AWS service).作为网站质量监控平台Foo的创建者,我最近努力迁移到Kubernetes和EKS(一种AWS服务)。 Kubernetes…