本篇将深入学习string类,通过各个测试函数玩遍c++string类,学到就是赚到!!!
文章目录
- 1.头文件和源文件
- 1.1源文件
- 1.2头文件
- 2.构造函数
- 3.赋值重载函数
- 4.元素访问运算符
- 5.迭代器
- 5.1正向迭代器
- 5.2反向迭代器
- 6.添加字符串
- 7.vs下扩容机制
- 8.insert()函数
- 9.c_str()函数
- 10.find、rfind、substr
- 10.1综合应用
- 10.2rfind()应用
- 11.find_first_of
- 12.字符串转换
- 13.运行结果
1.头文件和源文件
完整代码点击 string类学习 跳转码云获取
1.1源文件
#include"string.h"
int main()
{/*test_string01();test_string02();test_string03();test_string04();test_string06();test_string07();test_string08();test_string09();test_string10();test_string11();test_string12();test_string13();*/return 0;
}
1.2头文件
#paragma once
#include<iostream>
#include<string>
#include<list>
#include<assert.h>
using namespace std;
2.构造函数
void test_string01()
{//1.无参构造 string();string s1;//2.有参构造 string (const char* s);//string s2("hello world"); string s2 = "hello world";//*s--tmp--s2 构造+拷贝//3.拷贝构造 string (const string& str);string s3(s2);//4.部分拷贝构造//string(const string & str, size_t pos, size_t len = npos);//len参数分析://(1)不传参:取缺省值-1==取到结尾//(2)len > n:取到结尾string s4(s2, 6, 5);//5.部分构造string (const char* s, size_t n);string s5("hello world", 5);//6.字符构造string (size_t n, char c);string s6(10, '*');
}
3.赋值重载函数
void test_string02()
{string s1;string s2 = "hello world";//构造+拷贝->构造//1.string& operator= (const string& str);s1 = s2;//2.string& operator= (const char* s);s1 = "xxx";//3.string& operator= (char c);s1 = 'x';
}
4.元素访问运算符
void test_string03()
{string s1("hello world");//s1.operator[](2)cout << s1[2] << endl;s1[2] = 'x';cout << s1[2] << endl;//1.char& operator[] (size_t pos); //遍历string,每个字符+1for (size_t i = 0; i < s1.size(); i++){s1[i]++;}cout << s1 << endl;//2.const char& operator[] (size_t pos) const;const string s2("world");for (size_t i = 0; i < s1.size(); i++){cout << s2 << endl;}//3.内部检查越界//s2[6];//at运算符//与operator[]区别:// 越界抛异常//char& at(size_t pos); //const char& at(size_t pos) const;
}
5.迭代器
5.1正向迭代器
//iterator begin();
//const_iterator begin() const;
void test_string04()
{//1.遍历sstring s("hello");string::iterator it = s.begin();while (it != s.end()){(*it)++;cout << *it << " ";++it;}cout << endl;// 2.范围for -- 自动迭代,自动判断结束// 依次取s中每个字符,赋值给ch/*for (auto ch : s){ch++;cout << ch << " ";}*/for (auto& ch : s){ch++;cout << ch << " ";}cout << endl << s << endl;//3.list同样适用list<int> lt(10, 1);list<int>::iterator lit = lt.begin();while (lit != lt.end()){cout << *lit << " ";++lit;}cout << endl;for (auto e : lt){cout << e << " ";}cout << endl;// 范围for底层其实就是迭代器
}
5.2反向迭代器
void func(const string& str)
{//正向迭代器//string::const_iterator it = str.begin();auto it = str.begin();while (it != str.end()){//*it = 'x';cout << *it << " ";++it;}cout << endl;//反向迭代器//string::const_reverse_iterator rit = str.rbegin();auto rit = str.rbegin();while (rit != str.rend()){cout << *rit << " ";++rit;}cout << endl;
}void test_string05()
{string s("hello");string::reverse_iterator rit = s.rbegin();while (rit != s.rend()){cout << *rit << " ";++rit;}cout << endl;func(s);//迭代器:// //iterator begin();//const_iterator begin() const;//reverse_iterator rbegin(); //const_reverse_iterator rbegin() const;
}
6.添加字符串
void test_string06()
{//void push_back(char c);string s("hello");s.push_back('-');s.push_back('-');//string& append(const char* s);s.append("world");cout << s << endl;//string& operator+= (const string & str);//string & operator+= (const char* s);//string& operator+= (char c);string str("我来了");s += str;s += "!!!";s += '@';cout << s << endl;//template <class InputIterator> //string& append(InputIterator first, InputIterator last);s.append(++str.begin(), --str.end());cout << s << endl;//string copy(++s.begin(), --s.end());string copy(s.begin() + 5, s.end() - 5);cout << copy << endl;
}
7.vs下扩容机制
void test_string07()
{//string s1("11111111111111");//cout << s1.max_size() << endl;//cout << s1.capacity() << endl;string s;s.reserve(1000); //开空间//s.resize(1000, 'x'); //开空间+初始化size_t sz = s.capacity();sz = s.capacity();cout << "capacity changed: " << sz << '\n';cout << "making s grow:\n";for (int i = 0; i < 1000; ++i){s.push_back('c');if (sz != s.capacity()){sz = s.capacity();cout << "capacity changed: " << sz << '\n';}}
}
8.insert()函数
//string& insert (size_t pos, const char* s);
void test_string08()
{string str("wo lai le");//1.使用易错// wo20%20% lai le /*for (size_t i = 0; i < str.size();i++){if (str[i] == ' '){str.insert(i, "20%");i += 4;}}*///2.1修正//wo20% lai20% le/*for (size_t i = 0; i < str.size();){if (str[i] == ' '){str.insert(i, "20%");i += 4;}else{++i;}}*///2.2修正//string& erase (size_t pos = 0, size_t len = npos);/*for (size_t i = 0; i < str.size(); ++i){if (str[i] == ' '){str.insert(i, "20%");i += 3;}}cout << str << endl;for (size_t i = 0; i < str.size(); ++i){if (str[i] == ' '){str.erase(i, 1);}}cout << str << endl;*///3.改进(以空间换时间)string newstr;for (size_t i = 0; i < str.size(); ++i){if (str[i] != ' '){newstr += str[i];}else{newstr += "20%";}}cout << newstr << endl;//4.replace(效率不高)//string& replace (size_t pos, size_t len, const char* s);for (size_t i = 0; i < str.size(); ++i){if (str[i] == ' '){str.replace(i, 1, "20%");}}cout << str << endl;
}
9.c_str()函数
const char* c_str() const;
void test_string09()
{string filename("test.cpp");FILE* fout = fopen(filename.c_str(), "r");assert(fout);char ch = fgetc(fout);while (ch != EOF){cout << ch;ch = fgetc(fout);}
}
//C++与C的字符串区别
void test_string10()
{string filename("test.cpp");cout << filename << endl;cout << filename.c_str() << endl;filename += '\0';filename += "string.cpp";cout << filename << endl;cout << filename.c_str() << endl;
}
10.find、rfind、substr
10.1综合应用
void DealUrl(const string& url)
{size_t pos1 = url.find("://");if (pos1 == string::npos){cout << "非法url" << endl;return;}string protocol = url.substr(0, pos1);cout << protocol << endl;size_t pos2 = url.find('/', pos1 + 3);if (pos2 == string::npos){cout << "非法url" << endl;return;}string domain = url.substr(pos1 + 3, pos2 - pos1 - 3);cout << domain << endl;string uri = url.substr(pos2 + 1);cout << uri << endl << endl;
}
void test_string11()
{string filename("test.cpp.tar.zip");//size_t find(char c, size_t pos = 0) const;//size_t rfind(char c, size_t pos = npos) const;//size_t pos = filename.find('.');size_t pos = filename.rfind('.');if (pos != string::npos){//string substr (size_t pos = 0, size_t len = npos) const;//string suff = filename.substr(pos, filename.size() - pos);string suff = filename.substr(pos);cout << suff << endl;}//协议名://域名/动作string url1 = "https://cplusplus.com/reference/string/string/";string url3 = "ftp://ftp.cs.umd.edu/pub/skipLists/skiplists.pdf";DealUrl(url1);DealUrl(url3);
}
10.2rfind()应用
//输出一行字符串中最后一个单词长度
void function()
{string str;//istream& getline(istream & is, string & str);getline(cin, str);size_t pos = str.rfind(' ');if (pos != string::npos){cout << str.size() - pos - 1;}elsecout << str.size() << endl;
}
11.find_first_of
size_t find_first_of (const char* s, size_t pos = 0) const;
void test_string12()
{string str("Please, replace the vowels in this sentence by asterisks.");size_t found = str.find_first_of("aeiou");while (found != string::npos){str[found] = '*';found = str.find_first_of("aeiou", found + 1);}//遍历A--找到与B相同的字符返回位置cout << str << '\n';
}
12.字符串转换
void test_string13()
{int ival;double dval;cin >> ival >> dval;string istr = to_string(ival);string dstr = to_string(dval);cout << istr << endl;cout << dstr << endl;istr = "9999";dstr = "9999.99";ival = stoi(istr);dval = stod(dstr);
}