string基础知识点总结

本文详细介绍了C++中的string类型,包括字符串的特点、定义和初始化方式,以及一系列相关函数如str.c_str(),length(),append(),find(),replace(),substr(),compare()等的用法和示例。
摘要由CSDN通过智能技术生成

string知识点

1、string的特点

  • 字符串的管理:它会自动处理字符串内存的开辟和释放,无需手动操作;

  • 动态大小调整:string可以根据需要自动调整其大小。在进行字符串的添加、删除时string会自动调整其内部存储容量,确保空间足够;

  • 安全性:提供了越界访问检查,防止越界等;

  • 迭代器支持:string提供了迭代器(类似于for),可以用迭代器遍历字符串中的字符;

2、string的定义和初始化

#include <bits/stdc++.h>
using namespace std;
​
int main()
{
    //声明并初始化一个空字符
    string str1;
    
    //使用字符串字面量来初始化字符串
    string str2 = "hello, world";
    
    //使用另一个字符串string来初始化字符串
    string str3 = str2;
    
    //使用部分字符串来初始化字符串
    string str4 = str2.substr(0,5) ;    
    //str0.substr(a,b)表示截取str0里面从str0[a]到str0[b]的部分,
    //                  b省略则代表从str0[a]到str0结束
    
    //使用字符数组初始化字符串
    const char* charArray = "hello"; 
    string str5(charArray);
    
    //使用重复的字符初始化字符串
    string str6(5, 'a');
    //str0(a,b)表示有a个b 
    
    cout << "str1是" << str1 << endl ;
    cout << "str2是" << str2 << endl ;
    cout << "str3是" << str3 << endl ;
    cout << "str4是" << str4 << endl ;
    cout << "str5是" << str5 << endl ;
    cout << "str6是" << str6 << endl ;
     
    return 0;
}

当然,之前在输入输出里面说的 getline(cin,s) 也是可以使用的

特别的 cin >> str 也是可以使用的,但是依旧无法读入空格回车和teb;

  • 回忆常量指针: const char* a

#include <iostream>
using namespace std;
int main()
{
	int a=10,b=20;
	const int * p=&a;//常量指针,指向常量的指针 
	//*p=20;//错误,指针指向的值不能改,指针指向可以改
	p=&b;//正确
	int * const p2=&a;//指针常量,指针是个常量不能改变 
	*p2=20;//正确
	//p2=&b;//错误,指针指向不能改,指针指向的值可以改
	const int * const p3=&a;//“常量指针常量”,const既修饰指针又修饰常量 
	//*p3=20;//错误 值和地址都不能改 
	//p3=&b;//错误 值和地址都不能改 
	return 0;
} 

3.string的相关函数

1)string转换char型数组之 str.c_str( )
#include <iostream>
#include <string>
using namespace std;
​
int main()
{
    ios::sync_with_stdio(0),cin.tie(0),cout.tie(0);
    char a[1000] = "hello";
    string str(a);
    printf("%s",str.c_str());   //将string类型的str转换为了char型数组才能使用c风格的printf输出
    return 0;
}

在使用 printf 输出的时候需要将字符串转为字符常量才行;

2)获取字符串长度之 str.length( ) 或 str.size( )

注意string与字符型数组所存储的字符串

#include <iostream>
#include <string>
using namespace std;
​
int main()
{
    ios::sync_with_stdio(0),cin.tie(0),cout.tie(0);
    string str = "hello";
    int len = str.length();
    printf("%d",len);
    return 0;
}

3)字符串拼接之 str.append( ) 或 +
#include <iostream>
#include <string>
using namespace std;
​
int main()
{
    ios::sync_with_stdio(0),cin.tie(0),cout.tie(0);
    string str1 = "hello";
    string str2 = "world!";
    string str3 = str1 + "," + str2;
    string str4 = str1.append(",").append(str2); 
    cout << str3 << "\n" << str4;
    return 0;
}

4)字符串查找之 str.find( )

#include <iostream>
#include <string>
using namespace std;
​
int main()
{
    ios::sync_with_stdio(0),cin.tie(0),cout.tie(0);
    string str = "hello, world!";
    int pos = str.find("world");    //从str里面查找子串"world"在str里面的位置并返回其起始位置;
    if(pos != string::npos)
        cout << pos;
    return 0;
}

5)字符串替换之 str.replace( )

#include <iostream>
#include <string>
using namespace std;
​
int main()
{
    ios::sync_with_stdio(0),cin.tie(0),cout.tie(0);
    string str1 = "hello, world!";
    string str2 = "baby";
    str1.replace(7,5,str2);
    cout << str1;
    return 0;
}
  • 其中,str.replace(a,b,"yes")中a代表的是子串(被替换改变的那个字符串)被替换字符的下标,b代表的是被替换的长度,“yes”代表的是用来进行替换的字符串;

6)字符串拷贝之 str.substr( )

#include <iostream>
#include <string>
using namespace std;

int main()
{
	ios::sync_with_stdio(0),cin.tie(0),cout.tie(0);
	string str1 = "hello, world!";
	string str2 = str1.substr(7,5);
	cout << str2;
	return 0;
}

  • str.substr(pos,n)

    其会返回一个string,包含str中从pos开始的n个字符的拷贝。pos的默认值为0,n的默认值为s.size()-pos,即拷贝从pos开始的所有字符;

    需要注意的是pos不能越界,pos+n可以越界。如果起始位置pos超过了string的大小,则substr函数会抛出一个out_of_range异常。如果开始位置pos加上计数值n大于string的大小,则substr会调整计数值,只拷贝到string的末尾;

7)字符串比较之 str.compare( )

#include <iostream>
#include <string>
using namespace std;

int main()
{
	ios::sync_with_stdio(0),cin.tie(0),cout.tie(0);
	string str1 = "hello, world!";
	string str2 = "hello,a";
	int result = str1.compare(str2);
	if(result == 0)
		cout << "两字符串相等";
	else if(result < 0)
		cout << "str1小于str2";
	else
		cout << "str1大于str2"; 
	return 0;
}

因为在C++中string重载了<,>,=,所以可以直接用str1<str2的办法来比较两字符串的大小,比较规则为按照字典序逐个比较其大小,一旦遇到不等的直接确定大小关系;

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值