标准库中的string类(下)——“C++”

简介: 标准库中的string类(下)——“C++”

各位CSDN的uu们你们好呀,这段时间小雅兰的内容仍然是C++string类的使用的内容,下面,让我们进入string类的世界吧!!!


string类的常用接口说明


string - C++ Reference

string类的常用接口说明

string类对象的修改操作

insert

这是在第五个位置插入xxxx这个字符串!

下面的代码的意思是头插4个x字符!

头插还可以这么写,用迭代器的方式!

#include<iostream>
#include<string>
using namespace std;
int main()
{
  string s1("hello world");
  s1.insert(5, "xxxx");
  cout << s1 << endl;
 
  s1.insert(0, 4, 'x');
  cout << s1 << endl;
 
  s1.insert(s1.begin(), 'z');
  cout << s1 << endl;
    return 0;
}

insert最常见的用法还是插入字符和插入字符串!

严格来说,对于string类,insert是能少用就少用!


erase

下面两段代码的意思是:从第五个位置开始,删除四个字符

从第五个位置开始,后面有多少个字符就删多少个字符!


assign

#include<iostream>
#include<string>
using namespace std;
int main()
{
  string s1("hello world");
  s1.assign(s1);
  cout << s1 << endl;
 
  s1.assign(s1, 4, 7);
  cout << s1 << endl;
 
  s1.assign("pangrams are cool", 8);
  cout << s1 << endl;
 
  s1.assign("C-string");
  cout << s1 << endl;
    return 0;
}


replace

#include<iostream>
#include<string>
using namespace std;
int main()
{
  string s1("hello world hello lsy");
  cout << s1 << endl;
 
  //所有的空格替换成%20
  size_t pos = s1.find(' ');
  while (pos != string::npos)
  {
    s1.replace(pos, 1, "%20");
    pos = s1.find(' ');
  }
  cout << s1 << endl;
  return 0;
}

以前在学习C语言的时候,就写过程序实现这个功能,只是那时候还颇为复杂,学习了C++的string类之后,就简单多了,但是,这个程序写得还是不够好!

真正的问题是:每次find都要从头开始find,这样显然是没有必要的!

这样写就好多了!

int main()
{
   string s1("hello  world hello lsy");
   cout << s1 << endl;

   //所有的空格替换成%20
   size_t pos = s1.find(' ', 0);
   while (pos != string::npos)
   {
       s1.replace(pos, 1, "%20");
       pos = s1.find(' ', pos + 3);
   }
   cout << s1 << endl;
   return 0;
}

但是实际上,这个程序还是不会这样写,因为replace要挪动数据,效率是很低的!

下面,小雅兰就给大家介绍一种高效率的写法:

int main()
{
  string s1("hello  world hello lsy");
  cout << s1 << endl;
 
  //所有的空格替换成%20
  size_t pos = s1.find(' ', 0);
  while (pos != string::npos)
  {
    s1.replace(pos, 1, "%20");
    //效率很低,能不用就不要用了
    pos = s1.find(' ', pos + 3);
  }
  cout << s1 << endl;
 
  string s2("hello  world hello lsy");
  cout << s2 << endl;
  string s3;
  for (auto ch : s2)
  {
    if (ch == ' ')
    {
      s3 += "%20";
    }
    else
    {
      s3 += ch;
    }
  }
  cout << s3 << endl;
  s2.swap(s3);
  cout << s2 << endl;
 
  return 0;
}

swap

不过,swap是不一样的!

第一个swap会产生临时对象,会有拷贝,效率没那么高,第二个swap就是直接交换了指针!

第二种写法的唯一缺陷就是消耗了空间!


pop_back


c_str

#define _CRT_SECURE_NO_WARNINGS 1
#include<iostream>
#include<string>
using namespace std;

int main()
{
   string filename("test20240124.cpp");
   FILE* fout = fopen(filename.c_str(), "r");
   char ch = fgetc(fout);
   while (ch != EOF)
   {
       cout << ch;
       ch = fgetc(fout);
   }

   return 0;
}

这个接口的主要目的就是兼容C!

这个程序就把小雅兰今天在文件中写的代码全部读取到控制台啦!


find+substr

要取一个文件的后缀:

int main()
{
  string s1("Test.cpp");
  string s2("Test.zip");
 
  size_t pos1 = s1.find('.');
  if (pos1 != string::npos)
  {
    string suff = s1.substr(pos1, s1.size() - pos1);
    //string suff = s1.substr(pos1);
    cout << suff << endl;
  }
 
  size_t pos2 = s2.find('.');
  if (pos2 != string::npos)
  {
    string suff = s2.substr(pos2);
    cout << suff << endl;
  }
  return 0;
}

