Misc string test

/** 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.
请按任意键继续. . .










本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/243683.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

tiny4412 SDK1312B LED驱动

查看原理图和4412芯片手册&#xff0c;相关寄存器如下&#xff1a; GPM4CON&#xff1a;0x1100 02E0 GPM4DAT&#xff1a;0x1100 02E4

鲁九的有趣照片之一

在火车上也不忘了杀人&#xff0c;各个都想当凶手&#xff0c;当不上的就无聊到假寐&#xff0c;或者睁一只眼闭一只眼&#xff0c;事不关己高高挂起样子。其实凶手只有两个&#xff0c;谁都可以是&#xff0c;谁都可以不是。 喝水也要整齐划一&#xff0c;步调一致。 述强老弟…

RGB 转 YUV 算法

RGB 转 YUV 算法 `timescale 1ns / 1ps /* RGB 转 YUV 算法 计算公式: Y = 0.183R + 0.614G + 0.062B + 16; CB = -0.101R - 0.338G + 0.439B + 128; CR = 0.439R - 0.399G - 0.040B + 128; 其中,时序在计算过程中完全没有用到 输入到输出有三个 clock 的时延。 第一级流水线…

我在河南安阳拍摄的一个山村小孩儿

在河南考察。发现一个可爱的小男孩&#xff0c;于是就摆出架势拍了这一组照片。谁想“螳螂捕蝉黄雀在后”&#xff0c;我的一举一动被述强兄给偷拍下来。他说我的姿势还算专业。 这个小孩很茫然。不知道我在干什么&#xff1f; 哦&#xff0c;原来在照相啊。 突然看见一个小女孩…

通过xshell远程连接ubuntu

sudo apt install openssh-server

multi-byte wide-char

/** C 字符串的一点点转换*/ #include <iostream> #include <string> #include <cstring> #include <cstdlib> #include <clocale> //setlocale #include <Windows.h> #include <WinCon.h> using namespace std; //using namespace …

中值滤波器

中值滤波器 `timescale 1ns / 1ps module median_filtering(input clk,input rst_n,input [15:0] data_in,input data_in_en,input hs_in,input vs_in,output [15:0] data_out,output data_out_en,output hs_out,output vs_out);wire [15:0] line0; wire [15:0] line1; wire […

台湾印象之一:金马奖之夜

本月初赴台北参加由金马影展主席焦雄屏主持的“2008合作制片会议”&#xff0c;并出席了第45届金马奖颁奖典礼。走过红地毯&#xff0c;回首众多明星星光灿烂&#xff0c;鱼贯而入。当晚最耀眼的莫过于李安与舒淇组合。刚一进门就被记者包围&#xff0c;不能挪步。我也凑趣&…

台湾印象之三:吃与喝

梅子餐厅是我吃过的最有味道的小店。老板是一个白净也很有风度的老阿婆。这里的担仔面是一绝。还有炒肝&#xff0c;甜油而不腻&#xff0c;口感极妙。 金马影展主席焦雄屏女士在梅子餐厅宴请我和大为导演&#xff0c;还有几位内地来的女演员和电影官员。 度小月&#xff0c;非…

ubuntu16.04安装CecureCRT 并破解

下载ubuntu16可用的CecureCRT 注册机 下载需要2积分 下载完后执行 tar -xvf scrt-8.1.1.1319.ubuntu16-64.tar.gz $wget http://download.boll.me/securecrt_linux_crack.pl $sudo perl securecrt_linux_crack.pl /usr/bin/SecureCRT [sudo]password for root: crac…

AXI总线协议介绍

AXI总线协议介绍 AXI(Advanced eXtensible Interface)协议是一种面向高性能、高带宽系统设计的总线协议,能够满足各种高速系统的总线协议,能够满足各种高速系统的总线互连。 AXI协议的主要特点有: 独立的地址,控制和数据接口支持使用字节选通的不对齐数据的传输基于特定地…

VS2010支持C++11(原来的C++0x标准)

为什么电脑装上VS2010卡的要死&#xff0c;是电脑配置太垃圾了吗&#xff1f;是的吧

台湾印象之四:风流人物

台湾导演丁仰国&#xff08;作品《还珠格格》&#xff09;&#xff0c;大陆演员邵兵&#xff08;作品《红河谷》&#xff09;。 《海角七号》的导演魏德圣从李安和林青霞手里接过台湾地区最具人气电影奖。林青霞老了&#xff0c;可星光依旧。 周迅依然很迷人。她的《画皮》《女…

关于某公司冒充InfoQ中文站举办活动的声明

近日本网站发现有公司以InfoQ中文站的名义&#xff0c;给一些作者/专家的单位打电话联系&#xff0c;邀请其参加该公司举办的某技术英雄会活动。事后经作者/专家核实&#xff0c;这家公司为猎头公司&#xff0c;据分析其目的是收集作者/专家的信息。\u0026#xD;\nInfoQ中文站郑重…

如何映射本地虚拟机或远程服务器磁盘到本地

无论是用超级终端登陆虚拟机或服务器&#xff0c;还是映射虚拟机或服务器的磁盘到本地&#xff0c;都需要安装openssh-server&#xff0c;命令如下&#xff1a; ningubuntu:~$ sudo apt install openssh-server 下面进入正题。 当然&#xff0c;你可以使用vmware tools来实现本…

递归 将一个整数逆序

//将一个整数int倒转过来 #include <iostream> using namespace std;//利用引用 void IntRev(int nsrc, int &ndes) {if (0 nsrc){return;}else{ndes ndes*10 nsrc%10;IntRev(nsrc/10, ndes);} }//利用局部static变量只初始化一次的特性 int IntRev2(int num) {st…

MATLAB数据分析2

MATLAB数据分析2 %特殊矩阵%通用的特殊矩阵 %zeros函数:产生全0矩阵,即零矩阵 %格式: %zeros(m):产生mxm零矩阵 %zeros(m,n):产生mxn零矩阵 %zeros(size(A)):产生与矩阵A同样大小的零矩阵 % A = zeros(2,3) % B = zeros(size(reshape(A,3,2)))%ones函数:产生全1矩阵,即幺矩…

奥尼尔的话剧《榆树下的欲望》

好久没看话剧了。今天在人艺小剧场看了美国现代剧作家尤金奥尼尔&#xff08;Eugene ONeill,1888-1953&#xff09;的话剧《榆树下的欲望》。导演是任鸣。主要演员分别是郑天玮、张志忠、王雷等。这部戏是奥尼尔最著名的悲剧之一&#xff0c;据说曹禺先生当年写《雷雨》的时候&…

简单计算题

#include <iostream> #include <iomanip> //setprecision 设置输出精度 #include <cmath> using namespace std;//递增的函数 double fun(double x) {return (x pow(x, 2) pow(x, 3) pow(x, 4) pow(x, 5)); }int main(int argc, char *argv[]) {doubl…

台湾印象之五:宝岛万象

刚到台中&#xff0c;午饭时间&#xff0c;记者围住一位美女——张榕容&#xff0c;电影《渺渺》的演员&#xff0c;入围了本届金马奖“新人奖”。据说她是中国和荷兰的混血。 制片会议间隙表演的节目。一个男扮女装的舞者。 街头一只双目失明的老狗&#xff0c;被一个小孩子嬉…