c ++ string,用于删除字符的构建函数' b'并取代字符' a'由两个'

时间:2016-11-25 22:02:27

标签: c++ string function

在Hackerrank平台上进行的测试中,我被要求构建一个函数,在删除所有' b'之后返回一个字符串。并取代所有的' a'由两个' 。例如:i / p: abacdb o / p ddddcd 在搜索各种文献后,我能够出现使用此程序:(请注意我不允许更改功能类型

#include <iostream>
using namespace std;
char* ReplaceandRemove(char* s){
    int write_idx=0;
    int a_count=0;
    for(const char &C : s){
        if(C!='b'){
            s[write_idx++]=C;
        }
        if(C=='a'){
            ++a_count;
        }
    }

    s.resize(write_idx+a_count);
    int curr_idx=write_idx - 1;
    write_idx = s.size()-1;
    while(curr_idx>=0){
        if(curr_idx=='a'){
            s[write_idx--]='d';
            s[write_idx--]='d';
        }
        else{
            s[write_idx--]=s[curr_idx];
        }
        --curr_idx;
    }
    return s;

}

int main() {
    char s[6]={'a', 'b', 'a', 'c', 'd', 'b'};
    ReplaceandRemove(s);
    cout<<s;

    return 0;
}

该程序提供了许多超出我理解的错误:

In function 'char* ReplaceandRemove(char*)':
6:25: error: no matching function for call to 'begin(char*&)'
s){

6:25: note: candidates are:
42:0,
52,
40,
41,
42,
38,
39,
1:
89:5: note: template constexpr const _Tp* std::begin(std::initializer_list<_Tp>)


89:5: note: template argument deduction/substitution failed:
6:25: note: mismatched types 'std::initializer_list<_Tp>' and 'char*'
s){

51:0,
40,
41,
42,
38,
39,
1:
87:5: note: template _Tp* std::begin(_Tp (&)[_Nm])


87:5: note: template argument deduction/substitution failed:
6:25: note: mismatched types '_Tp [_Nm]' and 'char*'
s){

51:0,
40,
41,
42,
38,
39,
1:
58:5: note: template decltype (__cont.begin()) std::begin(const _Container&)


58:5: note: template argument deduction/substitution failed:
In substitution of 'template decltype (__cont.begin()) std::begin(const _Container&) [with _Container = char*]':
6:25: required from here
58:5: error: request for member 'begin' in '__cont', which is of non-class type 'char* const'
48:5: note: template decltype (__cont.begin()) std::begin(_Container&)


48:5: note: template argument deduction/substitution failed:
In substitution of 'template decltype (__cont.begin()) std::begin(_Container&) [with _Container = char*]':
6:25: required from here
48:5: error: request for member 'begin' in '__cont', which is of non-class type 'char*'
6:25: error: no matching function for call to 'end(char*&)'
s){

6:25: note: candidates are:
42:0,
52,
40,
41,
42,
38,
39,
1:
99:5: note: template constexpr const _Tp* std::end(std::initializer_list<_Tp>)


99:5: note: template argument deduction/substitution failed:
6:25: note: mismatched types 'std::initializer_list<_Tp>' and 'char*'
s){

51:0,
40,
41,
42,
38,
39,
1:
97:5: note: template _Tp* std::end(_Tp (&)[_Nm])


97:5: note: template argument deduction/substitution failed:
6:25: note: mismatched types '_Tp [_Nm]' and 'char*'
s){

51:0,
40,
41,
42,
38,
39,
1:
78:5: note: template decltype (__cont.end()) std::end(const _Container&)


78:5: note: template argument deduction/substitution failed:
In substitution of 'template decltype (__cont.end()) std::end(const _Container&) [with _Container = char*]':
6:25: required from here
78:5: error: request for member 'end' in '__cont', which is of non-class type 'char* const'
68:5: note: template decltype (__cont.end()) std::end(_Container&)


68:5: note: template argument deduction/substitution failed:
In substitution of 'template decltype (__cont.end()) std::end(_Container&) [with _Container = char*]':
6:25: required from here
68:5: error: request for member 'end' in '__cont', which is of non-class type 'char*'
15:7: error: request for member 'resize' in 's', which is of non-class type 'char*'


17:19: error: request for member 'size' in 's', which is of non-class type 'char*'

请帮我弄清楚我在做什么错误

1 个答案:

答案 0 :(得分:1)

for(const char &C : s){

schar *,是一个普通的char指针。范围迭代仅适用于std::begin()std::end()支持的那些类型。普通指针不是其中之一。

s.resize(write_idx+a_count);

schar *,是一个普通的char指针。它不是一个实现名为resize()的方法的类。

write_idx = s.size()-1;

schar *,是一个普通的char指针。它不是一个实现名为size()的方法的类。

显示的代码的总体问题是它假定s是某种类,如std::string,它实现了各种方法。它不是,它是一个简单的char *

这就是编译错误的原因。