但是这样写有一点小问题:

int main()
{
   string s1("Test.cpp");
   string s2("Test.tar.zip");

   size_t pos1 = s1.find('.');
   if (pos1 != string::npos)
   {
       //string suff = s1.substr(pos1, s1.size() - pos1);
       string suff = s1.substr(pos1);
       cout << suff << endl;
   }

   size_t pos2 = s2.find('.');
   if (pos2 != string::npos)
   {
       string suff = s2.substr(pos2);
       cout << suff << endl;
   }
   return 0;
}

取到的不是真后缀!

int main()
{
  string s1("Test.cpp");
  string s2("Test.tar.zip");
 
  size_t pos1 = s1.rfind('.');
  if (pos1 != string::npos)
  {
    //string suff = s1.substr(pos1, s1.size() - pos1);
    string suff = s1.substr(pos1);
    cout << suff << endl;
  }
 
  size_t pos2 = s2.rfind('.');
  if (pos2 != string::npos)
  {
    string suff = s2.substr(pos2);
    cout << suff << endl;
  }
  return 0;
}

这样才是取到的正确的后缀!

接下来,来看一个更为复杂的场景:

给定一个网址,把它的三个部分分离!

string str("https://legacy.cplusplus.com/reference/string/string/substr/");
string sub1, sub2, sub3;
pos1 = str.find(':');
sub1 = str.substr(0, pos1 - 0);
cout << sub1 << endl;
 
pos2 = str.find('/', pos1 + 3);
sub2 = str.substr(pos1 + 3, pos2 - (pos1 + 3));
cout << sub2 << endl;
 
sub3 = str.substr(pos2 + 1);
cout << sub3 << endl;


find_first_of

#include<iostream>
#include<string>
using namespace std;

int main()
{
   std::string str("Please, replace the vowels in this sentence by asterisks.");
   std::size_t found = str.find_first_of("aeiou");
   while (found != std::string::npos)
   {
       str[found] = '*';
       found = str.find_first_of("aeiou", found + 1);
   }

   std::cout << str << '\n';

   return 0;
}


find_last_of

#include<iostream>
#include<string>
using namespace std;

void SplitFilename(const std::string& str)
{
   std::cout << "Splitting: " << str << '\n';
   std::size_t found = str.find_last_of("/\\");
   std::cout << " path: " << str.substr(0, found) << '\n';
   std::cout << " file: " << str.substr(found + 1) << '\n';
}

int main()
{
   std::string str1("/usr/bin/man");
   std::string str2("c:\\windows\\winhelp.exe");

   SplitFilename(str1);
   SplitFilename(str2);

   return 0;
}


find_first_not_of

#include<iostream>
#include<string>
using namespace std;

int main()
{
   std::string str("look for non-alphabetic characters...");

   std::size_t found = str.find_first_not_of("abcdefghijklmnopqrstuvwxyz ");

   if (found != std::string::npos)
   {
       std::cout << "The first non-alphabetic character is " << str[found];
       std::cout << " at position " << found << '\n';
   }

   return 0;
}


find_last_not_of

#include<iostream>
#include<string>
using namespace std;

int main()
{
   std::string str("Please, erase trailing white-spaces   \n");
   std::string whitespaces(" \t\f\v\n\r");

   std::size_t found = str.find_last_not_of(whitespaces);
   if (found != std::string::npos)
       str.erase(found + 1);
   else
       str.clear();            // str is all whitespace

   std::cout << '[' << str << "]\n";

   return 0;
}


+

int main()
{
  string s1(" hello ");
  string s2("world");
 
  string ret1 = s1 + s2;
  cout << ret1 << endl;
 
  string ret2 = s1 + "xx";
  cout << ret2 << endl;
 
  string ret3 = "xx" + s1;
  cout << ret3 << endl;
  return 0;
}


字符串最后一个单词的长度

但是这个题目有一个坑,就是:不管是scanf还是cin,默认是用空格或者换行去分割!

#include <iostream>
using namespace std;
#include<string>
int main() 
{
    string str;
    getline(cin,str);
    size_t pos=str.rfind(' ');
    if(pos!=string::npos)
    {
        cout<<str.size()-(pos+1)<<endl;
    }
    else 
    {
        cout<<str.size()<<endl;
    }
    return 0;
}



还要继续加油!!!


