string类的常用接口说明

news/2024/11/16 21:33:40/

STL六大组件:

容器 

算法

配接器

迭代器

仿函数

空间配置器

温馨提示:只讲常用接口,使用方法说明详见代码注释

目录

一、string类对象的常见构造

二、string类对象的容量操作

三、类对象的访问及遍历操作

四、string类对象的修改操作

五、string类非成员函数

六、汇总代码


一、string类对象的常见构造

#include<iostream>
using namespace std;
#include<string>
//typedef basic_string<char16_t> u16string;std::string s0("Initial string");//构造空的string类对象,即空字符串(默认构造)
//default (1)	explicit basic_string(const allocator_type& alloc = allocator_type());
std::string s1;//用C-string来构造string类对象
//copy(2)    basic_string(const basic_string& str);
std::string s2(s0);//拷贝构造函数
//copy(2)    basic_string(const basic_string& str);
std::string s2(s0);void Teststring()
{string s1;//构造空的string类对象s1string s2("hello sunlang");//用C格式字符串构造string类对象s2string s3(s2);//拷贝构造s3
}

二、string类对象的容量操作

//返回字符串有效字符长度
//size_type size() const;
int main()
{std::string str("Test string");std::cout << "The size of str is" << str.size() << "characters.\n";return 0;
}
//检测字符串释放为空串,是返回true,否则返回false
//bool empty() const;
int main()
{std::string content;std::string line;std::cout << "Please introduce a text.Enter an empty line to finish:\n";do{getline(std::cin, line);content += line + '\n';} while (!line.empty());std::cout << "The text you introduced was:\n" << content;return 0;
}
//清空有效字符
//void clear();
int main()
{char c;std::string str;std::cout << "Please type some lines of text. Enter a dot (.) to finish:\n";do {c = std::cin.get();str += c;if (c == '\n'){std::cout << str;str.clear();}} while (c != '.');return 0;
}
//为字符串预留空间**
//void reserve (size_type n = 0);
int main()
{std::string str;std::ifstream file("test.txt", std::ios::in | std::ios::ate);if (file) {std::ifstream::streampos filesize = file.tellg();str.reserve(filesize);file.seekg(0);while (!file.eof()){str += file.get();}std::cout << str;}return 0;
}
//将有效字符的个数改成n个,多出的空间用字符C填充
//void resize (size_type n);
//void resize(size_type n, charT c)
int main()
{std::string str("I like to code in C");std::cout << str << '\n';std::string::size_type sz = str.size();str.resize(sz + 2, '+');std::cout << str << '\n';str.resize(14);std::cout << str << '\n';return 0;
}

三、类对象的访问及遍历操作

//返回pos位置的字符,const string类对象调用
//reference operator[] (size_type pos);
//const_reference operator[] (size_type pos) const;
int main()
{std::string str("Test string");for (int i = 0; i < str.length(); ++i){std::cout << str[i];}return 0;
}

四、string类对象的修改操作

//在字符串后面追加字符串str
//string(1)
//basic_string& operator+= (const basic_string& str);
//c - string(2)
//basic_string & operator+= (const charT * s);
//character(3)
//basic_string& operator+= (charT c);
int main()
{std::string name("John");std::string family("Smith");name += "K";         // c-stringname += family;         // stringname += '\n';           // characterstd::cout << name;return 0;
}
//返回C格式字符串
//const charT* c_str() const;
int main()
{std::string str("Please split this sentence into tokens");char* cstr = new char[str.length() + 1];std::strcpy(cstr, str.c_str());// cstr now contains a c-string copy of strchar* p = std::strtok(cstr, " ");while (p != 0){std::cout << p << '\n';p = strtok(NULL, " ");}delete[] cstr;return 0;
}
//从字符串pos位置开始往后找字符C,返回该字符在字符串中的位置
//string(1)
//size_type find(const basic_string& str, size_type pos = 0) const;
//c - string(2)
//size_type find(const charT * s, size_type pos = 0) const;
//buffer(3)
//size_type find(const charT* s, size_type pos, size_type n) const;
//character(4)
//size_type find(charT c, size_type pos = 0) const;
int main()
{std::string str("There are two needles in this haystack with needles.");std::string str2("needle");// different member versions of find in the same order as above:std::string::size_type found = str.find(str2);if (found != std::string::npos)std::cout << "first 'needle' found at: " << found << '\n';found = str.find("needles are small", found + 1, 6);if (found != std::string::npos)std::cout << "second 'needle' found at: " << found << '\n';found = str.find("haystack");if (found != std::string::npos)std::cout << "'haystack' also found at: " << found << '\n';found = str.find('.');if (found != std::string::npos)std::cout << "Period found at: " << found << '\n';// let's replace the first needle:str.replace(str.find(str2), str2.length(), "preposition");std::cout << str << '\n';return 0;
}

五、string类非成员函数

