#include <string>
#include <iostream>
#include <vector>
#include <sstream>
using namespace std;
//字符串分割函数
void StrSplit(string str,vector<string> *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<string> strvec;
StrSplit(test,&strvec);
std::stringstream ss;
int temp;
int max,min;
vector<string>::iterator iter1 = strvec.begin(), iter2 = strvec.end();
while (iter1 != iter2)
{
if(iter1 == strvec.begin())
{
ss<<strvec[0];
ss>>temp;
max=temp;
min=temp;
}
ss.clear();
cout << *iter1 << endl;
ss<<*iter1;
ss>>temp;
max=max>temp? max:temp;
min=temp<min? temp:min;
++iter1;
}
cout<<max+min<<endl;;
return 0;
}
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 <iostream> #include <string> 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<i;j++) cout<<stra[j]<<endl; return 0; }
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 : "<<position<<endl;
//查找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++;
}
//查找flag 中与s 第一个不匹配的位置
flag="acb12389efgxyz789";
position=flag.find_first_not_of (s);
cout<<"flag.find_first_not_of (s) :"<<position<<endl;
//反向查找,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,这是一个很大的数,其值不需要知道。