【C++】 —— string的使用(二)

简介: 【C++】 —— string的使用

【C++】 —— string的使用(一)https://developer.aliyun.com/article/1621427

1、operator+=   *

       库里面实现了三个+=运算符重载函数,我们在使用时就可以像内置类型那样+=;

这里可以+=一个string类类型对象、字符串(字符数组)或者一个字符。

void test4()
{
  string s1("hello ");
  string s2("world ");
  s1 += s2;
  cout << s1 << endl;
  
  char str[] = "ever";
  s1 += str;
  cout << s1 << endl;
 
  s1 += 'o';
  s1 += 'n';
  s1 += 'e';
  cout << s1 << endl;
}

2、append

       库里面实现的append感觉有点冗余;

       append可以增加string类型对象(也可以是其中的一部分)、字符串(可以是其中的一部分)、n个字符;传参也可以是迭代器。

void test5()
{
  string s1("hello ");
  string s2("world ");
  //s1.append(s2); 也可以直接添加s2的全部
  s1.append(s2, 1, 3);
  //从下标为1的位置开始,添加后面3个字符
  cout << s1 << endl;
 
  s1.append("everone");
  //s1.append("everone",4);
  //也可以这样,只添加前4个字符
  cout << s1 << endl;
 
  s1.append(3, 'x');
  //添加3个字符 'x'
  cout << s1 << endl;
}

3、push_back

push_back就是在string对象后面添加一个字符。(比较简单就不测试了)。

4、assgin

       assgin是设置string类对象的内容,(可以用同类型对象来设置(也可以是一部分)、也可以用字符串设置(或者其中一部分)、也可以将strng对象内容设置成n个字符;(也可以传迭代器))。

void test5()
{
  string s1("hello ");
  string s2("world ");
  //s1.append(s2); 
  s1.assign(s2, 1, 3);
  cout << s1 << endl;
 
  s1.assign("everone");
  //s1.append("everone",4);
  cout << s1 << endl;
 
  s1.assign(3, 'x');
  cout << s1 << endl;
}

5、insert

       insert与assgin不同,要比assgin多一个参数,这个参数pos就决定了从string对象中字符串哪个位置开始设置字符串的内容。

void test5()
{
  string s1("hello ");
  string s2("world ");
  //s1.append(s2); 
  s1.insert(3, s2, 1, 3);
  cout << s1 << endl;
 
  s1.insert(8, "everone");
  //s1.append("everone",4);
  cout << s1 << endl;
 
  s1.insert(15, 3, 'x');
  cout << s1 << endl;
}

6、erase

       erase是删除string对象中字符串的数据,(删除pos位置后面的len和字符);如果不传参就是全部删除(如果只是不传len的参数,就默认删除pos后面所有的数据)。

void test6()
{
  string s1("hello world !!!");
  cout << s1 << endl;
  s1.erase(11, 3);
  cout << s1 << endl;
  s1.erase(5);
  cout << s1 << endl;
  s1.erase();
  cout << s1 << endl;
}

7、replace

replace 替换string对象字符串中的数据;

函数的前两个参数,就是指需要替换的位置(pos位置后面的len个字符)。

       可以替换成string类类型对象(也可以是一部分)、字符串(也可以是一部分)、n个字符或者迭代器指向的区间。

void test7()
{
  string s1("Hello World ");
  string s2("everone");
  cout << s1 << endl;
  s1.replace(6, 5, s2);
  cout << s1 << endl;
 
  s1.replace(6, 5, "I Love");
  cout << s1 << endl;
 
  s1.replace(6, 5, 3, '*');
  cout << s1 << endl;
}

8、swap

       swap是库里面实现的一个函数(交换两个string类类型对象)。

void test8()
{
  string s1("hello world");
  string s2("I Love you");
  cout << "s1: " << s1 << endl;
  cout << "s2: " << s2 << endl;
  s1.swap(s2);
  cout << "s1: " << s1 << endl;
  cout << "s2: " << s2 << endl;
}

       3.4、String operations 字符串操作

       字符串操作,主要操作有返回字符串指针、查找、拷贝、获得子串等。(这里就使用其中的一部分)。

