C++/C关于字符串的使用与相互转化

一,C字符串


C语言中, 没有字符串的数据类型,可使用一个以NULL('\0')字符结尾的字符数组来保存字符串。

只有以null字符结尾的字符数组才是C字符串,否则只是一般的C字符数组。


相关函数等在头文件<string.h>

注:
<string.h>是C版本的头文件,包含比如strcpy()之类的字符串处理函数。


<cstring>在C++标准化(1998年)为了兼容以前将所有这些文件都进行了新的定义,加入到了标准库中,加入后的文件名就新增了一个"c"前缀并且去掉了.h的后缀名,所以string.h头文件成了cstring头文件。


<string>是C++标准定义的头文件,定义了一个string的字符串类并包含了string类的各种操作。同时又包含了老的C版本的字符串操作。


1.  使用字符数组存C字符串定义时可以利用"="号进行初始化,但之后不能用"="对C字符串再赋值。再赋值可用strcpy()。

C中也可以使用字符指针来访问一个字符串,通过字符指针指向存放字符串数组的首元素地址来进行访问。

注意体会下例子注释:


char a[11] = "kobe";//字符串的初始化
	//a="kobebryant"不能如此赋值!!
	strcpy(a,"kobecryant");//字符串的赋值
	a[4]='b';

	//获取字符串的长度,不包括'\0'在内
	int n=strlen(a);
	printf("%s\n",a);
	
	char *b = "kobe mvp!";
	b="kobe 24mvp!!";//指针可以这样赋值!!
	//b[7]='g';错误!
    printf("%s\n",b);


2. 一些常用函数用法:

#include<stdio.h>
#include<string.h>
int main(){
	char s1[]="kobe is FMVP";
	char s2[20]="num.24";
	char s3[]="num.08";
	char s4[20],*p;
	printf("s1现在是:%s\n",s1);
	printf("s2现在是:%s\n",s2);
	printf("s3现在是:%s\n",s3);
	printf("s4现在是:%s\n\n",s4);
//	int strlen(char *str)
	printf("s1长度:%d\ts2长度:%d\n\n",strlen(s1),strlen(s2));
	
//串拷贝 char *strcpy(char *str1,char *str2)
	strcpy(s4,s2);  
	printf("s2赋值s4后:%s\n\n",s4);
	
//串连接 char *strcat(char *str1,char*str2)
	printf("s1接在s2后:%s\n\n",strcat(s2,s1));
//注意:如果使用字符数组存放字符串,strcat函数并不检查第一个数组是否能够容纳第二个字符串,
//这样多出来的字符串就会溢出到相邻的存储单元而出现问题。
   
//串比较 int strcmp(char *str1,char *str)  //比较的是对应字符的ASCII码值,如果str1>str2,返回1
	printf("%d\t%d\t%d\n\n",strcmp(s2,s3),strcmp(s2,s2),strcmp(s3,s2));
	
//串定位char *strchr(char *str,char ch)
	p=strchr(s1,'M'); //找到返回字符在字串中的位置,否则返回-1
	strcpy(p,s2);
	printf("找到s1的第一次出现'M'的位置,把s2接上后:%s\n",s1);

	return 0;
}

上述运行结果:



二,C++字符串


string并不是C++语言本身具有的基本类型,它是在C++标准库中声明的一个字符串类,用这种类可以定义对象。每一个字符串变量都是string类的一个对象。使用string类型,我们必须包含头文件 <string>


1,声明与初始化

 string Str;
上面的声明没有传入参数,所以就直接使用了string的默认的构造函数,这个函数所作的就是把Str初始化为一个空字符串。

String类的构造函数和析构函数如下:
string s1
string s2(s1) 
string s2=s1
string s3("value") 
string s3="value"
string s(num,'c') //生成一个字符串,包含num个c字符


2. 输入
(1)方法一:

cin>>s;

cout<<s<<endl;

会忽略空白,从不是空白读取到空白结束,空白指空格符换行符制表符等


(2)方法二:

使用getline函数可以保留空白,每次读入一行遇到换行符为止。
例如:

#include<iostream>
#include<string>
int main(){
	std::string line;
	while(getline(std::cin,line))
		std::cout<<"input: "<<line<<std::endl;
	return 0;
}


3. 对象操作函数(网上整理而来未全检试)
 

随机读:

