嵌入式操作系统面向对象(3)

本文详细介绍了C++中的结构体,包括结构体在C与C++中的区别,权限修饰符以及使用示例。同时,文章深入探讨了string类,包括其字符操作、属性、赋值、连接、比较和查找功能,以及迭代器处理和删除函数,提供了丰富的代码示例。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

D313
宏函数
调用的时候会把函数的语句嵌入到调用的地方提高运行效率
一旦有了默认参数后面必须为默认参数

1、 结构体作用:封装不同类型的数据,即将不同类型的数据整合在一起,用同一种数据类型表示。

2、 C语言 VS C++

C语言:(1)定义结构变量必须加上struct关键字。

(2)结构体只能存放变量,不能存放函数。

C++: (1)定义结构变量无须加上struct关键字。

(2)结构体能存放变量,也能存放函数。

(3)可以在结构体里加入权限修饰符。

(4)可继承:拥有另一结构体的成员但不会对另一结构中的成员造成影响。

(5)可多态。

   C++对C的升级:使得struct(结构体) ==  class(类),但有一些叫法不同:

(1)结构体变量 ==
类的对象,即变量
== 对象。

  (2)函数称之为方法,结构里存放的变量称之为属性或者成员变量。

     (3)两者区别:默认权限不同(class默认private,struct默认public)。

3、 权限修饰符

(1)public公有权限:能被外界变量直接访问的成员。

(2)private私有权限:不能被外界变量直接访问,但是可是在struct内部函数中访问。

(3)protected被保护的: == private。

4、使用示例

#include

#include<string.h>

using namespace std;

struct student

{

public://公有

int num;

char name[10];

void print( )

{

    print1( );

    cout << "hello\n";

}

//由于age无法直接从外部函数读给结构体内部

//定义get/set函数从外部函数赋予内部变量值

void Age_Set( int a)

{

    this->age = a; //this->age指结构体内的age

}

private://私有

int age;

void print1( )

{

    cout << age << endl;

}

};

int main( )

{

student STU;

STU.num = 1;

strcpy(STU.name,“zhangsan”);

STU.Age_Set(20);

STU.print( );

return 0;

}

string

string类的字符操作

1、string定义:可变长的字符串

2、string字符串初始化,示例:

(1)
string
s = “hello world”;

(2)
sting
s1(“hello world2”);

(3)
string
*ps = new string(“hello world3”);

(4)
string
s4 = “”; //空字符串

3、string字符访问方式:使用[]或at函数

(1)
cout
<< s1[3] << endl; //输出字符l

(2)
cout
<< s1.at(4) << endl; //输出字符o

二者区别:越界情况[]不会报出异常,at函数会产生异常。

二、string类的特性(属性)

1、特性(*重点)

(1)
int
capacity()const; //返回当前容量(即string中不必增加内存即可存放的元素数) *

(2)
int
max_size()const; //返回string对象中可存放的最大字符串的长度 *

(3)
int
size()const; //返回当前字符串的大小 *

(4)
int
length()const; //返回当前字符串的长度

(5)
bool
empty()const; //当前字符串是否为空 *

(6)
void
resize(int len,char c); //把字符串当前大小置为len,并用字符c填充不足的部分

2、长度length、大小size(占有内存空间)此处相等,均不包含”\0”,常用size。

注:在C语言中strlen不包含”\0”而sizeof包含”\0”。

3、capacity规则:返回值总比它的size要大。

4、使用示例

(1)
cout
<< s1.size() << endl; //前四种

(2)
if(s4.empty()
== true) {cout << “s4 is null\n”;} //第五种

三、string类的输入/输出

1、cin >>用于输入,cout<<用于输出操作。

2、函数getline(istream &in,string &s);

用于从输入流in中读取字符串到s中,以换行符’\n’分开。

四、string类的赋值

1、赋值函数(会查会用即可)

(1)
string
&operator=(const string &s);//把字符串s赋给当前字符串

(2)
string
&assign(const char *s);//用c类型字符串s赋值

(3)
string
&assign(const char *s,int n);//用c字符串s开始的n个字符赋值

(4)
string
&assign(const string &s);//把字符串s赋给当前字符串

(5)
string
&assign(int n,char c);//用n个字符c赋值给当前字符串

(6)
string
&assign(const string &s,int start,int n);

//把字符串s中从start开始的n个字符赋给当前字符串

(7)
string
&assign(const_iterator first,const_itertor last);

//把first和last迭代器之间的部分赋给字符串

2、赋值具体使用示例:

(1)
sting
s5 =s1;

(2)
string
s5; s5 = s1;

(3)
sting
s5; s5.assign(s1); //此处使用的是第一种方法

(4)
sting
s5; s5.assign(s1,2,6); //给s5赋值s1在第2个字符开始的6个字符

五、string类的连接

1、连接函数

(1)
string
&operator+=(const string &s); //把字符串s连接到当前字符串的结尾

(2)
string
&append(const char *s); //把c类型字符串s连接到当前字符串结尾

(3)
string
&append(const char *s,int n);

//把c类型字符串s的前n个字符连接到当前字符串结尾

(4)
string
&append(const string &s); //同operator+=()

(5)
string
&append(const string &s,int pos,int n);

//把字符串s中从pos开始的n个字符连接到当前字符串的结尾

(6)
string
&append(int n,char c); //在当前字符串结尾添加n个字符c