//输入运算符重载
//template <class charT, class traits, class Alloc>
//basic_istream<charT, traits>& operator>> (basic_istream<charT, traits>& is,
//	basic_string<charT, traits, Alloc>& str);
int main()
{std::string name;std::cout << "Please, enter your name: ";std::cin >> name;std::cout << "Hello, " << name << "!\n";return 0;
}
//输出运算符重载
//template <class charT, class traits, class Alloc>
//basic_ostream<charT, traits>& operator<< (basic_ostream<charT, traits>& os,
//	const basic_string<charT, traits, Alloc>& str);
int main()
{std::string str = "Hello world!";std::cout << str << '\n';return 0;
}
//获取一行字符串
//template <class charT, class traits, class Alloc>
//(1)basic_istream<charT, traits>& getline(basic_istream<charT, traits>& is,
//	basic_string<charT, traits, Alloc>& str, charT delim);
//(2)
//template <class charT, class traits, class Alloc>
//basic_istream<charT, traits>& getline(basic_istream<charT, traits>& is,
//	basic_string<charT, traits, Alloc>& str);
int main()
{std::string name;std::cout << "Please, enter your full name: ";std::getline(std::cin, name);std::cout << "Hello, " << name << "!\n";return 0;
}
//大小比较
int main()
{std::string foo = "alpha";std::string bar = "beta";if (foo == bar) std::cout << "foo and bar are equal\n";if (foo != bar) std::cout << "foo and bar are not equal\n";if (foo < bar) std::cout << "foo is less than bar\n";if (foo > bar) std::cout << "foo is greater than bar\n";if (foo <= bar) std::cout << "foo is less than or equal to bar\n";if (foo >= bar) std::cout << "foo is greater than or equal to bar\n";return 0;
}

六、汇总代码

#include<iostream>
using namespace std;
#include<string>
//测试string容量相关的接口
//size/clear/resize
void Teststring1()
{//注意:string类对象支持直接用cin和cout进行输入和输出string s("hello,sunlang!!!");//用C-siring来构造string类对象cout << s.size() << endl;//返回字符串有效字符长度cout << s.length() << endl;//返回字符串有效字符长度cout << s.capacity() << endl;//返回空间总大小cout << s << endl;//将s中的字符串清空,注意清空时只是将size清0,不改变底层空间的大小s.clear();cout << s.size() << endl;cout << s.capacity() << endl;//将s中有效字符个数增加到10个,多出位置用‘a'进行补充//"aaaaaaaa"s.resize(10, 'a');cout << s.size() << endl;cout << s << endl;cout << s.capacity() << endl;//将s中有效字符个数增加到15个,多出位置用缺省值'\0'进行补充//"aaaaaaaa\0\0\0\0"s.resize(15);cout << s.size() << endl;cout << s.capacity() << endl;cout << s << endl;//将s中有效字符个数缩小到5个s.resize(5);cout << s.size() << endl;cout << s.capacity() << endl;cout << s << endl;
}
void Teststring2()
{string s;//测试reserve是否会改变string中有效元素个数s.reserve(100);cout << s.size() << endl;cout << s.capacity() << endl;//测试reserve参数小于string的底层空间大小时,是否会将空间缩小s.reserve(50);cout << s.size() << endl;cout<<s.capacity()<<endl;
}
//利用reserve提高插入数据的效率,避免增容带来的开销
void TestPushBack()
{string s;size_t sz = s.capacity();cout << "making s grow:\n";for (int i = 0; i < 100; ++i){s.push_back('c');if (sz != s.capacity()){sz = s.capacity();cout << "capacity changed:" << sz << '\n';}}
}
//构建vector时,如果提前已经知道string中大概要放多少个元素,可以提前将string中空间设置好
void TestPushBackReserve()
{string s;s.reserve(100);size_t sz = s.capacity();cout << "making s grow:\n";for (int i = 0; i < 100; ++i){s.push_back('c');if (sz != s.capacity()){sz = s.capacity();cout << "capacity changed:" << sz << '\n';}}}
//string的遍历
//begin()+end()   for+[]   范围for
//注意:string遍历时使用最多的还是for+下标或者范围for(C++11后才支持)
//begin()+end()大多数使用在需要使用STL提供的算法操作string时,比如:采用reserve逆置string
void Teststring3()
{string s1("hello sunlang");const string s2("Hello sunlang");cout << s1 << " " << s2 << endl;cout << s1[0] << " " << s2[0] << endl;s1[0] = 'H';cout << s1 << endl;//s2[0]='h';代码编译失败,因为const类型对象不能修改
}
void Teststring4()
{string s("hello sunlang");//以下三种方式除了遍历string对象,还可以遍历修改string中的字符//第一种使用最多//for+operator[]for (size_t i = 0; i < s.size(); ++i){cout << s[i] << endl;}//迭代器string::iterator it = s.begin();while (it != s.end()){cout << *it << endl;++it;}//string::reserve_iterator rit =s.rbegin();//C++11之后,直接使用auto定义迭代器,让编译器推到迭代器的类型//auto rit = s.rbegin();//while (rit != s.rend())//{//	cout << *rit << endl;//}//范围forfor (auto ch : s){cout << ch << endl;}
}
//测试string
//1、插入(拼接)方式:push_back append operator+=
//2、正向和反向查找:find()+rfind()
//3、截取子串:substr()
//4、删除:erase
void Teststring5()
{string str;str.push_back(' ');//在str后插入空格str.append("hello");//在str后追加一个字符"hello"str += 's';//在str后追加一个字符'b'str += "un";//在str后追加一个字符串"it"cout << str << endl;cout << str.c_str() << endl;//以C语言的方式打印字符串//获取file的后缀string file("Test.cpp");size_t pos = file.rfind('.');string suffix(file.substr(pos, file.size() - pos));cout << suffix << endl;//npos是string里面的一个静态成员变量//static const size_t npos=-1;//取出ur1中的域名string ur1("https://mp.csdn.net/mp_blog/creation/editor/128085730");cout << ur1 << endl;size_t start = ur1.find("://");if (start == string::npos){cout << "invalid url" << endl;return;}start += 3;size_t finish = url.find('/', start);string address = url.substr(start, finish - start);cout << address << endl;// 删除 url 的协议前缀pos = url.find("://");url.erase(0, pos + 3);cout << url << endl;
}
int main()
{Teststring1();Teststring2();TestPushBack();TestPushBackReserve();Teststring3();Teststring4();Teststring5();return 0;
}


