当前位置: 首页 > news >正文

网站建设询价报告深圳网站建设深圳企业网站建设

网站建设询价报告,深圳网站建设深圳企业网站建设,网站开发排行,wordpress后台字体修改19.1 string概述 1、string是STL的字符串类型,通常用来表示字符串。而在使用string之前,字符串通常是 用char * 表示的。string 与char * 都可以用来表示字符串,那么二者有什么区别呢。 2、string和 char * 的比较 (1&#xff09…

19.1 string概述

  • 1、string是STL的字符串类型,通常用来表示字符串。而在使用string之前,字符串通常是 用char * 表示的。string 与char * 都可以用来表示字符串,那么二者有什么区别呢。

  • 2、string和 char * 的比较
    (1)string是一个类, char*是一个指向字符的指针。
    (2)string封装了char * ,管理这个字符串,是一个char * 型的容器。
    (3)string不用考虑内存释放和越界。
    (4)string管理char * 所分配的内存。每一次string的复制,取值都由string类负责维护,不用担心复制越界和取值越界等。

  • 3、string提供了一系列的字符串操作函数(这个等下会详讲)
    查找find,拷贝copy,删除erase,替换replace,插入insert

19.2 string构造

(1)默认构造函数:

string();    //构造一个空的字符串string s1。

(2)拷贝构造函数:

string(const string &str);  //构造一个与str一样的string。如string s1(s2)。

(3)带参数的构造函数

string(const char *s);    //  用字符串s初始化
string(int n,char c);    //  用n个字符c初始化

例子:
在这里插入图片描述
完整示例代码:

#include <iostream>
#include <string>using namespace std;int main()
{string s1;                 //无参构造函数string s2("helloworld");   //有参构造函数string s3(10, 'a');   string s4(s2);            //拷贝构造函数cout << s1 << endl;   //重载了输出运算符 << cout << s2 << endl;cout << s3 << endl;cout << s4 << endl;cin >> s1;   //重载了输入运算符 >> cout << s1 << endl;s1 += "helloworld";  // 重载了 += 运算符if (s1 == s2)	// 重载了 == 运算符{}s1 = s1 + s2;   // 重载了 = 运算符return 0;
}

运行结果:
在这里插入图片描述

19.3 string使用

(1)string的存取字符操作:[ ] 或者 s.at( )

  • 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()在越界时会抛出异常,[ ]在刚好越界时会返回(char)0,再继续越界时,编译器直接出错。如果你的程序希望可以通过try,catch捕获异常,建议采用at()。
    在这里插入图片描述
    在这里插入图片描述
    在这里插入图片描述
    完整示例代码:
#include <iostream>
#include <exception>using namespace std;int main()
{string s("helloworld");cout << s[1] << endl;        //重载了下标运算符s[1] = 'x';cout << s << endl;cout << s.at(1) << endl;     //通过成员函数来访问下标为 1 的元素//cout << s[20000] << endl;    //越界访问程序异常结束try{cout << s.at(10) << endl;      //使用成员函数at(),越界访问会抛出异常}catch (exception &e){cout << e.what() << endl;}return 0;
}

运行结果:
在这里插入图片描述

(2)从string取得字符串首地址的操作:s.c_str( )

  • const char *c_str() const; //返回一个以’\0’结尾的字符串的首地址
    在这里插入图片描述
    完整示例代码:
#include <iostream>
#include <string.h>
#include <string>using namespace std;int main()
{char buf[32] = {0};string s("helloworld");strcpy(buf, s.c_str());     //c_str()返回字符串的首地址cout << buf << endl;return 0;
}

运行结果:
在这里插入图片描述

(3)把string拷贝的操作:s.copy( )

把字符串s从第0给下标开始,拷贝5个字符到buf中去。
在这里插入图片描述在这里插入图片描述
完整示例代码:

#include <iostream>
#include <string.h>using namespace std;int main()
{char buf[32] = {0};string s("helloworld");s.copy(buf, 5);               //第二个参数表示拷贝5个字节,第三个参数是默认参数,拷贝的位置,默认是0cout << buf << endl;memset(buf, 0, sizeof(buf));s.copy(buf, 4, 5); // 拷贝四个字节,从下标为5的字符开始拷贝cout << buf << endl;return 0;
}

运行结果:
在这里插入图片描述

(4)string的长度:s.length() 与 s.empty()

完整示例代码:

#include <iostream>using namespace std;int main()
{string s("helloworld");cout << s.length() << endl;if (s.empty()){cout << "长度为空" << endl;}else{cout << "长度不为空" << endl;}return 0;
}

运行结果:
在这里插入图片描述

(5)string的赋值:重载的 = 运算符 与 s.assign()

示例代码:

#include <iostream>using namespace std;int main()
{string s1("helloworld");s1 = "hello";        //重载了=运算符cout << s1 << endl;const char *s = "this is test";s1.assign(s);  // 把s这个字符串赋值给s1cout << s1 << endl;s1.assign(s, 7); //  把s这个字符串的前7个字符赋值给s1cout << s1 << endl;s1.assign(5, 'a');     //把5个a赋值给s1cout << s1 << endl;string s2("hey boy");s1.assign(s2);         //把对象s2赋值给s1cout << s1 << endl;s1.assign(s2, 4, 3); // 把s2从第四个字符开始往后3个字符,赋值给字符串s1cout << s1 << endl;return 0;
}

运行结果:

在这里插入图片描述

(6)string字符串连接:重载 += 运算符 与 s.append()

示例代码:

#include <iostream>using namespace std;int main()
{string s1("helloworld");s1 += "1234";    //重载了 += 运算符cout << s1 << endl;string s2("abcdefg");s1 += s2;cout << s1 << endl;const char *s = "haha";s1.append(s);cout << s1 << endl;s1.append(s, 2); // 把 s 前两个字符加在s1后面cout << s1 << endl;s1.append(s2);cout << s1 << endl;s1.append(s2, 4, 2);  // 把 s 第四个字符开始往后两个字符加在s1后面cout << s1 << endl;s1.append(10, 'x'); // 把10个x字符追加在字符串cout << s1 << endl;return 0;
}

运行结果:
在这里插入图片描述

(7)string的比较:s.compare()

示例代码:

#include <iostream>using namespace std;int main()
{string s1("helloworld");string s2("helloboy");const char *s = "hellogirl";if (s1.compare(s2) > 0){cout << s1 << " > " << s2 << endl;}if (s1.compare(s) > 0){cout << s1 << " > " << s << endl;}return 0;
}

运行结果:
在这里插入图片描述

(8)string的子串:s.substr()

示例代码:

#include <iostream>using namespace std;int main()
{string s("helloworld");cout << s.substr() << endl; // 返回完整的字符串scout << s.substr(5, 5) << endl; // 从下标五开始,往后五个字符return 0;
}

运行结果:

在这里插入图片描述

(9)string的查找和替换:s.find() 与 s.replace()

  1. 查找
    int find(char c,int pos=0) const; //从pos开始查找字符c在当前字符串的位置

    int find(const char *s, int pos=0) const; //从pos开始查找字符串s在当前字符串的位置

    int find(const string &s, int pos=0) const; //从pos开始查找字符串s在当前字符串中的位置

    注意:find函数如果查找不到,就返回-1

    int rfind(char c, int pos=npos) const; //从pos开始从后向前查找字符c在当前字符串中的位置
    int rfind(const char *s, int pos=npos) const; int rfind(const string &s, int pos=npos) const;
    //rfind是反向查找的意思,如果查找不到, 返回-1

  2. 替换
    string &replace(int pos, int n, const char *s);//删除从pos开始的n个字符,然后在pos处插入串s
    string &replace(int pos, int n, const string &s); //删除从pos开始的n个字符,然后在pos处插入串s
    void swap(string &s2); //交换当前字符串与s2的值

完整示例代码:

#include <iostream>
#include <string.h>using namespace std;int main()
{int p;string s1("helloworld");string s2("world");p = s1.find('o');cout << p << endl; // 4p = s1.find('x');    //不存在返回-1cout << p << endl; // -1p = s1.find('o', 5);cout << p << endl; // 6p = s1.find("ll");cout << p << endl; // 2p = s1.find(s2);cout << p << endl; // 5p = s1.rfind('o');    //反向查找(但是返回的位置还行从左边0开始)cout << p << endl; // 6s1.replace(5, 5, "xxx");cout << s1 << endl; // helloxxxxxs1.replace(5, 3, s2);cout << s1 << endl; // helloworldstring s3("helloworldhelloworldhelloworldhelloworld");p = s3.find("world");while (p != -1){s3.replace(p, strlen("world"), "x");p = s3.find("world", p + strlen("x"));}cout << s3 << endl;return 0;
}

运行结果:
在这里插入图片描述

(10)String的区间删除和插入:s.insert() 与 s.erase()

完整示例代码:

#include <iostream>using namespace std;int main()
{string s1("helloworld");string s2("12345");s1.insert(0, "this is ");// 从下标为0处,插入this iscout << s1 << endl; s1.insert(10, s2); // 从下标为10处,插入s2cout << s1 << endl; s1.insert(0, 5, 'x'); // 从下标为0处,插入5个xcout << s1 << endl;s1.erase(0, 20);cout << s1 << endl; // 删除下标0到20return 0;
}

运行结果:
在这里插入图片描述

http://www.yayakq.cn/news/990464/

相关文章:

  • 昆明做网站的公司有哪些四川今天刚刚发生的新闻
  • 工商注册在哪个网站建筑网校哪个比较好
  • 校园互动网站建设wordpress文章图片点击放大浏览
  • wordpress网站重新安装插件红酒网站程序
  • 怎么做微信网站吗网站产品整合推广
  • 一起做网店网站官方网站的建设时间表
  • 福州市建设管理处网站专业的网站建设报价
  • 隆基泰和 做网站网站如何制作浙江
  • 网站加入百度地图做生意必定红火的公司名字
  • 怎样租用个人网站空间网站被降权会发生什么
  • 登陆网站怎么做杭州互联网网站公司
  • 四合一网站logo生成器app
  • 义乌网站建设技巧培训网站建设完工后在什么科目核算
  • 深圳网站制作公司专业网站网站建设安全要求
  • 免费美食网站源码wordpress缓存数据库
  • 企业网站建设套餐价格广点通广告在哪里投放广告
  • 网站上的地图代码建网站多少钱 万户
  • 网络网站建设网站代码怎么放
  • 模板云网站建设广州网站车管所
  • 怀化网站建设联系方式江岸区建设局网站
  • 龙岩网站建设专家oss做静态网站
  • 免费做一建或二建题目的网站什么是网络营销?网络营销有哪些特点?
  • 高台县建设局网站深圳公明做网站
  • 福州免费企业网站建站四川建设网官网app
  • 海关总署2018年海关网站建设成都做app定制
  • 国外好的网站空间十大耐玩手机单机游戏
  • 网站建设负责人证明霍邱县住房和城乡建设局网站
  • 中国有名的网站建设公司淘宝提货网站怎么做的
  • 设计网站作品培训网站开发公司
  • 时尚类网站设计公司高中同步测控优化设计答案