std::string s = "hello, world!"; size_t pos = s.find_last_of("ow");

时间: 2024-04-01 16:37:11 浏览: 14
在这个例子中,`s` 是一个字符串,值为 `"hello, world!"`。`pos` 是一个 `size_t` 类型的变量,用于存储查找结果。 `find_last_of()` 函数查找字符串 `s` 中最后一个出现在字符集合 `"ow"` 中的字符,并返回其位置。 在这个例子中,字符集合 `"ow"` 包含字符 `'o'` 和 `'w'`。因此,函数将从字符串 `s` 的末尾开始向前查找,在位置 11 找到字符 `'o'`,并将其位置返回给变量 `pos`。 因此,执行完这段代码后,变量 `pos` 的值为 11。
相关问题

find_first_of和find_last_of及相关函数并举例

除了 `find_first_of`,`std::string` 类中还有一些其他的查找函数,它们的作用都是在字符串中查找指定字符或字符集的位置。下面是几个常用的查找函数及其功能: 1. `find(char ch, size_t pos = 0) const`:在字符串中查找字符 `ch` 的位置,从 `pos` 位置开始查找,默认值为 0。 ```cpp std::string s = "hello, world!"; size_t pos = s.find('o'); // 返回 4 ``` 2. `rfind(char ch, size_t pos = npos) const`:在字符串中查找字符 `ch` 的位置,从 `pos` 位置往前查找,`npos` 表示从字符串的末尾开始查找。 ```cpp std::string s = "hello, world!"; size_t pos = s.rfind('o'); // 返回 8 ``` 3. `find_first_of(const char* str, size_t pos = 0) const`:在字符串中查找第一个匹配指定字符集中任意一个字符的位置。 ```cpp std::string s = "hello, world!"; size_t pos = s.find_first_of("ow"); // 返回 4 ``` 4. `find_last_of(const char* str, size_t pos = npos) const`:在字符串中查找最后一个匹配指定字符集中任意一个字符的位置。 ```cpp std::string s = "hello, world!"; size_t pos = s.find_last_of("ow"); // 返回 9 ``` 5. `find_first_not_of(const char* str, size_t pos = 0) const`:在字符串中查找第一个不匹配指定字符集中任意一个字符的位置。 ```cpp std::string s = "hello, world!"; size_t pos = s.find_first_not_of("helo, "); // 返回 5 ``` 6. `find_last_not_of(const char* str, size_t pos = npos) const`:在字符串中查找最后一个不匹配指定字符集中任意一个字符的位置。 ```cpp std::string s = "hello, world!"; size_t pos = s.find_last_not_of("dlrow!"); // 返回 10 ```

find_first_of

`find_first_of` 是 C++ 中 `std::string` 类的成员函数之一,用于在字符串中查找第一个匹配指定字符集中任意一个字符的位置。 该函数有两个重载版本: 1. `size_t find_first_of(const char* str, size_t pos = 0) const;` 2. `size_t find_first_of(const string& str, size_t pos = 0) const;` 第一个版本接受一个 C 风格字符串 `str` 作为参数,第二个版本接受一个 `std::string` 对象 `str` 作为参数,两个版本的第二个参数 `pos` 都是可选的,表示从字符串的哪个位置开始查找,默认值为 0。 这个函数会在字符串中从 `pos` 位置开始往后查找,找到第一个匹配指定字符集中任意一个字符的位置并返回该位置。如果字符串中没有匹配的字符,则返回 `string::npos`。 例如,对于字符串 `"hello, world!"`,调用 `find_first_of("ow")` 会返回 4,表示该字符串中第一个匹配字符集 `"ow"` 中任意一个字符的位置是在第 4 个字符处,也就是字符 `'o'`。

相关推荐

#include<reg51.h> #include<intrins.h> #define dm P0 #define uchar unsigned char #define uint unsigned int sbit DQ=P1^6; sbit w0=P2^0; sbit w1=P2^1; sbit w2=P2^2; sbit w3=P2^3; sbit beep=P3^7; int temp1=0; uint h; uint temp; uchar r; uchar code ditab[16]={0x00,0x01,0x01,0x02,0x03,0x03,0x04,0x04,0x05,0x06,0x06,0x07,0x08,0x08,0x09,0x09}; uchar code table_dm[12]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f,0x00,0x40}; uchar code table_dml[]={0xbf,0x86,0xdb,0xcf,0xe6,0xed,0xfd,0x87,0xff,0xef}; uchar data temp_data[2]={0x00,0x00}; uchar data display[5]={0x00,0x00,0x00,0x00,0x00}; void delay(uint t) { for(;t>0;t--); } void xianshi() { int j; for(j=0;j<4;j++) { switch(j) { case 0: dm=table_dm[display[0]]; w0=0; delay(300); w0=1; case 1: dm=table_dml[display[1]]; w1=0; delay(300); w1=1; case 2: dm=table_dm[display[2]]; w2=0; delay(300); w2=1; case 3: dm=table_dm[display[3]]; w3=0; delay(300); w3=1; } } } ow_reset(void) { char presence=1; while(presence) { while(presence) { DQ=1;_nop_();_nop_(); DQ=0; delay(50); DQ=1; delay(6); presence=~DQ; } delay(45); presence=~DQ; } DQ=1; return presence; } void write_byte(uchar val) { uchar i; for(i=8;i>0;i--) { DQ=1;_nop_();_nop_(); DQ=0;_nop_();_nop_();_nop_();_nop_(); DQ=val&0x01; delay(6); val=val>>1; } DQ=1; delay(1); } uchar read_byte(void) { uchar i; uchar value=0; for(i=8;i>0;i--) { DQ=1;_nop_();_nop_(); value>>=1; DQ=0;_nop_();_nop_();_nop_();_nop_(); DQ=1;_nop_();_nop_();_nop_();_nop_(); if(DQ)value|=0x80; delay(6); } DQ=1; return value; } read_temp() { ow_reset(); delay(200); write_byte(0xcc); write_byte(0x44); ow_reset(); delay(1); write_byte(0xcc); write_byte(0xbe); temp_data[0]=read_byte(); temp_data[1]=read_byte(); temp=temp_data[1]; temp<<=8; temp=temp|temp_data[0]; return temp; } work_temp(uint tem) { uchar n=0; if(tem>6348) { tem=65536-tem; n=1; } display[4]=tem&0x0f; display[0]=ditab[display[4]]; display[4]=tem>>4; display[3]=display[4]/100; display[1]=display[4]%100; display[2]=display[1]/10; display[1]=display[1]%10; r=display[1]+display[2]*10+display[3]*100; if(!display[3]) { display[3]=0x0a; if(!display[2]) { display[2]=0x0a; } } if(n) { display[3]=0x0b; } return n; } void BEEP() { if((r>30)) { beep=!beep; } else { beep=0; } } void main() { beep=0; dm=0x00; w0=0; w1=0; w2=0; w3=0; for(h=0;h<4;h++) { display[h]=0; } ow_reset(); write_byte(0xcc); write_byte(0x44); for(h=0;h<50;h++) { xianshi(); } while(1) { if(temp1==0) { work_temp(read_temp()); xianshi(); BEEP(); } } }