const char &operator[](int n)const;
const char &at(int n)const;
char &operator[](int n);
char &at(int n);

#include<iostream>
#include<string>
using namespace std;
int main(){
	string s="kobe bryant",s2;
	cout<<s.operator[](2)<<endl;
	cout<<s.at(2)<<endl;
	cout<<s[2]<<endl;
//	cout<<s.at(20)<<endl;
	cout<<s.operator[](20)<<endl;
	return 0;
}

operator[]和at()均返回当前字符串中第n个位置的字符,但at函数提供范围检查,当越界时会抛出out_of_range异常,下标运算符[]不提供检查访问。



属性:

int capacity()const;    //返回当前容量(即string中不必增加内存即可存放的元素个数)
int max_size()const;    //返回string对象中可存放的最大字符串的长度
int size()const;        //返回当前字符串的大小
int length()const;       //返回当前字符串的长度
bool empty()const;        //当前字符串是否为空
void resize(int len,char c);//把字符串当前大小置为len,并用字符c填充不足的部分


赋值:
string &operator=(const string &s);//把字符串s赋给当前字符串
string &assign(const char *s);//用c类型字符串s赋值
string &assign(const char *s,int n);//用c字符串s开始的n个字符赋值
string &assign(const string &s);//把字符串s赋给当前字符串
string &assign(int n,char c);//用n个字符c赋值给当前字符串
string &assign(const string &s,int start,int n);//把字符串s中从start开始的n个字符赋给当前字符串
string &assign(const_iterator first,const_itertor last);//把first和last迭代器之间的部分赋给字符串


串接:
string &operator+=(const string &s);//把字符串s连接到当前字符串的结尾 
string &append(const char *s);            //把c类型字符串s连接到当前字符串结尾
string &append(const char *s,int n);//把c类型字符串s的前n个字符连接到当前字符串结尾
string &append(const string &s);    //同operator+=()
string &append(const string &s,int pos,int n);//把字符串s中从pos开始的n个字符连接到当前字符串的结尾
string &append(int n,char c);        //在当前字符串结尾添加n个字符c
string &append(const_iterator first,const_iterator last);//把迭代器first和last之间的部分连接到当前字符串的结尾


比较:
bool operator==(const string &s1,const string &s2)const;//比较两个字符串是否相等
运算符">","<",">=","<=","!="均被重载用于字符串的比较;
int compare(const string &s) const;//比较当前字符串和s的大小
int compare(int pos, int n,const string &s)const;//比较当前字符串从pos开始的n个字符组成的字符串与s的大小
int compare(int pos, int n,const string &s,int pos2,int n2)const;//比较当前字符串从pos开始的n个字符组成的字符串与s中pos2开始的n2个字符组成的字符串的大小
int compare(const char *s) const;
int compare(int pos, int n,const char *s) const;
int compare(int pos, int n,const char *s, int pos2) const;
compare函数在>时返回1,<时返回-1,==时返回0   


子串:
string substr(int pos = 0,int n = npos) const;//返回pos开始的n个字符组成的字符串

交换:
void swap(string &s2);    //交换当前字符串与s2的值


查找: 
int find(char c, int pos = 0) const;//从pos开始查找字符c在当前字符串的位置
int find(const char *s, int pos = 0) const;//从pos开始查找字符串s在当前串中的位置
int find(const char *s, int pos, int n) const;//从pos开始查找字符串s中前n个字符在当前串中的位置
int find(const string &s, int pos = 0) const;//从pos开始查找字符串s在当前串中的位置
//查找成功时返回所在位置,失败返回string::npos的值 
int rfind(char c, int pos = npos) const;//从pos开始从后向前查找字符c在当前串中的位置
int rfind(const char *s, int pos = npos) const;
int rfind(const char *s, int pos, int n = npos) const;
int rfind(const string &s,int pos = npos) const;
//从pos开始从后向前查找字符串s中前n个字符组成的字符串在当前串中的位置,成功返回所在位置,失败时返回string::npos的值 
int find_first_of(char c, int pos = 0) const;//从pos开始查找字符c第一次出现的位置
int find_first_of(const char *s, int pos = 0) const;
int find_first_of(const char *s, int pos, int n) const;
int find_first_of(const string &s,int pos = 0) const;
//从pos开始查找当前串中第一个在s的前n个字符组成的数组里的字符的位置。查找失败返回string::npos 
int find_first_not_of(char c, int pos = 0) const;
int find_first_not_of(const char *s, int pos = 0) const;
int find_first_not_of(const char *s, int pos,int n) const;
int find_first_not_of(const string &s,int pos = 0) const;
//从当前串中查找第一个不在串s中的字符出现的位置,失败返回string::npos 
int find_last_of(char c, int pos = npos) const;
int find_last_of(const char *s, int pos = npos) const;
int find_last_of(const char *s, int pos, int n = npos) const;
int find_last_of(const string &s,int pos = npos) const; 
int find_last_not_of(char c, int pos = npos) const;
int find_last_not_of(const char *s, int pos = npos) const;
int find_last_not_of(const char *s, int pos, int n) const;
int find_last_not_of(const string &s,int pos = npos) const;
//find_last_of和find_last_not_of与find_first_of和find_first_not_of相似,只不过是从后向前查找