http://www.ppmy.cn/news/42.html

相关文章

Vue2 中使用“全局事件总线“实现任意组件间通信

前言&#xff1a; vue 中组件间通信的方式有很多 ------ 父传子propos&#xff0c;全局事件总线&#xff0c;消息订阅&#xff0c;vuex......等等&#xff0c;这篇文章带大家学习一下通过全局事件总线来实现任意组件间的通信。 文章目录&#xff1a; 一&#xff1a;什么是全局…

Python写了个疫情信息快速查看工具

年关将至&#xff0c;大家对疫情的关注度也愈发提升&#xff0c;本次使用PyQt5撰写100行代码写一个疫情信息快速查看工具。 一&#xff0e;准备工作 1.PyQt5 PyQt 是一个用于创建GUI应用程序的跨平台的工具包&#xff0c;它将Python编程语言和Qt库 成功融合在一起。QT库目前…

HTML5期末考核大作业 基于HTML+CSS+JavaScript仿王者荣耀首页 游戏网站开发 游戏官网设计与实现

&#x1f389;精彩专栏推荐&#x1f447;&#x1f3fb;&#x1f447;&#x1f3fb;&#x1f447;&#x1f3fb; ✍️ 作者简介: 一个热爱把逻辑思维转变为代码的技术博主 &#x1f482; 作者主页: 【主页——&#x1f680;获取更多优质源码】 &#x1f393; web前端期末大作业…

Spark学习(6)-Spark SQL

1 快速入门 SparkSQL是Spark的一个模块, 用于处理海量结构化数据。 SparkSQL是非常成熟的 海量结构化数据处理框架. 学习SparkSQL主要在2个点: SparkSQL本身十分优秀, 支持SQL语言\性能强\可以自动优化\API简单\兼容HIVE等等。企业大面积在使用SparkSQL处理业务数据。 离线开…

http和dubbo接口调用主动设置超时时间

http接口超时方案 方案1&#xff1a;多个resttemplate&#xff0c;不同超时时间的使用不同的template&#xff0c;优点&#xff1a;简单&#xff0c;扩展起来复制粘贴&#xff0c;缺点&#xff1a;代码冗余&#xff0c;多个template占用内存不够优雅 方案2&#xff1a;单个res…

深度学习day01

Marchine leariing 机器学习就是自动找函式 告诉机器要找的函式用 Supervised Learning 函式的Loss ——评价函式的好坏 Reinforcement就是让机器自己下象棋&#xff0c;输赢自己尝试&#xff0c;没像监督学习那样有人为规定 给函式寻找范围&#xff1a; 函式寻找方法——…

基于单RGB相机的全新三维表示方法|NeurIPS 2022

随着深度学习的发展&#xff0c;基于单张RGB图像的人体三维重建取得了持续进展。 但基于现有的表示方法&#xff0c;如参数化模型、体素栅格、三角网格和隐式神经表示&#xff0c;难以构筑兼顾高质量结果和实时速度的系统。 针对上述问题&#xff0c;天津大学团队联合清华大学…

Spire.XLS for Java 12.11.8 Excel to PDF bug Fix

谷歌找破解版Spire.XLS for Java is a professional Java Excel API that enables developers to create, manage, manipulate, convert and print Excel worksheets without using Microsoft Office or Microsoft Excel. Spire.XLS for Java supports both for the old Excel …