(7)
string
&append(const_iterator first,const_iterator last);

//把迭代器first和last之间的部分连接到当前字符串的结尾

2、连接具体使用示例:(将s1\abc\s2连接在一起赋给s5)

(1)
string s5 = s1 + “abc” + s2;

(2)
string s5; s5.append(s1); s5.append(“abc”);
s5.append(s2);

六、string类的比较

1、运算符">","<",">=","<=","!="均被重载用于字符串的比较。

使用示例:

     if(s

== s1) { cout << “s == s1” << endl; }

2、string自带重载函数:(compare函数在>时返回1,<时返回-1,==时返回0)

(1)
int compare(const string &s) const;

//比较当前字符串和s的大小

(2)
int compare(int pos, int n,const string &s)const;

//比较当前字符串从pos开始的n个字符组成的字符串与s的大小

(3)
int compare(int pos, int n,const string &s,int
pos2,int n2)const;

//比较当前字符串从pos开始的n个字符组成的字符串与s中

//pos2开始的n2个字符组成的字符串的大小

(4)
int compare(const char *s) const;

//以下为使用指针修饰的字符串

(5)
int compare(int pos, int n,const char *s) const;

(6)
int compare(int pos, int n,const char *s, int pos2)
const;

七、string类的查找函数

1、find 函数

(1)
int find(char c,
int pos = 0) const;

//从pos开始查找字符c在当前字符串的位置

(2)
int find(const
char *s, int pos = 0) const;

//从pos开始查找字符串s在当前串中的位置

(3)
int find(const
char *s, int pos, int n) const;

//从pos开始查找字符串s中前n个字符在当前串中的位置

(4)
int find(const
string &s, int pos = 0) const;

//从pos开始查找字符串s在当前串中的位置

2、find_first_not_of函数

从当前串中查找第一个不在串s中的字符出现的位置,失败返回string::npos

(1)
int find_first_not_of(char c, int pos = 0) const;

(2)
int find_first_not_of(const char *s, int pos = 0)
const;

(3)
int find_first_not_of(const char *s, int pos,int n)
const;

(4)
int find_first_not_of(const string &s,int pos =
0) const;

3、find_last_(/not_)of函数

(1)
int find_last_of(char c, int pos = npos) const;

(2)
int find_last_of(const char *s, int pos = npos) const;

(3)
int find_last_of(const char *s, int pos, int n =
npos) const;

(4)
int find_last_of(const string &s,int pos = npos)
const;

(5)
int find_last_not_of(char c, int pos = npos) const;

(6)
int find_last_not_of(const char *s, int pos = npos)
const;

(7)
int find_last_not_of(const char *s, int pos, int n)
const;

(8)
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相似

//只不过是从后向前查找

4、 使用示例

 int pos = s1.find(“hello”); //设 s1 为“hi hello

world2”

 cout << pos << endl; //输出匹配到的第一个hello字串位置:3

八、string类的迭代器处理

1、迭代器作用:遍历(一个一个访问)的容器(string\vector),也可理解为指针的泛化。

2、迭代器声明:

(1)用string::iterator或string::const_iterator声明迭代器变量。

(const_iterator不允许改变迭代的内容)

(2)如需从后向前迭代遍历(如rbegin、rend),则通过设置迭代器string::reverse_interator或 string::const_reverse_interator来实现。

3、迭代器函数(以下均以无const修饰类型)

(1)
iterator begin(); //返回string的起始位置

(2)
iterator end();
//返回string的最后一个字符后面的位置

(3)
iterator rbegin();
//返回string的最后一个字符的位置

(4)
iterator rend(); //返回string第一个字符位置的前面

3、使用示例

string :: iterator it; // 定义一个迭代器it

//begin()指向第一个字符,end()指向最后一个有效字符(数据)的下一个

for(it = s5.begin(); it != s5.end(); it++) //设s5 = “hello world”

{

 *it = ‘1’; 

//it代表指针指向的位置,此处使用可使s5全部变为’1’

  cout << *it << endl; //每次换行输出一个字符,此处输出11行单个字符’1’

}

cout << s5 << endl; //此处输出11111111111

九、string类的删除函数

1、删除函数

(1)
iterator
erase(iterator first, iterator last);

//删除[first,last)之间的所有字符,返回删除后迭代器的位置

(2)
iterator
erase(iterator it);

//删除it指向的字符,返回删除后迭代器的位置

(3)
string
&erase(int pos = 0, int n = npos);

//删除pos开始的n个字符,返回修改后的字符串

2、使用示例

(1)
s5.erase(s5.begin()

  • 2,s5.begin()+6);

//设 s5 = “01234567890”,此处删除[2,6)即’2345’

cout << s5 << endl; //此处输出’0167890’

(2)
for(it
= s5.begin(); it != s5.end()😉 //设s5 = “hello worldl”

{

if(*it == ‘l’)

it = s5.erase(it);

//删除s5中所有的l,返回删除后迭代器的位置

else it
++;

}

cout << s5 << endl; //输出’heo word’

C++函数作用查询网站:

https://en.cppreference.com/

调用的时候会把函数的语句嵌入到调用的地方提高运行效率

一旦有了默认参数后面必须为默认参数

string

C++函数作用查询网站:

https://en.cppreference.com/

string的字符访问

string

string的特性

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值