替换: 
string &replace(int p0, int n0,const char *s);//删除从p0开始的n0个字符,然后在p0处插入串s
string &replace(int p0, int n0,const char *s, int n);//删除p0开始的n0个字符,然后在p0处插入字符串s的前n个字符
string &replace(int p0, int n0,const string &s);//删除从p0开始的n0个字符,然后在p0处插入串s
string &replace(int p0, int n0,const string &s, int pos, int n);//删除p0开始的n0个字符,然后在p0处插入串s中从pos开始的n个字符
string &replace(int p0, int n0,int n, char c);//删除p0开始的n0个字符,然后在p0处插入n个字符c
string &replace(iterator first0, iterator last0,const char *s);//把[first0,last0)之间的部分替换为字符串s
string &replace(iterator first0, iterator last0,const char *s, int n);//把[first0,last0)之间的部分替换为s的前n个字符
string &replace(iterator first0, iterator last0,const string &s);//把[first0,last0)之间的部分替换为串s
string &replace(iterator first0, iterator last0,int n, char c);//把[first0,last0)之间的部分替换为n个字符c
string &replace(iterator first0, iterator last0,const_iterator first, const_iterator last);//把[first0,last0]之间的部分替换成[first,last)之间的字符串


插入: 
string &insert(int p0, const char *s);
string &insert(int p0, const char *s, int n);
string &insert(int p0,const string &s);
string &insert(int p0,const string &s, int pos, int n);
//前4个函数在p0位置插入字符串s中pos开始的前n个字符

string &insert(int p0, int n, char c);//此函数在p0处插入n个字符c
iterator insert(iterator it, char c);//在it处插入字符c,返回插入后迭代器的位置
void insert(iterator it, const_iterator first, const_iterator last);//在it处插入[first,last]之间的字符
void insert(iterator it, int n, char c);//在it处插入n个字符c


删除:
iterator erase(iterator first, iterator last);//删除[first,last]之间的所有字符,返回删除后迭代器的位置
iterator erase(iterator it);//删除it指向的字符,返回删除后迭代器的位置
string &erase(int pos = 0, int n = npos);//删除pos开始的n个字符,返回修改后的字符串


三,C和C++之间的转化


1.关键:

const char *data()const;//返回一个非null终止的c字符数组
const char *c_str()const;//返回一个以null终止的c字符串

int copy(char *s, int n, int pos = 0) const;//把当前串中以pos开始的n个字符拷贝到以s为起始位置的字符数组中,返回实际拷贝的数目


2.string-> const char *

两种方法:

string s1="kobebryant";
	const char *a=s1.c_str();
	const char *b=s1.c_str();
	cout<<a<<endl;
	cout<<b<<endl;

3. string-> char *

#include<iostream>
#include<malloc.h>
#include<string>
using namespace std;
int main(){
	string s1="kobebryant 123";
	char *c;
	int len=s1.length();
	c=(char*)malloc((len)*sizeof(char));
	s1.copy(c,len,0);
	cout<<c<<endl;
	
}


4. char *->string

string s;
	char *p="kobe mvp!";
	s=p;
	cout<<s<<endl;


5. char[ ]->string

string s;
	char p[10]="kobe mvp!";
	s=p;
	cout<<s<<endl;

6. string->char[ ]

string s = "kobe bryant";
    char a[12];
    for( int i=0;i<s.length();i++)
        a[i] = s[i];
    a[i] = '\0';
    printf("%s\n",a);









  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值