/** 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. Faster R-CNN论文详解
  2. 程序员如何跳出35岁魔咒,史上最全思维图收集解救你
  3. mysql忘记密码,怎么办?
  4. oracle sh文件怎么打开,Oracle数据库逻辑备份的SH文件
  5. 爱普生690k打印针测试软件_办公室打印机什么牌子好 办公室打印机怎么选购【详解】...
  6. svm各种工具箱(先放着了,省的找起来麻烦^.^)
  7. atitit.ntfs ext 文件系统新特性对比
  8. 素数c语言,C语言素数怎么表示
  9. 计算机组成原理__第6章之硬盘存储器
  10. 负载均衡及其常见实现方式
  11. 电子白板功能的设计与实现
  12. L2-027. 名人堂与代金券,结构体排序
  13. ubuntu 20.04 ssh “Key exchange failed“
  14. 利用SimpleTagSupport创建定制标签
  15. 关于C#英文注释改成中文注释
  16. 智能血压计方案/设计案列/APP/小程序
  17. 油气田勘探数字化转型现状及展望
  18. “那些看似不起波澜的日复一日,终会在某天让你看到坚持的意义。”
  19. 泪目,不枉费挑灯夜战两个月,终于拿到offer了,集合 +Spring+JVM+ 并发 +Redis 总结
  20. CP2K代码分析一:CP2K主程序

热门文章

  1. shp2sde命令行方式向arcsde批量导入数据脚本的生成步骤
  2. linux 安装软件_Linux:其它软件安装方式
  3. c语言受到哪些编程的影响,C语言,一个影响了整个世界的编程语言!
  4. 配置库用户_GEE学习笔记 六十八:【GEE之Python版教程二】配置Python开发环境
  5. 通用技术和信息技术合格考知识点_高二信息与通用技术会考知识点
  6. matlab中的lower,请问:MATLAB中,有实现Gabor变换的函数吗?
  7. 深度学习之基于Tensorflow2.0实现ResNet50网络
  8. 51822模拟ble广播-实践
  9. oracle opaque_transform,oracle databse link
  10. firl 函数 matlab,经典matlab信号处理学习