C++ 中string.find() 函数的用法总结[转]

string类型find()函数的运用 P1308 统计单词数 string类型find函数的运用与理解。 #include <cstdio> #include <iostream> #include <cstring> #include <algorithm> #include <stack> #include <set> #include <stri... 阅读详情

转载地址:http://blog.csdn.net/yushuai007008/article/details/7051385

 

#include <string>
#include <iostream>
using namespace std;

void main()
{

  1.  ////find函数返回类型 size_type   
  2. string s("1a2b3c4d5e6f7g8h9i1a2b3c4d5e6f7g8ha9i");  
  3. string flag;  
  4. string::size_type position;  
  5.   
  6. //find 函数 返回jk 在s 中的下标位置    
  7. position = s.find("jk");  
  8.  if (position != s.npos)  //如果没找到,返回一个特别的标志c++中用npos表示,我这里npos取值是4294967295,   
  9.  {  
  10.   cout << "position is : " << position << endl;  
  11.  }  
  12.  else  
  13.  {  
  14.   cout << "Not found the flag" + flag;  
  15.  }   
 ////find函数返回类型 size_type
string s("1a2b3c4d5e6f7g8h9i1a2b3c4d5e6f7g8ha9i");
string flag;
string::size_type position;

//find 函数 返回jk 在s 中的下标位置 
position = s.find("jk");
 if (position != s.npos)  //如果没找到,返回一个特别的标志c++中用npos表示,我这里npos取值是4294967295,
 {
  cout << "position is : " << position << endl;
 }
 else
 {
  cout << "Not found the flag" + flag;
 } 
  1. //find 函数 返回flag 中任意字符 在s 中第一次出现的下标位置   
  2.  flag = "c";  
  3.  position = s.find_first_of(flag);  
  4.  cout << "s.find_first_of(flag) is : " << position << endl;  
//find 函数 返回flag 中任意字符 在s 中第一次出现的下标位置
 flag = "c";
 position = s.find_first_of(flag);
 cout << "s.find_first_of(flag) is : " << position << endl;

  1. //从字符串s 下标5开始,查找字符串b ,返回b 在s 中的下标   
  2. position=s.find("b",5);  
  3. cout<<"s.find(b,5) is : "<<position<<endl;  
 //从字符串s 下标5开始,查找字符串b ,返回b 在s 中的下标
 position=s.find("b",5);
 cout<<"s.find(b,5) is : "<<position<<endl;

 

  1. //查找s 中flag 出现的所有位置。   
  2.  flag="a";  
  3.  position=0;  
  4.  int i=1;  
  5.  while((position=s.find_first_of(flag,position))!=string::npos)  
  6.  {  
  7.   //position=s.find_first_of(flag,position);   
  8.   cout<<"position  "<<i<<" : "<<position<<endl;  
  9.   position++;  
  10.   i++;  
  11.  }  
//查找s 中flag 出现的所有位置。
 flag="a";
 position=0;
 int i=1;
 while((position=s.find_first_of(flag,position))!=string::npos)
 {
  //position=s.find_first_of(flag,position);
  cout<<"position  "<<i<<" : "<<position<<endl;
  position++;
  i++;
 }
  1.    
 
  1. //查找flag 中与s 第一个不匹配的位置   
  2. flag="acb12389efgxyz789";  
  3. position=flag.find_first_not_of (s);  
  4. cout<<"flag.find_first_not_of (s) :"<<position<<endl;  
 //查找flag 中与s 第一个不匹配的位置
 flag="acb12389efgxyz789";
 position=flag.find_first_not_of (s);
 cout<<"flag.find_first_not_of (s) :"<<position<<endl;
  1.    
 
  1.  //反向查找,flag 在s 中最后出现的位置   
  2.  flag="3";  
  3.  position=s.rfind (flag);  
  4.  cout<<"s.rfind (flag) :"<<position<<endl;  
  5. }  
 //反向查找,flag 在s 中最后出现的位置
 flag="3";
 position=s.rfind (flag);
 cout<<"s.rfind (flag) :"<<position<<endl;
}


 说明:

1.  如果string sub = ”abc“;

              string s = ”cdeabcigld“;

     s.find(sub) , s.rfind(sub) 这两个函数,如果完全匹配,才返回匹配的索引,即:当s中含有abc三个连续的字母时,才返回当前索引。

     s.find_first_of(sub),   s.find_first_not_of(sub),   s.find_last_of(sub),  s.find_last_not_of(sub)  这四个函数,查找s中含有sub中任意字母的索引。

2.  如果没有查询到,则返回string::npos,这是一个很大的数,其值不需要知道。

 

C语言string中的find()函数 在c++中,字符串类型string后面加上.find()的作用,是 string str1, str2; int i; string str1 = "qwertyuuoesdi"; string str2="uu"; char c = 'q'; i = str1.find(str2);//从串str1中查找时str2,返回str2中首个字符在str1中的地址 printf("%d\n",i); i = str1.find(str2,5);//从str1的第5个字符开始查找str2 prin 阅读详情

相关推荐

京东联盟API实战:5分钟搞定商品返利链接生成(附Python代码)

本文详细介绍了如何利用京东联盟API快速构建自动化商品返利链接生成系统。通过Python代码实战,从API申请、环境配置到核心功能封装,手把手教你实现批量商品信息查询与推广链接生成,帮助内容创作者和开发者大幅提升电商推广效率。

weixin_29216957的博客 686

c++---find()函数

c++的find函数简介,示例

qq_43700779的博客 1万+

射频电源与等离子体技术:原理、应用与优化策略

射频电源(RF Power Supply)作为高频交流电信号发生器,在半导体制造和材料处理中扮演关键角色。其核心原理是通过高频电场激发气体产生等离子体,这种电离气体包含电子、离子和活性基团,具有独特的非平衡特性。技术价值体现在等离子体密度和电子温度的可控性上,使得在刻蚀和沉积工艺中能精确调控材料处理效果。应用场景广泛覆盖PECVD、深槽刻蚀等工业流程,其中13.56MHz ISM频段因其全球合规性成为行业标准。优化策略包括双频驱动和阻抗匹配网络设计,可显著提升工艺均匀性和设备效率。

weixin_29163797的博客 410

C++ string中的find()函数

1.string中find()返回值是字母在母串中的位置(下标记录),如果没有找到,那么会返回一个特别的标记npos。(返回值可以看成是一个int型的数) #include&lt;cstring&gt; #include&lt;cstdio&gt; #include&lt;iostream&gt; using namespace std; int main() { ////find函数返回...

Lavi的专栏 2万+

linux redis7 完整的集群搭建过程

一。安装: 1.1 说明: Redis官方只提供了源码,并没有提供经过编译之后的安装包。 因此,安装Redis,要先编译、后安装。(即源码安装方式) 注意:需要联网安装 1.2 安装编译的工具: yum install gcc gcc-c++ -y 1.3 下载tar包,上传到linux服务器,解压配置环境变量 1.4 cd +解压的目录下:编译 make 注意:如果编译失败,请重新进行解压编译,...

m0_38028438的博客 732

手把手教你用S7-PLCSIM Advanced V3.0搭建PLC仿真测试环境(含WinPcap避坑指南)

本文详细介绍了如何使用西门子S7-PLCSIM Advanced V3.0搭建PLC仿真测试环境,包括WinPcap 4.1.3驱动的安装避坑指南。从环境准备、软件安装到配置调试,提供了一套完整的解决方案,帮助工程师和教学人员快速构建可靠的PLC仿真环境,解决常见的报错问题。

weixin_30879169的博客 656

string.find()函数(C/C++)

string.find()函数:分析总结+代码应用

psudd的博客 5085

win11重装系统简易教程-小白也可以上手!

