有关 const_iterator 的问题
有关const_iterator的问题在下面程序中,把const_注释掉,就会出错。为什么?#include<iostream>#include<string>#incl...
有关 const_iterator 的问题
在下面程序中, 把const_注释掉,就会出错。
为什么?
#include<iostream>
#include<string>
#include<algorithm>
using namespace std;
size_t count( const string &, const string & );
int main()
{
string child = "abd";
string parent = "xabaacbaxabb";
size_t counter = count( child, parent );
cout<<child<<" appears "<<counter<<" time(s) in "<<parent<<" ."<<endl;
return 0;
}
size_t count( const string & childstr, const string & parentstr )
{
size_t counter = 0;
string::/*const_*/iterator temp_iter1 = parentstr.end(), temp_iter2;//ERROR!!!!!!!!!!!!!!!
for(string::/*const_*/iterator iter = parentstr.begin(); iter != parentstr.end(); ++iter )//ERROR!!!!!!!!!!!!!!!
{
temp_iter2 = search( iter, parentstr.end(), childstr.begin(), childstr.end() );
if( temp_iter2 == parentstr.end())
{
return counter;
}
else if( temp_iter1 != temp_iter2 )
{
++counter;
temp_iter1 = temp_iter2;
}
}
cout<<"return error = -1"<<endl;
return -1;
} 展开
在下面程序中, 把const_注释掉,就会出错。
为什么?
#include<iostream>
#include<string>
#include<algorithm>
using namespace std;
size_t count( const string &, const string & );
int main()
{
string child = "abd";
string parent = "xabaacbaxabb";
size_t counter = count( child, parent );
cout<<child<<" appears "<<counter<<" time(s) in "<<parent<<" ."<<endl;
return 0;
}
size_t count( const string & childstr, const string & parentstr )
{
size_t counter = 0;
string::/*const_*/iterator temp_iter1 = parentstr.end(), temp_iter2;//ERROR!!!!!!!!!!!!!!!
for(string::/*const_*/iterator iter = parentstr.begin(); iter != parentstr.end(); ++iter )//ERROR!!!!!!!!!!!!!!!
{
temp_iter2 = search( iter, parentstr.end(), childstr.begin(), childstr.end() );
if( temp_iter2 == parentstr.end())
{
return counter;
}
else if( temp_iter1 != temp_iter2 )
{
++counter;
temp_iter1 = temp_iter2;
}
}
cout<<"return error = -1"<<endl;
return -1;
} 展开
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询