嵌入式C++开发详解(五)

常见容器类

一、string类

  ·string类型支持长度可变的字符串,C++标准库负责管理与存储字符相关的内存,以及

     提供各种有用的操作。

  ·typedef basic_string<char> string;

  ·typedef basic_string<wchar_t> wstring;

  ·要使用string类型对象,必须包含相关头文件

     #include <string>

     using std :: string;

1.string类的构造函数

·string s1;  //默认构造函数,s1为空串

·string s2(s1);   //将s2初始化为s1的一个副本

·string s3(value); //将s3初始化为一个字符串字面值副本

·string s4(n,c);   //将s4初始化为字符c的n个副本

代码示例:

#include <iostream>

#include <string>

 

using namespace std;

 

int main()

{

string s1;

cout << s1 << endl;

 

string s2("hello");

string s3(s2);

cout << s3 << endl;

string s4(10, 'a');

cout << s4 << endl;

 

return 0;

}

运行结果:

 

2.string类的字符操作

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

·const char &at(int n)const;

·char &operator[](int n);

·char &at(int n);

  operator[]at()均返回当前字符串中第n个字符的位置,但at函数提供范围检查,当越

   界时会抛出out_of_range异常,下标运算符[]不提供检查访问

· int copy(char *s, int n, int pos = 0) const;//把当前串中以pos开始的n个字符拷贝到以s

   起始位置的字符数组中,返回实际拷贝的数目

代码示例1:

#include <iostream>

#include <string>

 

using namespace std;

 

int main()

{

string s1;

cout << s1 << endl;

 

string s2("hello");

cout << s2[2] << endl;

cout << s2.at(2) << endl;

string s3(s2);

cout << s3 << endl;

string s4(10, 'a');

cout << s4 << endl;

 

return 0;

}

运行结果:

 

代码示例2:

#include <iostream>

#include <string>

 

using namespace std;

 

int main()

{

string s1("hello");

cout << s1 << endl;

 

string s2("hello");

cout << s2[2] << endl;

cout << s2.at(2) << endl;

string s3(s2);

cout << s3 << endl;

string s4(10, 'a');

cout << s4 << endl;

 

char buffer[100] = {0};

s1.copy(buffer,3,1);

cout << buffer  << endl;

 

return 0;

}

运行结果:

 

3.string类的特性描述

·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填充不足的部分

4.string类的输入输出操作

·string类重载运算符operator>>用于输入,同样重载运算符operator<<用于输出操作。

·函数getline(istream &in,string &s);用于从输入流in中读取字符串到s中,以换行符'\n'分开

代码比较:

版本1:

#include <iostream>

#include <string>

using namespace std;

int main()

{

string s1;

cin >> s1;

cout << s1 << endl;

return 0;

}

版本2:

#include <iostream>

#include <string>

using namespace std;

int main()

{

string s2;

getline(cin, s2);

cout << s2 << endl;

return 0;

}

运行结果对比:

 

 

5.string类的赋值

·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);//firstlast迭代器之间的部分赋给

  字符串

代码示例:

#include <iostream>

#include <string>

 

using namespace std;

 

int main()

{

int i;

string s1;

s1.assign("hello");

cout << s1 << endl;

 

for (i = 0; i < s1.length(); i++)

{

cout << s1[i] << endl;

}

return 0;

}

运行结果:

 

6.string类的迭代器处理

string类提供了向前和向后遍历的迭代器iterator,迭代器提供了访问各个字符的语法,类 似于指针操作,迭代器不检查范围。

用string::iterator或string::const_iterator声明迭代器变量,const_iterator不允许改变迭代的内容。常用迭代器函数有:

·const_iterator begin()const;

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

·const_iterator end()const;

·iterator end();                    //返回string

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值