本文介绍一下win11系统重装的各个步骤,也作为记录供自己后面查看。附上我之前参考的一位知乎大神写的重装win10系统文章的地址本文用到的工具全部是,不需要下载第三方镜像文件(不是说这个不好,只是第三方的软件或镜像文件网上非常多,琳琅满目,小白需要筛选甄别,直接用官方的就比较省心,大神请自便~)

Sddqa的博客 1万+

C++String中的find用法

#include<string> string 是c++中一个非常重要函数。 在处理字符串的时候经常用到。 find是string中一个查找函数。 find用法: 1.find() 示例:(上代码) #include<iostream> #include<string> using namespace std; int main() { s...

帅帅气气的黑猫警长 8万+

深入解析EDID中Display Descriptor Block的配置技巧与实战应用

本文深入解析EDID中Display Descriptor Block的配置技巧与实战应用。通过剖析像素时钟、有效像素、消隐区与同步脉冲等核心参数,并结合1280x720@60Hz的实战配置案例,指导开发者如何正确为定制硬件或非标准显示器编写“身份证”,避免黑屏、分辨率异常等问题,确保显示设备完美点亮。

1a2s3d4f5g的博客 889

TXS0104EPWR 4通道电平转换模块原理图设计,已量产

在嵌入式系统设计中,不同模块间的电平不兼容是常见问题 —— 比如 1.8V 的 MCU 与 3.3V 的传感器通信,或是 3.3V 的外设与 5V 的主控交互。这类场景下,一款高效、低功耗的双向电平转换器就成了关键组件。本文将结合实际原理图,深度解析 TI TXS0104 这款 4 通道双向电平转换器的设计要点与应用实践。

不脱发的程序猿 514

C++ string中的find() 函数

1、string中find()返回值是字母在母串中的位置(下标记录),如果没有找到,那么会返回一个特别的标记npos,也就是-1。(返回值可以看成是一个int型的数) 例子:找的情况 #include<iostream> #include<string> using namespace std; int main() { string str = "aabc"; int pos = str.find("abc"); if (pos != str.n

gentleman_97的博客 2万+

第229篇 轨迹时间参数化——TOPP-RA和速度约束处理

本文介绍了时间最优路径参数化(TOPP)问题及其解法。TOPP研究如何在给定空间路径上分配时间,使机器人运动时间最短且不超速,需满足速度、加速度和力矩约束。经典解法采用相平面法,通过前向和后向扫描确定最优速度曲线。TOPP-RA改进了数值稳定性,支持多约束处理。文章对比了TOPP与轨迹优化的区别,列举了开源工具,并解答了面试常见问题,指出TOPP广泛应用于机械臂、AGV等领域。TOPP不改变路径形状,仅优化速度分配,其最优解具有bang-bang特性。

郑伟强dev的专栏 337

string类的find()函数总结

