简体   繁体   English

=使用const string&时未为std :: search定义

[英]= not defined for std::search when using const string&

I have the following code, which works fine , however when I change the iter getB(string& s , string& tag); 我有以下代码, 可以正常工作 ,但是当我更改iter getB(string& s , string& tag); function definition to iter getB(const string& s , const string& tag); iter getB(const string& s , const string& tag);函数定义iter getB(const string& s , const string& tag); I get the error pasted at the end. 我将错误粘贴到最后。 I believe the = operator is not defined because with this new function definition, if I don't assign the result of search to i the program compiles, though later resulting in a segmentation fault, which I believe is expected. 我相信未定义=运算符,因为使用此新函数定义,如果我不将search结果分配给i程序进行编译,尽管稍后会导致出现分段错误,这是我所期望的。 Can someone explain to me why I can't assign the result of search when the function definition contains const keywords. 有人可以向我解释为什么当函数定义包含const关键字时我不能分配search结果。 Thank you. 谢谢。

the code: 编码:

#include<string>
#include<iostream>
#include<algorithm>

using namespace std;
typedef string::iterator iter;
iter getB(string& s , string& tag);
int main (){
   string line = "hello, how are you?";
   string tag = "how";
   iter i = getB(line,tag);

   for(i ; i!=line.end(); i++){
       cout << *i ;
   }

   cout << endl;
return 0;
}
iter getB(string& s , string& tag)
{
    iter i;

    i = search(s.begin() , s.end() , tag.begin() , tag.end());

    return i;
}

~
~
the error message with the altered function definition: 具有更改的功能定义的错误消息:

test1.cpp: In function 'iter getB(const std::string&, const std::string&)': test1.cpp:24: error: no match for 'operator=' in 'i = std::search [with _ForwardIterator1 = __gnu_cxx::__normal_iterator<const char*, std::basic_string<char, std::char_traits<char>, std::allocator<char> > >, _ForwardIterator2 = __gnu_cxx::__normal_iterator<const char*, std::basic_string<char, std::char_traits<char>, std::allocator<char> > >](((const std::string*)s)->std::basic_string<_CharT, _Traits, _Alloc>::begin [with _CharT = char, _Traits = std::char_traits<char>, _Alloc = std::allocator<char>](), ((const std::string*)s)->std::basic_string<_CharT, _Traits, _Alloc>::end [with _CharT = char, _Traits = std::char_traits<char>, _Alloc = std::allocator<char>](), ((const std::string*)tag)->std::basic_string<_CharT, _Traits, _Alloc>::begin [with _CharT = char, _Traits = std::char_traits<char>, _Alloc = std::allocator<char>](), ((const std::string*)tag)->std::basic_string<_CharT, _Traits, _Alloc>::end [with _CharT = char, _Traits = std::char_traits<char>, _Alloc = std::allocator<char>]())' /usr/include/c++/4.2.1/bits/stl_iterator.h:637: note: candidates are: __gnu_cxx::__normal_iterator<char*, std::basic_string<char, std::char_traits<char>, std::allocator<char> > >& __gnu_cxx::__normal_iterator<char*, std::basic_string<char, std::char_traits<char>, std::allocator<char> > >::operator=(const __gnu_cxx::__normal_iterator<char*, std::basic_string<char, std::char_traits<char>, std::allocator<char> > >&)

For a const string , begin() and end() return std::string::const_iterator not std::string::iterator . 对于const stringbegin()end()返回std::string::const_iterator而不是std::string::iterator You can convert an iterator to a const_iterator , but not vice versa. 您可以将iterator转换为const_iterator ,反之亦然。

The quick solution would be to change your typedef: 快速的解决方案是更改您的typedef:

typedef string::const_iterator iter;

If s is a const string & , then s.begin() , and hence the return type of search , is string::const_iterator . 如果sconst string & ,则s.begin()以及search的返回类型为string::const_iterator This is not convertible to string::iterator , which is the type of i . 这不能转换为string::iterator ,它是i的类型。

This is as it should be, since otherwise that conversion would break const -correctness by allowing you to modify the string. 这是应该的,因为否则转换将通过允许您修改字符串来破坏const -correctness。 You should change the type of i to string::const_iterator , or perhaps auto if you're using C++11. 您应将i的类型更改为string::const_iterator ,如果使用的是C ++ 11,则应更改为auto

if you pass in a const iterator to std::search, then the return value is also a const iterator (it will be of the same type as the first iterator passed in) 如果将const迭代器传递给std :: search,则返回值也是const迭代器(它将与传入的第一个迭代器具有相同的类型)

change this: 改变这个:

typedef string::iterator iter; 

to this: 对此:

typedef string::const_iterator iter; 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM