std::string操作---compare、starts_with、ends_with

compare()
比较两个字符串

#include <string>
#include <iostream>
using namespace std;
int main(){
string{""};//string
string("abc");//char *
	int ret=string{"a"}.compare(string{"b"});//大于1,小于-1,等于0
	int ret2=string{"abc"}.compare(1,2,string{"bc"});//"abc"的[1,1+2)和”bc“比较
	int ret3=string{"abc"}.compare(0,1,string{"Abc"},1,1);//a<b
	return 0;
}

starts_with()
C++20
检查string是否始于给定前缀

#include <iostream>
#include <string>
using namespace std;
int main(){
	string s{"good hello,world"};
	if(s.starts_with('g'))
		cout << "success" << endl;;
	else
		cout << "defeat" << endl;
	s.starts_with("good");
	return 0;
}

ends_with()
c++20
检查string是否终于给定后缀

#include <iostream>
#include <string>
using namespace std;
int main(){
	string s{"good hello,world"};
	if(s.ends_with('d'))
		cout << "success" << endl;;
	else
		cout << "defeat" << endl;
	s.end_with("world");
	return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
std::condition_variable is a synchronization primitive provided by the C++ Standard Library that enables threads to wait for a certain condition to become true. It is typically used in conjunction with a std::mutex to protect access to shared data. The basic idea is that one or more threads wait on the condition variable until some other thread signals it. The waiting threads are blocked until the condition variable is notified, at which point one or more of them are unblocked and can proceed. To use std::condition_variable, you typically follow these steps: 1. Define a std::condition_variable object. 2. Define a std::mutex object to protect access to shared data. 3. Define a boolean variable that represents the condition that the threads are waiting for. 4. Use the mutex to protect access to the shared data and the condition variable. 5. When a thread needs to wait for the condition to become true, it calls wait() on the condition variable while holding the mutex. 6. When the condition becomes true, some other thread calls notify_one() or notify_all() on the condition variable to wake up the waiting thread(s). Here's some example code: ``` #include <mutex> #include <condition_variable> #include <thread> #include <iostream> std::mutex mtx; std::condition_variable cv; bool ready = false; void worker() { std::unique_lock<std::mutex> lock(mtx); while (!ready) { cv.wait(lock); } std::cout << "Worker thread got signal!\n"; } int main() { std::thread t(worker); // Do some work here... { std::lock_guard<std::mutex> lock(mtx); ready = true; } cv.notify_one(); t.join(); return 0; } ``` In this example, the main thread starts a worker thread and does some work. When the work is done, it signals the worker thread by setting the "ready" flag to true and calling notify_one() on the condition variable. The worker thread is blocked on the condition variable until it receives the signal, at which point it prints out a message.

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值