博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
字符串分割,求最大值最小值之和——形如(1,2,3,6)
阅读量:4553 次
发布时间:2019-06-08

本文共 3441 字,大约阅读时间需要 11 分钟。

#include 
#include
#include
#include
using namespace std;//字符串分割函数void StrSplit(string str,vector
*strvec){ string strtemp; string::size_type pos1, pos2; pos2 = str.find(','); pos1 = 0; while (string::npos != pos2) { (*strvec).push_back(str.substr(pos1, pos2 - pos1)); pos1 = pos2 + 1; pos2 = str.find(',', pos1); } (*strvec).push_back(str.substr(pos1)); }int main(){ string test; cin>>test; vector
strvec; StrSplit(test,&strvec); std::stringstream ss; int temp; int max,min; vector
::iterator iter1 = strvec.begin(), iter2 = strvec.end(); while (iter1 != iter2) { if(iter1 == strvec.begin()) { ss<
>temp; max=temp; min=temp; } ss.clear(); cout << *iter1 << endl; ss<<*iter1; ss>>temp; max=max>temp? max:temp; min=temp

1.sstream利用输入输出做数据转换

1 stringstream ss_stream; 2 ss_stream << i;   // 将int输入流中 3 ss_stream >> str; // 将ss_stream中的数值输出到str中 4  5 //注意:如果做多次数据转换;必须调用clear()来设置转换模式 6 ss_stream << "456";  7 ss_stream >> i;   // 首先将字符串转换为int 8 ss_stream.clear(); 9 ss_stream << true;10 ss_stream >> i;   // 然后将bool型转换为int;假如之前没有做clear,那么i会出错

2.错误捕捉处理

if (! ss.good()){    //错误发生处理方案}

3.分割处理也可以有另一种处理方式

先查找分隔字符的位置,让后截取子串。#include 
#include
using namespace std;int main(){ string str="aaa,bbb,ccc,ddd"; string stra[10]; int index = str.find(','); int i=0; while(index!=-1) { stra[i++] = str.substr(0,index);//截取子串 str=str.substr(index+1); index = str.find(','); //查找分隔符位置 } stra[i++]=str; for(int j=0;j

4.string::size_type抽象意义是尺寸单位类型。string::size_type它在不同的机器上,长度是可以不同的,并非固定的长度。但只要你使用了这个类型,就使得你的程序适合这个机器。与实际机器匹配。

5.string 类提供了 6 种查找函数,每种函数以不同形式的 find 命名。这些操作全都返回 string::size_type 类型的值,以下标形式标记查找匹配所发生的位置;或者返回一个名为 string::npos 的特殊值,说明查找没有匹配。string 类将 npos 定义为保证大于任何有效下标的值。

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;   }

  

//find 函数 返回flag 中任意字符 在s 中第一次出现的下标位置   flag = "c";   position = s.find_first_of(flag);   cout << "s.find_first_of(flag) is : " << position << endl;

  

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

  

//查找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  "<<<" : "<
<

  

//查找flag 中与s 第一个不匹配的位置  flag="acb12389efgxyz789";  position=flag.find_first_not_of (s);  cout<<"flag.find_first_not_of (s) :"<
<

    

//反向查找,flag 在s 中最后出现的位置   flag="3";   position=s.rfind (flag);   cout<<"s.rfind (flag) :"<
<

说明:

(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,这是一个很大的数,其值不需要知道。

转载于:https://www.cnblogs.com/super86/p/3355587.html

你可能感兴趣的文章
apache服务器安装
查看>>
Search a 2D Matrix
查看>>
文件解析漏洞
查看>>
弹性成像的一些术语
查看>>
作业2
查看>>
vim 笔记
查看>>
MySQL的基本使用命令
查看>>
第三次作业
查看>>
蓝桥杯-猜算式
查看>>
PHP定界符<<<EOF
查看>>
05_模板方法模式
查看>>
linux下搭建DHCP服务
查看>>
双曲函数与反双曲函数
查看>>
EF中的Select * From T Where id In(xxx,xxx)子查询
查看>>
IDEA Maven配置
查看>>
mapreduce 实现矩阵乘法
查看>>
Jquery EasyUI封装简化操作
查看>>
OO第一单元总结
查看>>
[原创]你所需要了解的软件测试相关标准
查看>>
最近这么火的iOS视频直播
查看>>