读书人

啊又一个不懂的日C++真不好弄(给

发布时间: 2012-02-11 09:51:34 作者: rapoo

啊……又一个不懂的,日,C++真不好弄(给分速度~~~)
#include<iostream>
#include<string>
using namespace std;
void main()//程序用来显示String “<……>”中的内容
{
string content="<1,1><2,2><1,1><,21><32454.>";
int enpos=0;//用substr(stpos,enpos)方法来显示string里位置stpos到enpos子段的内容
int stpos=0;
while(stpos!=string::npos)
{
enpos=content.find_first_of('>',stpos);
cout<<content.substr(stpos,enpos)<<endl;
stpos=content.find_first_of('<',enpos);
}//原来是想要一行显示一个<……>内容
}
结果:
〈1,1
〈2,2〉〈1,1
〈1,1〉〈,2〉〈324
〈,21〉〈32454,〉
〈32454,〉
想要的结果应该是:
<1,1>
<2,2>
<1,1>
<,21>
<32454.>


[解决办法]
#include <iostream>
#include <string>
using namespace std;
void main()//程序用来显示String “ <…… >”中的内容
{
string content="<1,1> <2,2 > <1,1 > <,21 > <32454. >";
int enpos=0;//用substr(stpos,enpos)方法来显示string里位置stpos到enpos子段的内容
int stpos=0;
while(stpos!=string::npos)
{
enpos=content.find_first_of('>',stpos);
cout <<content.substr(stpos,enpos-stpos+1)<<endl;
stpos=content.find_first_of('<',enpos);

}//原来是想要一行显示一个 <…… >内容
}
[解决办法]

C/C++ code
#include <iostream > #include <string > using namespace std; void main()//程序用来显示String “ <…… >”中的内容 {     string content=" <1,1 > <2,2 > <1,2 > <,21 > <32454. >";     int enpos=0;//用substr(stpos,enpos)方法来显示string里位置stpos到enpos子段的内容     int stpos=0;     while(1)     {         stpos=content.find_first_of( '<',enpos);        enpos=content.find_first_of( '>',stpos);                 if(stpos<0 ||enpos<0)            break;        cout <<content.substr(stpos,enpos-stpos+1) <<endl;             }//原来是想要一行显示一个 <…… >内容 }
[解决办法]
C/C++ code
#include <iostream> #include <string> using namespace std; void main(){     string content = " <1,1> <2,2> <1,1> <,21> <32454.>";      int nPre = -1;    int nCur = -1;    while(true)     {         nPre = content.find('>', nPre+1);         nCur = content.find('<', nCur+1);        if (nPre == string::npos)        {            break;        }        cout << content.substr(nCur, nPre-nCur+1) << endl;     }}
[解决办法]
substr的用法
最简单的改法:cout <<content.substr(stpos,enpos-stpos+1) <<endl;
[解决办法]
C/C++ code
#include <iostream> #include <string> #include <algorithm>using namespace std; int main() {     string content = "<1,1> <2,2> <1,1> <,21> <32454.>";     string::iterator first = content.begin();    string::iterator last = content.end();    while (true) {        string::iterator it = find(first, last, '<');        first = find(it, last, '>');                if (first == last) {            break;        }        cout << string(it, ++first) << endl;    }} 

读书人网 >C++

热点推荐