1、c_str   *

       c_str返回string对象里的字符串指针。(需要注意的就是返回的指针是const修饰的,必须用const指针接受)。

void test9()
{
  string s1("Hello World");
  const char* s = s1.c_str();
  cout << s << endl;
}

2、find   *

       find查找, 在string对象里查找指定字符(string类类型对象、字符串、字符),也可以指定区间进行查找。找到就返回下标;没有找到返回npos。

void test9()
{
  string s1("Hello World");
  string s2("Hello");
  size_t f1 = s1.find(s2);
  size_t f2 = s1.find("Love");
  size_t f3 = s1.find('o');
 
  cout << f1 << endl;
  cout << f2 << endl;
  cout << f3 << endl;
}

3、substr   *

       substr获得string对象字符串中的子串。这结合上面的find,

实现将一个网址的协议、域名以及资源分开。

void test10()
{
  string s1("https://blog.csdn.net/LH__1314?type=blog");
  size_t f1 = s1.find("://", 0);
  string protocol = s1.substr(0, f1);
 
  size_t f2, f3;
  f2 = s1.find('/', f1 + 3);
  string domain = s1.substr(f1 + 3, f2 - (f1 + 3));
  string resource = s1.substr(f2 + 1);
 
  cout << protocol.c_str() << endl;
  cout << domain.c_str() << endl;
  cout << resource.c_str() << endl;
}

       3.5、Inerator

       inerator就是所谓的迭代器,在string中迭代器作用没有那么大,(甚至可以将它理解为指针(但迭代器不是指针))。

在使用这些函数之前,要先知道一种类型,迭代器 iterator

string::iterator it;

因为在其他容器里也存在迭代器,所以定义时要指定类域。

1、begin、end

       这里的两个函数返回的都是迭代器,begin返回的是起始位置,end返回的是终止位置。

       这里库里面还实现了const修饰的函数,const修饰的迭代器类型有所不同

string::const_iterator it;

这里直接使用:

void test11()
{
  string s1("I Love You");
  string::iterator it = s1.begin();
  while (it != s1.end())
  {
    cout << *it << " ";
    it++;
  }
  cout << endl;
}

2、范围for

有了迭代器,我们就可以实现一种语法 范围for

void test11()
{
  string s1("I Love You");
  for (auto ch : s1)
  {
    cout << ch << " ";
  }
}

到这里,string类的基本使用就结束了。

感谢各位大佬支持并指出问题,

                       如果本篇内容对你有帮助,可以一键三连支持以下,感谢支持!!!



相关文章
|
5天前
|
C++ 容器
|
5天前
|
C++ 容器
|
5天前
|
存储 C++ 容器
|
9天前
|
安全 C语言 C++
【C++篇】探寻C++ STL之美:从string类的基础到高级操作的全面解析
【C++篇】探寻C++ STL之美:从string类的基础到高级操作的全面解析
27 4
|
9天前
|
存储 编译器 程序员
【C++篇】手撕 C++ string 类:从零实现到深入剖析的模拟之路
【C++篇】手撕 C++ string 类:从零实现到深入剖析的模拟之路
41 2
|
10天前
|
编译器 C语言 C++
【C++】C++ STL 探索:String的使用与理解(三)
【C++】C++ STL 探索:String的使用与理解
|
10天前
|
存储 编译器 C++
【C++】C++ STL 探索:String的使用与理解(二)
【C++】C++ STL 探索:String的使用与理解
|
10天前
|
编译器 C语言 C++
【C++】C++ STL 探索:String的使用与理解(一)
【C++】C++ STL 探索:String的使用与理解
|
1天前
|
C语言 C++
深度剖析C++string(中)
深度剖析C++string(中)
5 0
|
1天前
|
存储 编译器 程序员
深度剖析C++string(上篇)(2)
深度剖析C++string(上篇)(2)
5 0