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

郑州个人做网站汉狮2023免费推广入口

郑州个人做网站汉狮,2023免费推广入口,logo制作下载,wordpress获得最新评论string基本概念 string是C风格的字符串,本质上是一个类。 string 和 char* 的区别 char* 是一个指针; string是一个类,内部封装了 char* ,用来管理字符串,是一个 char* 型的容器。 特点 string内部封装了很多成员…

string基本概念

string是C++风格的字符串,本质上是一个类。

string 和 char* 的区别

char* 是一个指针;

string是一个类,内部封装了 char* ,用来管理字符串,是一个 char* 型的容器。

特点

string内部封装了很多成员函数

例如:查找find、拷贝copy、删除delete、替换replace、插入insert

string管理 char* 分配的内存,不用担心复制越界和取值越界等,由类内部负责。

string的构造函数

#include <iostream>
#include <Windows.h>
#include <string>
#include <vector>
#include <algorithm> // 使用STL提供的遍历算法,遍历vector容器using namespace std;void test()
{// 默认构造string s1 = "Hello World";cout << "s1 = " << s1 << endl;// 第二种构造函数const char* str = "Hello World";string s2(str);cout << "s2 = " << s2 << endl;// 第三种构造函数string s3(s2);cout << "s3 = " << s3 << endl;// 第四种构造函数string s4(10, 'A');cout << "s4 = " << s4 << endl;
}int main(void)
{test();system("pause");return 0;
}

运行截图:

 string赋值操作

#include <iostream>
#include <Windows.h>
#include <string>using namespace std;void test()
{// 第一种赋值方式string s1 = "Hello World";cout << "s1 = " << s1 << endl;// 第二种赋值方式string s2;s2 = s1;cout << "s2 = " << s2 << endl;// 第三种赋值方式string s3;s3 = 'A';cout << "s3 = " << s3 << endl;// 第四种赋值方式string s4;s4.assign("Hello C++");cout << "s4 = " << s4 << endl;//第五种赋值方式string s5;s5.assign("Hello C++", 5);cout << "s5 = " << s5 <<endl;// 第六种赋值方式string s6;s6.assign(s5);cout << "s6 = " << s6 <<endl;// 第七种赋值方式string s7;s7.assign(10, 'W');cout << "s7 = " << s7 << endl;}int main(void)
{test();system("pause");return 0;
}

运行截图:

string字符串拼接

#include <iostream>
#include <Windows.h>
#include <string>using namespace std;void test()
{// 第一种字符串拼接方式string str1 = "Hello";str1 += " C++";cout << str1 << endl;// 第二种字符串拼接方式str1 += '!';cout << str1 << endl;// 第三种字符串拼接方式string str3 = " Good morning";str1 += str3;cout << str1 << endl;// 第四种字符串拼接方式string str4 = "I";str4.append(" Love ");cout << str4 << endl;// 第五种字符串拼接方式string str5 = str4;str5.append("C++ and Java", 3);cout << str5 << endl;// 第六种字符串拼接方式string str6 = "你好:";str6.append(str1);cout << str6 << endl;// 第七种字符串拼接方式string str7;str7.append(str1, 0, 9);cout << str7 << endl;}int main(void)
{test();system("pause");return 0;
}

运行截图:

 

string字符串查找和替换

#include <iostream>
#include <Windows.h>
#include <string>using namespace std;// 字符串查找
void test()
{string s1 = "Hello World";int pos = s1.find("Hello");if (pos == -1){cout << "未找到字符串" << endl;}else{cout << "pos = " << pos << endl; // pos = 0}
}// 字符串替换
void test02()
{string str = "Hello World";str.replace(6, 5, "C++");cout << str << endl;
}int main(void)
{test();test02();system("pause");return 0;
}

运行截图:

 

string字符串比较

#include <iostream>
#include <Windows.h>
#include <string>using namespace std;void test()
{string s1 = "Hello World";string s2 = "Hello";if (s1.compare(s2) == 0){cout << "字符串相等" << endl;}else{cout << "字符串不等" << endl;}
}int main(void)
{test();system("pause");return 0;
}

运行截图:

 

string字符串存取

#include <iostream>
#include <Windows.h>
#include <string>using namespace std;void test()
{string s = "Hello World";// 1、通过[]访问单个字符for (int i = 0; i < s.size(); i++){cout << s[i] << "  ";}cout << endl;// 2、通过at访问单个字符for (int i = 0; i < s.size(); i++){cout << s.at(i) << "  ";}cout << endl;}int main(void)
{test();system("pause");return 0;
}

运行截图:

 

string子串获取

#include <iostream>
#include <Windows.h>
#include <string>using namespace std;void test()
{string s1 = "Hello World";string s2 = s1.substr(6, 5);cout << s2 << endl; // 输出World
}int main(void)
{test();system("pause");return 0;
}

运行截图:

http://www.shuangfujiaoyu.com/news/8257.html

相关文章:

  • 黄山旅游攻略2022seo外链发布平台有哪些
  • 女人与马做受网站保定百度推广优化排名
  • 做刷机网站赚钱吗seo分析网站
  • 公司网站可以自己做么百度推广工作怎么样
  • 南昌做兼职的网站设计泉州百度网站推广
  • 橡胶东莞网站建设技术支持广州抖音推广公司
  • 网站做兼容处理怎么设置靠谱的代写平台
  • 昌平网站制作快速排名优化怎么样
  • 网站外链分析怎么做最佳bt磁力搜索引擎
  • 龙湖建设工程有限公司网站杭州龙席网络seo
  • 嘉兴 做网站 推广成都seo公司排名
  • it网站制作策划seo优化网络推广
  • 网站运营这么做产品怎么在网上推广
  • 商丘市网站建设公司网站怎么做到秒收录
  • 网站开发经理具备什么知识如何做互联网营销推广
  • 多语言网站实现如何推广网店
  • 最火的推广软件优化网站关键词排名
  • 东莞做网站哪里好自建站模板
  • 设计师免费资源导航seo01
  • 音乐网站制作源代码广州网站设计建设
  • asp网站开发源码2023年新闻摘抄十条
  • 网站开发设计技术链友之家
  • 西安网站建设huanxiseo网站推广专员
  • 长沙网站推广¥做下拉去118cr百度总部公司地址在哪里
  • rest api wordpressseo网站有哪些
  • 多用户商城(c2c)网站制作方案山西百度查关键词排名
  • 网站建设管理是怎么用模板做网站
  • 电商网站更适合seo基础入门教程
  • 百度给做网站吗搜索引擎推广实训
  • 做交通锁具网站今天的病毒感染情况