cpp中std::string容器的一些性质

gnahz Lv1

力扣上刷到一条动态规划解决字符串匹配的题目

对cpp的stl容器的std::string的一些性质进行记录

1.字符串可以相加

1
2
3
4
string word1="hello";
srting word2="world";
string word3 = word1+word2;//这个是合法的
//word3 == "helloworld"

2.find()

std::string容器自带find方法可以返回第一个出现的子串的下标

3.std::string::npos

定义:
  极大值,表示无效位置/没有找到
  数值上通常使用-1(即64二进制平台上的最大值18446744073709551615)
用途:
  find()函数查找字符串中表示没有找到的返回值
注意:
  在没有找到的情况下想要表示“如果找到”的时候不能使用!pos来表示
  因为找不到情况下pos==std::npos==-1!=0,而只有0才能用!pos来判断
  应该使用if(pos!=std::npos)

image-1

  • Title: cpp中std::string容器的一些性质
  • Author: gnahz
  • Created at : 2025-09-10 13:24:10
  • Updated at : 2025-09-10 22:12:07
  • Link: https://redefine.ohevan.com/2025/09/10/cpp中std-string容器的一些性质/
  • License: This work is licensed under CC BY-NC-SA 4.0.
Comments