首页 > 其他分享 >string类对象成员函数及使用【笔记】

string类对象成员函数及使用【笔记】

时间:2024-03-17 23:29:05浏览次数:21  
标签:const string 函数 pos 笔记 char s1 size

文章目录


一、常见构造

// 1. 空参构造, 空字符串
string(){}

// 2. 用C-string 构造string类对象
string(const char* str){}

// 3.拷贝构造函数
string(const string& str){}

void TestString()
{
    string s1;	// 无参构造
    string s2("hello, world"); 	// 用c-格式字符串构造string类对象s2
    string s3(s2);	// 拷贝构造s3
}

二、string类对象的容量操作

方法说明
size()返回字符串长度(不包含‘\0’)
length()和size功能一致,没区别
capacity()返回空间总大小(跟扩容机制有关)
empty()检查字符串是否为空串,是返回true, 否则返回false
clear()清空有效字符(不改变底层空间的大小即capacity不变)
reserve()为字符串预留空间(扩容)
resize()将有效字符的个数改为n个, 多出的空间用字符c填充

三、string对象的访问及遍历操作

方式内容说明
使用 [] 访问operator[] 运算符重载返回pos位置的字符,const string 类对象调用
使用迭代器begin()+end()begin()获取一个字符的迭代器,end()获取最后一个字符下一个位置的迭代器
反向迭代器rbegin()+rend()rbegin()获取一个字符的迭代器,rend()获取最后一个字符下一个位置的迭代器
s范围forfor(auto e: str)底层原理是迭代器
// operator[] 运算符重载——底层
class String
{
public:
    // [] 运算符重载
	char& operator[](size_t pos)
	{
		return _str[pos];
	}

private:
	char* _str;
	size_t _size;
	size_t _capacity;
};
void test()
{
	string s("hello, world");
	
	// 1. 使用[]访问
	for (int i=0; i< s.size(); ++i)
	{
		cout << s[i] << " ";
	}
	cout << endl;

	// 2. 使用迭代器
	string::iterator it = s.begin();
	while (it != s.end())
	{
		cout << *it << " ";
		++it;
	}
	cout << endl;

	// 3. 使用反向迭代器
	string::reverse_iterator it2 = s.rbegin();
	while (it2 != s.rend())
	{
		cout << *it2 << " ";
		++it2;
	}
	cout << endl;

	// 4. 使用范围for
	for (auto e : s)
	{
		cout << e << " ";
	}
}

四、string 类对象的修改操作(仅列举最常用的)

函数名称功能说明
operator +=(运算符重载)在字符串后追加字符串str
c_str()返回C格式的字符串
find() 和 npos在字符串中,从前往后找字符c, 找到返回该字符的索引位置, 找不到返回npos
rfind()和find相反, 从后往前找
substr()在字符串中从pos位置开始, 截取n个字符,然后将其返回
  1. operator +=(运算符重载)

    • string 和 string
    • string 和 c 字符串
    • string 和 字符
    void test_operator()
    {
    	// string (1)
    	// string& operator+= (const string & str);
    	string s1("hello, world");
    	string s2("nihao shijie");
    	s1 += s2;
    	cout << s1 << endl;
    
    	//c-string (2)
    	// string& operator+= (const char* s);
    	string s3("hello, world");
    	const char* s4 = "nihao, shijie";
    	s3 += s4;
    	cout << s3 << endl;
    
    	// character (3)
    	// string& operator+= (char c);
    	string s5("hello, world");
    	char ch = 'A';
    	s5 += ch;
    	cout << s5 << endl;
    }
    
  2. c_str()

    • 可以将string类对象转换成c字符串。比如:使用strcpy()函数,拷贝字符串。
    void test_c_str()
    {
    	// const char* c_str() const;
    	// char * strcpy ( char * destination, const char * source );
    	// 两个参数都必须是char* 的字符串
    	string s1("hello, world");
    	char* buff = new char[s1.size() + 1];	// 开辟一个和s1 一样大小的空间,+1 是为了存储‘\0’
    	// strcpy(buff, s1);	// 错误, 因为s1的类型是string类型,需要的参数是const char* 类型
    	strcpy(buff, s1.c_str());	// 将string类型的对象转换成c字符串类型
    	cout << buff << endl;
    }
    
  3. find() 和 npos

    • npos 是一个静态的无符号整数。作为find()的返回值表示没有查找到。作为参数表示字符串的结尾位置的索引。
    void test_find()
    {
    	/*  character(1) -- 字符串中查找字符(常用)
    	    size_t find(char c, size_t pos = 0) const;
    		参数: c 表示要查找的字符, pos表示从哪个位置开始查找, 不写默认从头开始找。
    		返回值:查找到返回该字符在字符换中的索引位置,找不到返回 npos
    	*/
    	string s1("hello, world");
    	size_t index = s1.find('o');
    	if (index != string::npos)
    	{
    		s1[index] = 'A';
    	}
    	cout << s1 << endl;;
        
    	/*	string (2)	-- 字符串中查找string字符串
    		size_t find (const string& str, size_t pos = 0) const;
    	*/
    	string s2("hello, world");
    	string s22("wo");
    	size_t index2 = s2.find(s22);
    	if (index2 != string::npos)
    	{
    		s2[index2] = 'B';
    	}
    	cout << s2 << endl;
    
    	/*	c-string (3) -- 字符串中查找c字符串(常用)
    	size_t find (const char* s, size_t pos = 0) const;
    	*/
    	string s3("hello, world");
    	size_t index3 = s3.find("wo");
    	if (index3 != string::npos)
    	{
    		s3[index3] = 'C';
    	}
    	cout << s3 << endl;
        
        /*	buffer (4)	--  在string字符串中查找指定长度的c字符串
    	size_t find (const char* s, size_t pos, size_t n) const;
    	s是c字符串, pos 查找的起始位置, n 要查找的c字符串中,从起始位置开始的一段长度
    	*/
        string s4("hello, world");
        size_t index4 = s4.find("orl", 0, 3);
        if (index4 != string::npos)
        {
            cout << index4 << endl;
            s4[index4] = 'D';
        }
        else
        {
            cout << "没有找到‘orl’" << endl;
        }
        cout << s4 << endl;
    }
    
  4. rfind()

    • 和find()类似, 从后往前查找
    void test_rfind()
    {
    	/*
    	string (1)	
    	size_t rfind (const string& str, size_t pos = npos) const;
    	c-string (2)	
    	size_t rfind (const char* s, size_t pos = npos) const;
    	buffer (3)	
    	size_t rfind (const char* s, size_t pos, size_t n) const;
    	character (4)	
    	size_t rfind (char c, size_t pos = npos) const;
    	*/
    
    	/*
    	character (4) -- 从后往前查找字符
    	size_t rfind (char c, size_t pos = npos) const;
    	pos 表示从pos位置开始往前找。
    	*/
    	string s1("hello, world");
    	size_t index1 = s1.rfind('o');
    	if (index1 != string::npos)
    	{
    		s1[index1] = 'A';
    	}
    
    	string s2("hello, world");
    	size_t index2 = s2.rfind('o', 7);
    	if (index2 != string::npos)
    	{
    		s2[index2] = 'B';
    	}
    
    	cout << s1 << endl;;
    	cout << s2 << endl;;
    }
    
  5. substr()

    • 截取子串
    void test_substr()
    {
    	//string substr (size_t pos = 0, size_t len = npos) const;
    	// 截取从pos位置开始到len位置结束的字串
    	// string::npos 的值表示字符串结束前的所有字符。
    	string s("hello, world");
    	string s2 = s.substr(2, 9);
    	cout << s2 << endl;
    }
    

五、getline()

  • 配合cin使用可以一次性读取输入的一行的内容
void test_getline()
{
    //istream& getline (istream& is, string& str);
	// is 表示流提取, str 表示要存放的字符串对象
	// 输出: 字符串中最后一个单词的长度
	string line;
	while (getline(cin, line))
	{
		size_t pos = line.rfind(' ');
		cout << line.size() - (pos + 1) << endl;
	}
}

