string.find()函数、 strncat函数、strncmp函数的用法
来源:互联网 发布:网红经济学数据 编辑:程序博客网 时间:2024/05/22 08:29
string.find()函数
类string提供了大量查找功能和搜索功能,其中比较常用的查找和搜索函数是find()函数、
find_first_not_of()函数、find_first_of()函数、find_last_not_of()函数、find_last_of()函数、rfind()等。
find()函数的语法如下所示:
(1) size_type find(E c, size_type pos = npos) const;用来查找单个字符在字符串中出现的位置并返回
该位置基于0的索引值。
(2) size_type find(const E *s, size_type pos = npos) const;用来查找以空字符结尾的字符数组在
字符串中出现的位置并返回该位置基于0索引值。
(3) size_type find(const E *s, size_type pos, size_type n = npos) const;用来查找以空字符结尾的
字符数组在字符串中出现的位置并返回该位置基于0索引值,它是从npos开始查找的。
(4) size_type find(const basic_string &str, size_type pos = npos) const;用来查找字符串并且返回
该搜索的字符串的基于0索引值的位置值,它是从npos开始查找的。
find()函数的功能是从std::string对象的头部顺序找目标值,如果找到返回该目标值出现的位置,如果没有在
字符串对象中找到目标对象,返回值为-1。
string类find函数返回值判定
代码示例:
- int main()
- {
- string s = "Alice Bob Charlie";
- size_t position;
- position = s.find("none");
- if (position >= 0)
- cout << "Found! position is : " << position << endl;
- else
- cout << "Not found!" << endl;
- }
现象&后果
程序运行结果输出"Found! position is : 4294967295",但实际上所找的字符串"none"并不存在于字符串s中。
Bug分析
程序的目的是,在源字符串s中查找目的字符串,若找到,则显示"Found",并返回目标子串在源字符串中的位置;反之,若未找到,则返回"Not found"。string.find在未找到时会返回string::npos。
在C++中常量npos是这样定义的:
- static const size_t npos = -1;
即常量npos定义的值为-1. 但又因为npos 的类型size_t是无符号整数类型,所以npos实际上是一个正数,并且是size_t类型的最大值。
上述代码中,把find函数返回的值赋给size_t类型的变量position,而size_t类型的变量position是永远大于等于0,所以即使find返回npos,if条件也为true。
正确的做法是在if条件中直接用npos作比较。
正确代码
- int main()
- {
- string s = "Alice Bob Charlie";
- size_t position;
- position = s.find("none");
- if (position != string::npos)
- cout << "Found! position is : " << position << endl;
- else
- cout << "Not found!" << endl;
- }
应用举例:http://blog.csdn.net/enjoying_science/article/details/47067093
strncat函数
函数原型:extern char *strncat(char *dest,char *src,int n)
参数说明:src为源字符串,dest为目的字符串,n为指定的src中的前n个字符。
所在库名:#include <string.h>
函数功能:把src所指字符串的前n个字符添加到dest结尾处,覆盖dest结尾处的'/0',实现字符串连接。
返回说明:返回指针,连接后的字符串。
其它说明:暂时无。
实例:
#include <stdio.h>
int main()
{
char str1[100]="SKY2098,persist IN DOING AGAIN!";
char *str2="sky2098,must be honest!";
int n=15;
char *strtemp;
strtemp=strncat(str1,str2,n); //将字符串str2中的前n个字符连接到str1的后面
printf("The string strtemp is: %s ", strtemp);
return 0;
}
在VC++ 6.0 编译运行:
实现了指定某个字符串中的字符连接到另一个字符串上的操作。
strncmp函数
函数原型:extern int strcmp(char *str1,char * str2,int n)
参数说明:str1为第一个要比较的字符串,str2为第二个要比较的字符串,n为指定的str1与str2的比较的字符数。
所在库名:#include <string.h>
函数功能:比较字符串str1和str2的前n个字符。
返回说明:返回整数值:当str1<str2时,返回值<0; 当str1=str2时,返回值=0; 当str1>str2时,返回值>0。
其它说明:暂时无。
实例:
#include<stdio.h>
int main()
{
char *str1="Hello,I am sky2098,I liking programing!";
char *str2="Hello,I am sky2098,gramk has gone。";
int n=13; //指定比较前13个字符
int inttemp;
inttemp=strncmp(str1,str2,n); //将字符串比较的返回值保存在int型变量inttemp中
if(inttemp<0)
{
printf("strlen(str1) < strlen(str2)");
}
else if(inttemp>0)
{
printf("strlen(str1) > strlen(str2)");
}
else
{
printf("strlen(str1) == strlen(str2)");
}
return 0;
}
在VC++ 6.0编译运行:
程序中,只对str1和str2的前13个字符进行比较,发现它们的字典序相等,则打印出相等的消息。
再看另一个例子:
#include<stdio.h>
int main()
{
char *str1="Hello,I am sky2098,I liking programing!";
char *str2="Hello,I am sky2098,gramk has gone。";
int n=strlen(str2);
int inttemp;
inttemp=strncmp(str1,str2,n); //将字符串比较的返回值保存在int型变量inttemp中
if(inttemp<0)
{
printf("strlen(str1) < strlen(str2)");
}
else if(inttemp>0)
{
printf("strlen(str1) > strlen(str2)");
}
else
{
printf("strlen(str1) == strlen(str2)");
}
return 0;
}
在VC++ 6.0编译运行:
转自:http://blog.csdn.net/sky2098/article/details/1532586
- string.find()函数、 strncat函数、strncmp函数的用法
- 模拟输出受限制的字符串函数-strncat,strncmp,strncpy
- strncmp, strncat, strncpy函数的定义与实现
- strcmp,strncmp,memcmp,strcat,strncat函数实现
- string.find()函数用法
- 模拟实现C语言strncpy.strncat.strncmp函数
- strcpy、strcat函数源码,strncpy,strncat,strncmp,strtok
- 【C语言】模拟实现strncpy、strncat、strncmp函数
- 【C语言】模拟实现strncpy strncat strncmp strchr strrchr函数
- 模拟实现 strncpy strncat strncmp strchr strrchr 函数
- C语言~模拟实现strncpy、strncat、strncmp函数
- strncmp函数的应用
- strncmp函数的使用
- 从0开始<十三>:getint()函数(类似于scanf函数)以及库函数strncpy、strncat及strncmp函数的实现
- strlen(),strcpy(),strncpy(),strcat(),strncat(),strcmp(),strncmp()等函数的实现
- 关于字符串函数的一些代码实现(strncpy strncat strncmp strchr strrchr)
- c语言中部分限制字符函数的实现strncmp,strncat,strncpy,strstr,strrstr
- 模拟实现长度受限制的字符串函数(strncpy、strncat、strncmp)
- python的range()函数用法
- Android Menu菜单使用
- BestCoder 1st Anniversary ($) 题解
- 制作HTML5海报
- Xshell 4 SSH隧道跳转访问局域网服务器
- string.find()函数、 strncat函数、strncmp函数的用法
- wpf的附加事件
- PowerDesigner如何导出SQL
- 八大排序算法-shell 排序
- 恐高的鸟
- wpf 如何在后台设置附加属性的值
- PAT (Advanced Level) 1046. Shortest Distance (20) 求环中两点之间的距离
- 901-1注册表相应操作
- 3.2.1 正则表达式的语法(2)