/** std::string深入详解* Visual Studio 2008Sp1, 使用Ctrl + F5启动调试*/
#include <iostream>
#include <string>
#include <cstdio>
#include <cstddef>
#include <cstring>
#include <cstdlib>   //qsort
#include <errno.h>        /* Error Codes */
#include <numeric>
#include <algorithm>
using namespace std;//#define _CRT_SECURE_NO_WARNINGS with /D
#pragma warning(disable:4996)  //disable stupid warning of This function or variable may be unsafe. //Consider using strerror_s instead#define  MAXLEN 1000
char line[MAXLEN];int getline(char s[], int lim)
{int ch = 0, i;i = 0;while(--lim > 0 && ((ch = getchar()) != EOF) && ch != '\n')s[i++] = ch;if (ch =='\n')s[i++] = ch;s[i] = '\0';return i;
}//raise
int cmp(const void *a, const void *b)
{return *(char *)b - *(char *)a;
}int main(int argc, char *argv[]){const basic_string<char> s1("test something");string s2("test");// The first member function  C-stringstring stra("hello ");const char *cstra = "c-string";stra.append(cstra);cout<<"Appending the C-string cstra to string stra gives: "<<stra<<endl<<endl;The second member functionconst char *cstrb = "some elsd";stra.append(cstrb,2);cout<<stra<<endl<<endl;cout<<"input a character string"<<endl;if(getline(line, MAXLEN) < 1)exit(1);//quick sortcout<<"before line: "<<line<<endl;qsort(line, sizeof(line)/sizeof(line[0]), sizeof(line[0]), cmp);cout<<"quick sort line: "<<line<<endl;///string 测试cout<<"测试string ss,下面给出ss的字符串:"<<endl<<endl;string ss("    maybe you are long long girl, i'm who are you.12563.");cout<<ss<<endl<<endl;cout<<ss.find_first_not_of(' ')<<endl;     //前面有很多空格,查找第一个非空格的cout<<ss.find_first_not_of("abcdefghijkmno")<<endl;  //返回第一个不在指定字符集里面的元素位置cout<<ss.find_last_not_of("abcdefghijkmno")<<endl;cout<<ss.find("longk")<<endl;   //如果string中没有查找的内容,他会超出范围的去查找cout<<"计算string ss中的 o 个数:"<<count(ss.begin(), ss.end(), 'o')<<endl;cout<<"string ss的长度: "<<ss.size()<<",  string ss中的字母和数字: "<<count_if(ss.begin(), ss.end(), isalnum)<<endl;  //条件比较,ctype.h isalnum判断字母或者是数字////strcspn && strspncout<<endl<<endl<<endl;const char *pszTest = "long long ago, there is girl, she\'name is little redhat";cout<<"\r\n测试strcspn()函数,待测试的字符串pszTest: "<<pszTest<<endl;cout<<"长度:"<<strlen(pszTest)<<"---firt_not_of length `xyza` "<<strcspn(pszTest, "xyza")<<endl;////test  strtokchar str[] = "now#is the time for all#####good men to come to the#aid of their country\0";char *delims = "#";char *token = NULL;cout<<"\n\n测试strtok函数(linux下请使用函数·char *strsep (char ** __stringp, const char * __delim)·),""待测试的字符串:\n"<<str<<endl<<endl;token = strtok(str, delims);   //线程不安全的函数, 列外str被破坏掉了while(token != NULL){printf("%s\n", token);token = strtok(NULL, delims);}////memchrcout<<"\n\n\n测试函数void * memchr (void * ptr, int value, size_t num );\r\n"<<endl;char *pch;int ch;ch = 'e';strcpy(str, "now#is the time for all#####good men to come to the#aid of their country" );cout<<"The test str:\n"<<str<<endl;pch = (char *)memchr(str, ch, strlen(str));if (pch != NULL){printf(">>Character \'%c\' is found at %d.\n", ch, pch - str +1);printf(">>%s\n", pch);}else{printf("Fuck, Character \'%c\' is NOT found!\n", ch);}cout<<"\n\n"<<endl;////strerrorcout<<"测试strerror(),创造一个error: press any key continue..."<<endl;cin.get();//for(int err = 1; err < 42; err++){// printf("Error code%d: %s\n", err, strerror(err)); //}FILE *file;file = fopen("unexist.file", "r");if(file == NULL)  printf("Open file crashed, Error code %d: %s\n", errno, strerror(errno));/////strcasecmpcout<<"\n\n测试strcasecmp()函数,输入一只动物吧!(dog,cat,etc...)"<<endl;/*使用自己的C函数*/if(getline(line, MAXLEN) >1){line[strlen(line) - 1] = '\0';    //delete new line/* do something compare */if (stricmp(line, "dog") == 0){  //stricmp实质是引用了string.h中的strcasecmp()函数,坑爹啊printf("Dog is very interesting....en?\n");}else if (stricmp(line, "cat") == 0){printf("Actually, I do not like cats.\n");}else if(stricmp(line, "cow") == 0){printf("Cattle (colloquially cows) are the most common type of large domesticated ungulates.""They are a prominent modern member of the subfamily Bovinae, are the most widespread ""species of the genus Bos, and are most commonly classified collectively as Bos primigenius."" Cattle are raised as livestock for meat (beef and veal), as dairy animals for milk and ""other dairy products, and as draft animals (oxen / bullocks) (pulling carts, plows and ""the like). Other products include leather and dung for manure or fuel. In some countries, ""such as India, cattle are sacred. From as few as eighty progenitors domesticated in ""southeast Turkey about 10,500 years ago, it is estimated that there are now 1.3 billion ""cattle in the world today.\n");}else if (stricmp(line, "pig") == 0){printf("A pig is any of the animals in the genus Sus, within the Suidae family of even-toed ""ungulates. Pigs include the domestic pig, its ancestor the wild boar, and several other ""wild relatives. Pigs are omnivores and are highly social and intelligent animals.\n");}else{printf("%s ,?what animal it was??\n", line);}}//使用istream对象, std::string convert to C-style stringcout<<"输入一些什么东西吧\n"<<endl;std::string szline;std::getline(std::cin, szline);   //don't be std::cinstrcpy(line, szline.c_str());printf(">>%s\n", line);/////下面看看一个typedef与const的结合typedef std::string *pstring;//const pstring mystring;     //error, 因为mystring变量是const类型的,先要初始化//const int jj;              //errorstd::string mystr1("got some string here.");std::string mystr2("wow,i got the second string.");const pstring mystring = &mystr1;//mystring = &mystr2;  //error 指针mystring只是指向mystr1的cout<<"mystring: \n"<<*mystring<<endl;mystring->append(" some append string.");cout<<"after append of mystring:\n"<<*mystring<<endl;return 0;
}