标签:const,string,函数,pos,笔记,char,s1,size
From: https://blog.csdn.net/m0_66434421/article/details/136793235

相关文章

  • 小新喊你了解字符串函数了(二)
    接上一篇,我们再学习四个新的字符串函数1.strstr函数的使用和模拟实现 char*strstr(constchar*str1,constchar*str2);strstr函数用于字符串的查到,在一个字符串中查找另一个字符串,找到了就返回字符串str2在字符串str1中第⼀次出现的位置 ,找不到就会返回NULL(空......
  • Word2vec 学习笔记
    word2vec学习笔记0.引言1.Word2vec简介1-1.CBOW1-2.SG2.实战0.引言最近研究向量检索,看到有同事使用MeCab、Doc2Vec,所以把Word2vec这块知识学习一下。1.Word2vec简介Word2vec即wordtovector,顾名思义,就是把词转换成向量,该方法在2013年由谷歌公司......
  • Java String类的compareTo() 方法
    compareTo()方法用于两种方式的比较:字符串与对象进行比较。按字典顺序比较两个字符串。intcompareTo(Objecto)或intcompareTo(StringanotherString)参数o--要比较的对象。anotherString--要比较的字符串。返回值返回值是整型,它是先比较对应字符的大小(ASC......
  • A. String Transformation 1
    原题链接题解\(a\tob,b\toc,a\toc\)等价于\(a\tob\toc\)\(a\toz,b\toz\)也可以等价于\(a\tob\toz\)花费不变所以是并查集,然后累积建边数量如果\(finds(a)==finda(z)\)代表\(a\)可以通过其他的变化也顺便变成\(z\)code#include<bits/stdc++.h>usingna......
  • 一.函数的递归
    简单而通俗易懂的说,函数的递归就是:函数自己调用自己。就是把大事化小事,递的意思就是推进的意思。归就是回归的意思。递归的限制:两个条件:1.递归存在限制条件,当满⾜这个限制条件的时候,递归便不再继续。                        ......
  • APT32 RTC+低功耗调试笔记
    1、项目需求   采用APT32F1023单片机,内部27K时钟驱动RTC,内部6M定时器作为主频。周期检测外部供电是否恢复,如果恢复则使用正常工作模式,否则仅开启RTC,关闭其他外设,进入低功耗待机模式。2、存在问题    A:开启看门狗后,会周期触发看门狗复位     B:进入低功耗模式后,......
  • 综合架构学习笔记-3---
    综合架构学习笔记-3---rsync-扩展脚本虚拟机测试环境ip10.0.1.0网关10.0.1.2子网掩码255.255.255.0知识回顾rsync如何部署?1.安装软件rsync2.编写配置文件虚拟用户备份目录密码文件具体流程演示linux系统安装部署服务流程:a下载安装软件yumb编写配置......
  • 综合架构学习笔记-2
    综合架构学习笔记-2---rsync-实战实战1rsync守护进程部署测试环境--虚拟机环境ip10.0.1.0网关10.0.1.2子网掩码255.255.255.0步骤服务端第一步:下载安装软件 [root@localhost~]#yuminstall-yrsync 第二步:编写配置文件把原来的配置文件删除 [roo......
  • 综合架构学习笔记-3---rsync-扩展脚本
    综合架构学习笔记-3---rsync-扩展脚本虚拟机测试环境ip10.0.1.0网关10.0.1.2子网掩码255.255.255.0知识回顾rsync如何部署?1.安装软件rsync2.编写配置文件虚拟用户备份目录密码文件具体流程演示linux系统安装部署服务流程:a下载安装软件yumb编写配置......
  • 综合架构学习笔记-2---rsync-实战
    综合架构学习笔记-2---rsync-实战实战1rsync守护进程部署测试环境--虚拟机环境ip10.0.1.0网关10.0.1.2子网掩码255.255.255.0步骤服务端第一步:下载安装软件 [root@localhost~]#yuminstall-yrsync 第二步:编写配置文件把原来的配置文件删除 [roo......