相关文章
|
2月前
|
编解码 Java 开发者
Java String类的关键方法总结
以上总结了Java `String` 类最常见和重要功能性方法。每种操作都对应着日常编程任务,并且理解每种操作如何影响及处理 `Strings` 对于任何使用 Java 的开发者来说都至关重要。
309 5
|
6月前
|
对象存储 C++ 容器
c++的string一键介绍
这篇文章旨在帮助读者回忆如何使用string,并提醒注意事项。它不是一篇详细的功能介绍,而是一篇润色文章。先展示重载函数,如果该函数一笔不可带过,就先展示英文原档(附带翻译),最后展示代码实现与举例可以直接去看英文文档,也可以看本篇文章,但是更建议去看英文原档。那么废话少说直接开始进行挨个介绍。
137 3
|
6月前
|
人工智能 机器人 编译器
c++模板初阶----函数模板与类模板
class 类模板名private://类内成员声明class Apublic:A(T val):a(val){}private:T a;return 0;运行结果:注意:类模板中的成员函数若是放在类外定义时,需要加模板参数列表。return 0;
178 0
|
6月前
|
存储 编译器 C语言
关于string的‘\0‘与string,vector构造特点,反迭代器与迭代器类等的讨论
你真的了解string的'\0'么?你知道创建一个string a("abcddddddddddddddddddddddddd", 16);这样的string对象要创建多少个对象么?你知道string与vector进行扩容时进行了怎么的操作么?你知道怎么求Vector 最大 最小值 索引 位置么?
164 0
|
6月前
|
存储 编译器 程序员
c++的类(附含explicit关键字,友元,内部类)
本文介绍了C++中类的核心概念与用法,涵盖封装、继承、多态三大特性。重点讲解了类的定义(`class`与`struct`)、访问限定符(`private`、`public`、`protected`)、类的作用域及成员函数的声明与定义分离。同时深入探讨了类的大小计算、`this`指针、默认成员函数(构造函数、析构函数、拷贝构造、赋值重载)以及运算符重载等内容。 文章还详细分析了`explicit`关键字的作用、静态成员(变量与函数)、友元(友元函数与友元类)的概念及其使用场景,并简要介绍了内部类的特性。
269 0
|
8月前
|
编译器 C++ 容器
【c++11】c++11新特性(上)(列表初始化、右值引用和移动语义、类的新默认成员函数、lambda表达式)
C++11为C++带来了革命性变化,引入了列表初始化、右值引用、移动语义、类的新默认成员函数和lambda表达式等特性。列表初始化统一了对象初始化方式,initializer_list简化了容器多元素初始化;右值引用和移动语义优化了资源管理,减少拷贝开销;类新增移动构造和移动赋值函数提升性能;lambda表达式提供匿名函数对象,增强代码简洁性和灵活性。这些特性共同推动了现代C++编程的发展,提升了开发效率与程序性能。
313 12
|
9月前
|
编译器 C++
类和对象(中 )C++
本文详细讲解了C++中的默认成员函数,包括构造函数、析构函数、拷贝构造函数、赋值运算符重载和取地址运算符重载等内容。重点分析了各函数的特点、使用场景及相互关系,如构造函数的主要任务是初始化对象,而非创建空间;析构函数用于清理资源;拷贝构造与赋值运算符的区别在于前者用于创建新对象,后者用于已存在的对象赋值。同时,文章还探讨了运算符重载的规则及其应用场景,并通过实例加深理解。最后强调,若类中存在资源管理,需显式定义拷贝构造和赋值运算符以避免浅拷贝问题。
|
9月前
|
编译器 C++
类和对象(下)C++
本内容主要讲解C++中的初始化列表、类型转换、静态成员、友元、内部类、匿名对象及对象拷贝时的编译器优化。初始化列表用于成员变量定义初始化,尤其对引用、const及无默认构造函数的类类型变量至关重要。类型转换中,`explicit`可禁用隐式转换。静态成员属类而非对象,受访问限定符约束。内部类是独立类,可增强封装性。匿名对象生命周期短,常用于临时场景。编译器会优化对象拷贝以提高效率。最后,鼓励大家通过重复练习提升技能!
|
10月前
|
编译器 C++ 开发者
【C++篇】深度解析类与对象(下)
在上一篇博客中,我们学习了C++的基础类与对象概念,包括类的定义、对象的使用和构造函数的作用。在这一篇,我们将深入探讨C++类的一些重要特性,如构造函数的高级用法、类型转换、static成员、友元、内部类、匿名对象,以及对象拷贝优化等。这些内容可以帮助你更好地理解和应用面向对象编程的核心理念,提升代码的健壮性、灵活性和可维护性。
|
9月前
|
设计模式 安全 C++
【C++进阶】特殊类设计 && 单例模式
通过对特殊类设计和单例模式的深入探讨,我们可以更好地设计和实现复杂的C++程序。特殊类设计提高了代码的安全性和可维护性,而单例模式则确保类的唯一实例性和全局访问性。理解并掌握这些高级设计技巧,对于提升C++编程水平至关重要。
182 16