输出结果

Appending the C-string cstra to string stra gives: hello c-stringhello c-stringsoinput a character string
long long ago, there is girl, she's name is little redhat..
before line: long long ago, there is girl, she's name is little redhat..quick sort line: ttttssssrrrooonnnmllllliiiihhhggggeeeeeedaaa..,,'测试string ss,下面给出ss的字符串:maybe you are long long girl, i'm who are you.12563.4
0
55
4294967295
计算string ss中的 o 个数:5
string ss的长度: 56,  string ss中的字母和数字: 39测试strcspn()函数,待测试的字符串pszTest: long long ago, there is girl, she'nameis little redhat
长度:55---firt_not_of length `xyza` 10测试strtok函数(linux下请使用函数·char *strsep (char ** __stringp, const char *__delim)·),待测试的字符串:
now#is the time for all#####good men to come to the#aid of their countrynow
is the time for all
good men to come to the
aid of their country测试函数void * memchr (void * ptr, int value, size_t num );The test str:
now#is the time for all#####good men to come to the#aid of their country
>>Character 'e' is found at 10.
>>e time for all#####good men to come to the#aid of their country测试strerror(),创造一个error: press any key continue...Open file crashed, Error code 2: No such file or directory测试strcasecmp()函数,输入一只动物吧!(dog,cat,etc...)
pig
A pig is any of the animals in the genus Sus, within the Suidae family of even-t
oed ungulates. Pigs include the domestic pig, its ancestor the wild boar, and se
veral other wild relatives. Pigs are omnivores and are highly social and intelli
gent animals.
输入一些什么东西吧lol.file
>>lol.file
mystring:
got some string here.
after append of mystring:
got some string here. some append string.
请按任意键继续. . .

