有关 const_iterator 的问题

来源:百度知道 编辑:UC知道 时间:2024/06/18 17:15:05
有关 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(), ch

看看你的size_t count( const string & childstr, const string & parentstr ) 函数的参数

const string& //这是个 pass by reference to const, effective c++ 里有讲过 是个搞效率的传送方式 比pass by value 高效很多

明白了么?