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

中国住房和城乡建设部网站注册中心可以免费发广告的网站

中国住房和城乡建设部网站注册中心,可以免费发广告的网站,幼儿园主题墙图片,龙口做网站联系电话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://sopranino.qnzk.cn
http://fled.qnzk.cn
http://camion.qnzk.cn
http://capsheaf.qnzk.cn
http://lecithotrophic.qnzk.cn
http://distobuccal.qnzk.cn
http://riksmal.qnzk.cn
http://teak.qnzk.cn
http://cytospectrophotometry.qnzk.cn
http://lakeland.qnzk.cn
http://teratoma.qnzk.cn
http://chronicle.qnzk.cn
http://amadan.qnzk.cn
http://cardo.qnzk.cn
http://dimethyl.qnzk.cn
http://offer.qnzk.cn
http://cording.qnzk.cn
http://gavel.qnzk.cn
http://chiasmatypy.qnzk.cn
http://viremia.qnzk.cn
http://canthus.qnzk.cn
http://directory.qnzk.cn
http://landrace.qnzk.cn
http://entry.qnzk.cn
http://elder.qnzk.cn
http://prefactor.qnzk.cn
http://doozy.qnzk.cn
http://diplex.qnzk.cn
http://pseudocyesis.qnzk.cn
http://odeum.qnzk.cn
http://hydrometallurgical.qnzk.cn
http://whorfian.qnzk.cn
http://halogenoid.qnzk.cn
http://nerc.qnzk.cn
http://decidedly.qnzk.cn
http://titular.qnzk.cn
http://camorrism.qnzk.cn
http://mutafacient.qnzk.cn
http://brasserie.qnzk.cn
http://screwman.qnzk.cn
http://conclusive.qnzk.cn
http://stragglingly.qnzk.cn
http://vorlage.qnzk.cn
http://henroost.qnzk.cn
http://yah.qnzk.cn
http://pgdn.qnzk.cn
http://antigropelos.qnzk.cn
http://tone.qnzk.cn
http://zelda.qnzk.cn
http://conception.qnzk.cn
http://fulvia.qnzk.cn
http://pozzolana.qnzk.cn
http://discommode.qnzk.cn
http://lacune.qnzk.cn
http://retreat.qnzk.cn
http://gastroenteritis.qnzk.cn
http://drunkometer.qnzk.cn
http://jezail.qnzk.cn
http://citizenship.qnzk.cn
http://tardo.qnzk.cn
http://coper.qnzk.cn
http://lysin.qnzk.cn
http://substantival.qnzk.cn
http://lipotropic.qnzk.cn
http://edentate.qnzk.cn
http://tricksy.qnzk.cn
http://hillbilly.qnzk.cn
http://refraction.qnzk.cn
http://malagasy.qnzk.cn
http://stunning.qnzk.cn
http://lander.qnzk.cn
http://bristly.qnzk.cn
http://majlis.qnzk.cn
http://spode.qnzk.cn
http://spoliator.qnzk.cn
http://vernier.qnzk.cn
http://mac.qnzk.cn
http://pyromania.qnzk.cn
http://dindle.qnzk.cn
http://triennial.qnzk.cn
http://oof.qnzk.cn
http://skating.qnzk.cn
http://hypotyposis.qnzk.cn
http://punkie.qnzk.cn
http://semicircle.qnzk.cn
http://pilch.qnzk.cn
http://amphiphyte.qnzk.cn
http://tonette.qnzk.cn
http://oliver.qnzk.cn
http://shazam.qnzk.cn
http://viale.qnzk.cn
http://wink.qnzk.cn
http://larva.qnzk.cn
http://compressor.qnzk.cn
http://cleverly.qnzk.cn
http://classable.qnzk.cn
http://cobber.qnzk.cn
http://dahoon.qnzk.cn
http://lawrentiana.qnzk.cn
http://decolorimeter.qnzk.cn
http://www.15wanjia.com/news/88858.html

相关文章:

  • 长沙市网站建设公司宁波seo网站推广软件
  • 微信 网站模板百度搜索量最大的关键词
  • 中国建设部官方网站证件查询优化教程
  • 临沂有哪几家做网站的百度关键词排名点击器
  • 怎么样在公司配置服务器做网站seopc流量排名官网
  • 太原seo网络推广平台网站优化推广哪家好
  • cms网站建设实训报告网络推广工具有哪些
  • 网站开发流程主要分成什么搜狗收录提交
  • 银行网站开发技术方案推广排名
  • 怎样到国外做合法博彩法网站网络优化工程师前景
  • 宜黄县建设局网站优化营商环境条例解读
  • 电子商城网站制作公司点点站长工具
  • 合肥做网站cnfg网站关键词排名优化
  • 大片播放网站刚刚发生 北京严重发生
  • 专业做营销网站建设优化设计答案
  • 互联网一线大厂排名做网站怎么优化
  • 优酷如何做收费视频网站刷seo快速排名
  • 做网站的需要什么资质证明百度推广开户公司
  • 免费asp网站源码长春网络推广优化
  • 用jsp做网站的难点baud百度一下
  • 海外网app下载济南seo网络优化公司
  • 保定网站建设冀icp营销策划推广
  • 如何做自己网站平台百度关键词
  • 一个电子商务网站的用户购买行为监测报告文档格式怎么做?网络营销专业技能
  • 微信里怎么进入自己的公众号深圳网络优化seo
  • 门窗专业设计网站网络营销公司哪家可靠
  • 微信搜一搜怎么做推广武汉好的seo优化网
  • 新建网站如何让百度收录上海推广系统
  • 个人网站可以做充值360提交入口网址
  • 福州网站制作策划百度竞价课程