Misc string test相关推荐

  1. hBuilder天蓝主题插件

    自己创建个文本把代码复制进去,然后把文件名改成.tmTheme 的格式,自己导入到hBuilder主题中就可以了 <?xml version="1.0" encoding=& ...

  2. Unity客户端框架收集

    Unity框架搜集 https://blog.csdn.net/t163361/article/details/106499225 Loxodon Framework https://github.c ...

  3. unity客户端开源框架

    TinaX Framework 链接:https://github.com/yomunsam/TinaX/tree/master TinaX 主要实现了以下功能: Lua 语言支持 出于普遍的热更新需 ...

  4. 《javascript 语言精粹》精华部分

    第1章 精华 JavaScript的特性中有一部 分特性带来的麻烦远远超出它们的价值.其中,一些特性是因为规范很不完善,从而可能导致可移植性的问题:一些特性会导致生成难以理解和修改的代码:一些特 性促 ...

  5. HBuilder ,及自用主题

    HBuilder ,及自用主题 字体:Consolas http://bbs.csdn.net/topics/390858585  让代码更美:你最爱的编程字体 http://www.dcloud.i ...

  6. Java知识——精华总结

    Java知识--精华总结 一.java概述与基础知识 1.何为编程? 编程就是让计算机为解决某个问题而使用某种程序设计语言编写程序代码,并最终得到结果的过程. 为了使计算机能够理解人的意图,人类就必须 ...

  7. OpenJDK1.8 :java/lang/NoSuchMethodError‘: Method sun.misc.Unsafe.defineClass(Ljava/lang/String;[BII)

    记录一个OpenJDK1.8的一个BUG : Crash日志 报错信息 : Event: 0.078 Thread 0x00007f1160055800 Exception <a 'java/l ...

  8. 使用RSA私钥或pfx私钥签名String

    项目有个需求,使用私钥签名请求body内容,放在请求头部,作为头部一个字段内容请求外部服务,签名有二种方式,对方提供私钥串/直接提供pfx私钥文件. 一. 提供私钥串  示例代码如下: public ...

  9. 深入解析String#intern

    为什么80%的码农都做不了架构师?>>>    引言 在 JAVA 语言中有8中基本类型和一种比较特殊的类型String.这些类型为了使他们在运行过程中速度更快,更节省内存,都提供了 ...

最新文章

  1. 速领!抗疫大礼包(含QQ音乐、全民K歌、网易云音乐等等)
  2. Python面向对象程序设计之抽象工厂模式之二-一个更加pythonic的抽象工厂
  3. SM37job状态意义
  4. php把400个数组建二维,请教怎么将多维数组转换为二维数组
  5. import package的问题
  6. 不是有效的函数或过程名_过程和函数
  7. python带cookie发包demo
  8. mysql innobackupex 备份及恢复
  9. 搭建 Ubuntu 可视化界面
  10. Excel快捷键大全
  11. ByteBuffer的原理和使用详解
  12. 关于ASP木马提升权限
  13. 特殊纪念日Android APP内设置黑灰色背景
  14. android pad的屏幕纯多少,16:9比例10.1寸屏幕,Galaxy Tab S4可以说是一部好的安卓平板...
  15. css碎步测量,隧洞测量实习日记.doc
  16. jvm 性能调优之 jmap
  17. 涨价、盈利、IPO?共享充电宝没你想象得好过!
  18. 使用git控制word版本
  19. IBM合作伙伴世界峰会:将全部筹码都押在认知计算上
  20. signature=b0de5b058018aa87bad0e19868c78dad,来用百度密语吧!!!

热门文章

  1. 人工大脑项目 —— Nengo
  2. 扔掉伟哥!男性壮阳食品荟萃
  3. JSTL的错误“attribute test does not accept any expressions”解决方法
  4. php框架所用到的核心概念,【PHP】PHP现代框架代表-Laravel框架核心技术特性
  5. oracle缺失值表示,Oracle SQL,用最接近的非缺失填充缺失值
  6. 计算机指令系统课件,计算机组成原理课件05指令系统.ppt
  7. css盒子模型圆形运用,【前端】CSS3学习笔记(三)——盒子模型
  8. 深度学习之基于opencv和CNN实现人脸识别
  9. python模拟ajax请求_短信炸弹—用Python模拟ajax请求
  10. 『设计模式』撩妹秘籍竟是使用设计模式的抽象工厂模式