最新推荐

recommend-type

深入探讨:oracle中row_number() over()分析函数用法

本篇文章是对oracle中row_number() over()分析函数的用法进行了详细的分析介绍,需要的朋友参考下
recommend-type

深入理解用mysql_fetch_row()以数组的形式返回查询结果

同mysql_result()一样,mysql_fetch_row()也可以用来获取查询结果集,其区别在于函数的返回值不是一个字符串,而是一个数组。函数定义如下。复制代码 代码如下:array mysql_fetch_row(int result) 参数说明如下。...
recommend-type

tensorflow 2.0模式下训练的模型转成 tf1.x 版本的pb模型实例

升级到tf 2.0后, 训练的模型想转成1.x版本的.pb模型, 但之前提供的通过ckpt转pb模型的方法都不可用(因为保存的ckpt不再有.meta)文件, 尝试了好久, 终于找到了一个方法可以迂回转到1.x版本的pb模型. ...
recommend-type

15个区块链共识算法的开源实现【BFT/Raft/Paxos/Pow/PoS】

共识算法是实现自主产权区块链的必不可少的关键环节,本文列出社区中相对成熟的区块链共识算法开源实现,包括BFT共识、Raft共识、Paxos共识、PoW共识等,可供希望开发自主产权区块链的团队参考学习。...
recommend-type

JavaScript中windows.open()、windows.close()方法详解

windows.open()方法详解: window.open(URL,name,features,replace)用于载入指定的URL到新的或已存在的窗口中,并返回代表新窗口的Window对象。它有4个可选的 参数: URL:一个可选的字符串,声明了要在新窗口中...
recommend-type

zigbee-cluster-library-specification

最新的zigbee-cluster-library-specification说明文档。
recommend-type

管理建模和仿真的文件

管理Boualem Benatallah引用此版本:布阿利姆·贝纳塔拉。管理建模和仿真。约瑟夫-傅立叶大学-格勒诺布尔第一大学,1996年。法语。NNT:电话:00345357HAL ID:电话:00345357https://theses.hal.science/tel-003453572008年12月9日提交HAL是一个多学科的开放存取档案馆,用于存放和传播科学研究论文,无论它们是否被公开。论文可以来自法国或国外的教学和研究机构,也可以来自公共或私人研究中心。L’archive ouverte pluridisciplinaire
recommend-type

【实战演练】MATLAB用遗传算法改进粒子群GA-PSO算法

![MATLAB智能算法合集](https://static.fuxi.netease.com/fuxi-official/web/20221101/83f465753fd49c41536a5640367d4340.jpg) # 2.1 遗传算法的原理和实现 遗传算法(GA)是一种受生物进化过程启发的优化算法。它通过模拟自然选择和遗传机制来搜索最优解。 **2.1.1 遗传算法的编码和解码** 编码是将问题空间中的解表示为二进制字符串或其他数据结构的过程。解码是将编码的解转换为问题空间中的实际解的过程。常见的编码方法包括二进制编码、实数编码和树形编码。 **2.1.2 遗传算法的交叉和
recommend-type

openstack的20种接口有哪些

以下是OpenStack的20种API接口: 1. Identity (Keystone) API 2. Compute (Nova) API 3. Networking (Neutron) API 4. Block Storage (Cinder) API 5. Object Storage (Swift) API 6. Image (Glance) API 7. Telemetry (Ceilometer) API 8. Orchestration (Heat) API 9. Database (Trove) API 10. Bare Metal (Ironic) API 11. DNS
recommend-type

JSBSim Reference Manual

JSBSim参考手册,其中包含JSBSim简介,JSBSim配置文件xml的编写语法,编程手册以及一些应用实例等。其中有部分内容还没有写完,估计有生之年很难看到完整版了,但是内容还是很有参考价值的。