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函数返回值判定

代码示例:

  1. int main()  
  2. {  
  3.     string s = "Alice Bob Charlie";  
  4.     size_t position;  
  5.     position = s.find("none");  
  6.     if (position >= 0)  
  7.         cout << "Found! position is : " << position << endl;  
  8.     else  
  9.         cout << "Not found!" << endl;  
  10. }  

现象&后果

程序运行结果输出"Found! position is : 4294967295",但实际上所找的字符串"none"并不存在于字符串s中。

Bug分析

程序的目的是,在源字符串s中查找目的字符串,若找到,则显示"Found",并返回目标子串在源字符串中的位置;反之,若未找到,则返回"Not found"。string.find在未找到时会返回string::npos。

在C++中常量npos是这样定义的:

  1. 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作比较。

正确代码

  1. int main()  
  2. {  
  3.     string s = "Alice Bob Charlie";  
  4.     size_t position;  
  5.     position = s.find("none");  
  6.     if (position != string::npos)  
  7.         cout << "Found! position is : " << position << endl;  
  8.     else  
  9.         cout << "Not found!" << endl;  
  10. }  



应用举例: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 <string.h>
#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<string.h>
#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<string.h>
#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


0 0