STL记录

字符串相关

  • sscanf(log.c_str(), "%d:%[^:]:%d", &idx, type, &time);//string.c_str():将string转成一个字符串指针,%[a-z]:表示尽可能多的匹配a-z中的任意字符, %[^a-z]:表示匹配到a-z中的任意字符停止匹配。a-z可以替换成任意字符:a-z1-9

  • isalnum(char ):判断字符是否为字母或者数字,返回0/1

  • tolower(char ):将大写字母转换成小写字母,其余字符不做改变,返回小写字母或原字符

  • stringstream:字符串流 cpp手册

      stringstream sstream;
      string s;
      // 将多个字符串放入 sstream 中
      sstream << "first" << " " << "string,";
      sstream << " second string";
      cout << "strResult is: " << sstream.str() << endl;// "first string, second string"
      sstream >> s;
      cout << s << endl;// "first"
    
  • find_first_of:cpp手册

      #include <cassert>
      #include <iostream>
      #include <string>
      #include <string_view>
    
      int main() {
      	using namespace std::string_literals;
      	std::string::size_type sz;
    
      	// (1)
      	sz = "alignas"s.find_first_of("klmn"s);
      	//     └────────────────────────┘
      	assert(sz == 1);
      	sz = "alignof"s.find_first_of("wxyz"s);
      	//
      	assert(sz == std::string::npos);
    
      	// (2)
      	const char* buf = "xyzabc";
      	sz = "consteval"s.find_first_of(buf, 0, 3);//0是buf的检索起点,3是buf的检索终点
      	//
      	assert(sz == std::string::npos);
      	sz = "consteval"s.find_first_of(buf, 3, 6);//3是buf的检索起点,6是buf的检索终点
      	//    └─────────────────────────┘c in buf
      	assert(sz == 0);
    
      	// (3)
      	sz = "decltype"s.find_first_of(buf);
      	//      └──────────────────────┘c in buf
      	assert(sz == 2);
    
      	// (4)
      	sz = "co_await"s.find_first_of('a');
      	//       └──────────────────────┘
      	assert(sz == 3);
    
      	// (5)
      	std::string_view sv{"int"};
      	sz = "constinit"s.find_first_of(sv);
      	//      └───────────────────────┘n in sv
      	assert(sz == 2);
    
      	std::cout << "All tests passed.\n";
      }
    
  • find_first_not_of:找到“不是”的字符,用法类似上

  • find_last_of:找到“最后是”的字符,用法类似上

  • find_last_not_of:找到“最后不是”的字符,用法类似上

STL容器

  • vector.emplace_back():直接在 vector 尾部创建这个元素,省去了移动或者拷贝元素的过程。
posted @ 2023-04-11 17:08  bothgone  阅读(19)  评论(0)    收藏  举报