string类的头文件提供了很多搜索相关的函数比如find()函数及其变体。这使得我们可以以多种不同的方式在字符串中搜索给定的子字符串或字符。但是对于初学者来讲,经常被这些长相类似的函数所混淆。 下面总结了string类的find相关函数:1、find():find函数有四种变体: 方法原型 描述 size_type find(const string & str, size_type

_Previous的博客 6万+

VB摄像头监控

该VB模块可对摄像头捕获的画面进行录像等,可在创建的窗口进行控制

深度学习工具包 DeepLearningToolbox

深度学习工具包Deprecation notice.-----This toolbox is outdated and no longer maintained.There are much better tools available for deep learning than this toolbox, e.g. [Theano](http://deeplearning.net/software/theano/), [torch](http://torch.ch/) or [tensorflow](http://www.tensorflow.org/)I would suggest you use one of the tools mentioned above rather than use this toolbox.Best, Rasmus.DeepLearnToolbox================A Matlab toolbox for Deep Learning.Deep Learning is a new subfield of machine learning that focuses on learning deep hierarchical models of data.It is inspired by the human brain's apparent deep (layered, hierarchical) architecture.A good overview of the theory of Deep Learning theory is[Learning Deep Architectures for AI](http://www.iro.umontreal.ca/~bengioy/papers/ftml_book.pdf)For a more informal introduction, see the following videos by Geoffrey Hinton and Andrew Ng.* [The Next Generation of Neural Networks](http://www.youtube.com/watch?v=AyzOUbkUf3M) (Hinton, 2007)* [Recent Developments in Deep Learning](http://www.youtube.com/watch?v=VdIURAu1-aU) (Hinton, 2010)* [Unsupervised Feature Learning and Deep Learning](http://www.youtube.com/watch?v=ZmNOAtZIgIk) (Ng, 2011)If you use this toolbox in your research please cite [Prediction as a candidate for learning deep hierarchical models of data](http://www2.imm.dtu.dk/pubdb/views/publication_details.php?id=6284)```@MASTERSTHESIS\{IMM2012-06284, author = "R. B. Palm", title = "Prediction as a candidate for learning deep hierarchical models of data", year = "2012",}```Contact: rasmusbergpalm at gmail dot comDirectories included in the toolbox-----------------------------------`NN/` - A library for Feedforward Backpropagation Neural Networks`CNN/` - A library for Convolutional Neural Networks`DBN/` - A library for Deep Belief Networks`SAE/` - A library for Stacked Auto-Encoders`CAE/` - A library for Convolutional Auto-Encoders`util/` - Utility functions used by the libraries`data/` - Data used by the examples`tests/` - unit tests to verify toolbox is workingFor references on each library check REFS.mdSetup-----1. Download.2. addpath(genpath('DeepLearnToolbox'));Example: Deep Belief Network---------------------```matlabfunction test_example_DBNload mnist_uint8;train_x = double(train_x) / 255;test_x = double(test_x) / 255;train_y = double(train_y);test_y = double(test_y);%% ex1 train a 100 hidden unit RBM and visualize its weightsrand('state',0)dbn.sizes = [100];opts.numepochs = 1;opts.batchsize = 100;opts.momentum = 0;opts.alpha = 1;dbn = dbnsetup(dbn, train_x, opts);dbn = dbntrain(dbn, train_x, opts);figure; visualize(dbn.rbm{1}.W'); % Visualize the RBM weights%% ex2 train a 100-100 hidden unit DBN and use its weights to initialize a NNrand('state',0)%train dbndbn.sizes = [100 100];opts.numepochs = 1;opts.batchsize = 100;opts.momentum = 0;opts.alpha = 1;dbn = dbnsetup(dbn, train_x, opts);dbn = dbntrain(dbn, train_x, opts);%unfold dbn to nnnn = dbnunfoldtonn(dbn, 10);nn.activation_function = 'sigm';%train nnopts.numepochs = 1;opts.batchsize = 100;nn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.10, 'Too big error');```Example: Stacked Auto-Encoders---------------------```matlabfunction test_example_SAEload mnist_uint8;train_x = double(train_x)/255;test_x = double(test_x)/255;train_y = double(train_y);test_y = double(test_y);%% ex1 train a 100 hidden unit SDAE and use it to initialize a FFNN% Setup and train a stacked denoising autoencoder (SDAE)rand('state',0)sae = saesetup([784 100]);sae.ae{1}.activation_function = 'sigm';sae.ae{1}.learningRate = 1;sae.ae{1}.inputZeroMaskedFraction = 0.5;opts.numepochs = 1;opts.batchsize = 100;sae = saetrain(sae, train_x, opts);visualize(sae.ae{1}.W{1}(:,2:end)')% Use the SDAE to initialize a FFNNnn = nnsetup([784 100 10]);nn.activation_function = 'sigm';nn.learningRate = 1;nn.W{1} = sae.ae{1}.W{1};% Train the FFNNopts.numepochs = 1;opts.batchsize = 100;nn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.16, 'Too big error');```Example: Convolutional Neural Nets---------------------```matlabfunction test_example_CNNload mnist_uint8;train_x = double(reshape(train_x',28,28,60000))/255;test_x = double(reshape(test_x',28,28,10000))/255;train_y = double(train_y');test_y = double(test_y');%% ex1 Train a 6c-2s-12c-2s Convolutional neural network %will run 1 epoch in about 200 second and get around 11% error. %With 100 epochs you'll get around 1.2% errorrand('state',0)cnn.layers = { struct('type', 'i') %input layer struct('type', 'c', 'outputmaps', 6, 'kernelsize', 5) %convolution layer struct('type', 's', 'scale', 2) %sub sampling layer struct('type', 'c', 'outputmaps', 12, 'kernelsize', 5) %convolution layer struct('type', 's', 'scale', 2) %subsampling layer};cnn = cnnsetup(cnn, train_x, train_y);opts.alpha = 1;opts.batchsize = 50;opts.numepochs = 1;cnn = cnntrain(cnn, train_x, train_y, opts);[er, bad] = cnntest(cnn, test_x, test_y);%plot mean squared errorfigure; plot(cnn.rL);assert(er<0.12, 'Too big error');```Example: Neural Networks---------------------```matlabfunction test_example_NNload mnist_uint8;train_x = double(train_x) / 255;test_x = double(test_x) / 255;train_y = double(train_y);test_y = double(test_y);% normalize[train_x, mu, sigma] = zscore(train_x);test_x = normalize(test_x, mu, sigma);%% ex1 vanilla neural netrand('state',0)nn = nnsetup([784 100 10]);opts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samples[nn, L] = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.08, 'Too big error');%% ex2 neural net with L2 weight decayrand('state',0)nn = nnsetup([784 100 10]);nn.weightPenaltyL2 = 1e-4; % L2 weight decayopts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samplesnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex3 neural net with dropoutrand('state',0)nn = nnsetup([784 100 10]);nn.dropoutFraction = 0.5; % Dropout fraction opts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samplesnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex4 neural net with sigmoid activation functionrand('state',0)nn = nnsetup([784 100 10]);nn.activation_function = 'sigm'; % Sigmoid activation functionnn.learningRate = 1; % Sigm require a lower learning rateopts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samplesnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex5 plotting functionalityrand('state',0)nn = nnsetup([784 20 10]);opts.numepochs = 5; % Number of full sweeps through datann.output = 'softmax'; % use softmax outputopts.batchsize = 1000; % Take a mean gradient step over this many samplesopts.plot = 1; % enable plottingnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex6 neural net with sigmoid activation and plotting of validation and training error% split training data into training and validation datavx = train_x(1:10000,:);tx = train_x(10001:end,:);vy = train_y(1:10000,:);ty = train_y(10001:end,:);rand('state',0)nn = nnsetup([784 20 10]); nn.output = 'softmax'; % use softmax outputopts.numepochs = 5; % Number of full sweeps through dataopts.batchsize = 1000; % Take a mean gradient step over this many samplesopts.plot = 1; % enable plottingnn = nntrain(nn, tx, ty, opts, vx, vy); % nntrain takes validation set as last two arguments (optionally)[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');```[![Bitdeli Badge](https://d2weczhvl823v0.cloudfront.net/rasmusbergpalm/deeplearntoolbox/trend.png)](https://bitdeli.com/free "Bitdeli Badge")

C++中find()函数用法

algorithm中的find()函数 返回值是目标元素的下标,找不到时返回值为迭代器结尾 string = "hello"; find(s.begin(), s.end(), 'o') == s.end() string中的与find相关函数 ① find() string str1, str2; char c; str1.find(str2);//从串str1中查找时str...

qq_52051345的博客 5万+

汉书HE8,好用的汇编代码编写工具

汉书HE8,好用的汇编代码编写工具早期编写汇编代码所用功能强大

string的find( )函数✅

基本用法 string的find()函数用于找出字母在字符串中的位置。 find(str,position) str:是要找的元素 position:字符串中的某个位置,表示从从这个位置开始的字符串中找指定元素(不填第二个参数,默认从字符串的开头进行查找) 返回值为目标字符的位置,当没有找到目标字符时返回npos ...

Running Snail 3万+

Probability and Statistics - 4th edition

Probability and Statistics - 4th edition

C++string中find_first_of()函数和find_last_of()函数

C++string中find_first_of()函数和find_last_of()函数

信小海 4万+

C++中的find函数

C++中的find()函数有多种用法。它可以用于string类,用于查找字符或字符串。。此外,find()也可以用于vector容器,用于查询指定元素是否存在。还有一个STL函数find(),它位于头文件下,返回一个迭代器,指向范围内搜索元素的第一次出现。

Iamme 2万+

c++string函数(一)——find、rfind详细用法

string的find函数1.正向查找find()1.1 s.find(str)1.2 s.find(str,pos)1.3 s.find_first_of(str) 和 s.find_last_of(str)1.4查找目标字符串在字符串出现的总次数2.逆向查找rfind() 1.正向查找find() 1.1 s.find(str) string中find()返回值是字母在母串中的下标位置。 如果没有找到,那么会返回一个特别的标记npos,一般写作string::npos。 string s, c; int

Erya Programming learning 10万+

C++string中find()和rfind()函数

C++string中find()和rfind()函数 1、find()函数 查字符或字符串,若查找成功,则返回正向查到的第一个字符下标或第一个字符串首字符的下标;若查找失败,无法返回正确的下标。find()函数的返回值为无符号整数类型。 2、 rfind()函数 逆向查字符或字符串,若查找成功,则返回逆向查到的第一个字符下标或第一个字符串首字符的下标;若查找失败,无法返回正确的下标。逆向查到的第一...

信小海 1万+

C++string中的find()函数

C++ string中的find()函数 查找字符串s1中是否包含子串s2? 思路:此处需要用到string库中的find函数与npos参数。 (1)string::npos参数 string::npos参数: npos是一个常数,用来表示不存在的位置,npos定义的类型是: string::size_type。npos定义为:static const size_type npos=-1; ...

Sunflower的博客 5936

C++ 中string.find()函数的用法及实例

C++ 中string.find()函数的用法及实例

李典金 7641

C++中string的find函数

参考:博客园 同勉共进 find函数的返回值是无符号整型,如果查找不到,就会返回4294967295(string::npos表示-1或4294967295) /*测试string中的find*/ #include&lt;iostream&gt; #include&lt;string&gt; using namespace std; int main() { //测试size...

小卜妞的博客 9535

C++中,string::nops的用法以及和string.find()的区别

////find函数返回类型 size_type string s("1a2b3c4d5e6f7g8h9i1a2b3c4d5e6f7g8ha9i"); string flag; string::size_type position; //find 函数 返回jk 在s 中的下标位置 position = s.find("jk"); if (position != s.npos) //如果没...

如梦山河的博客 8994

C++ string中find() ,rfind() 等函数 用法总结及示例

string中 find()的应用  (rfind() 类似,只是从反向查找)原型如下:(1)size_t find (const string& str, size_t pos = 0) const;  //查找对象--string类对象(2)size_t find (const char* s, size_t pos = 0) const; //查找对象--字符串(3)size_t find

张勤一 1万+

c++string.find()函数用法整理

发现find函数有很多用法,容易混淆,于是整理一下方便以后查看 #include #include using namespace std; void main() { ////find函数返回类型 size_type string s("1a2b3c4d5e6f7g8h9i1a2b3c4d5e6f7g8ha9i"); string flag; string::size_type posi

monkeyduck的专栏 5万+
上一篇: 从字符串中删除指定子串或指定元素
下一篇: 数据库范式——通俗易懂【转】
希尔希尔
博客等级 码龄17年 9粉丝 7原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

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

抵扣说明:

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

余额充值