【问题标题】:c++ string, build function that removes character 'b' and replaces character 'a' by two 'd'c++字符串,删除字符'b'并将字符'a'替换为两个'd'的构建函数
【发布时间】:2016-11-25 22:02:26
【问题描述】:

在 Hackerrank 平台上进行的一项测试中,我被要求构建一个函数,该函数在删除所有“b”并将所有“a”替换为两个“d”后返回一个字符串。例如: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*'

请帮我找出我做错了什么

【问题讨论】:

    标签: c++ string function


    【解决方案1】:
    for(const char &C : s){
    

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

    s.resize(write_idx+a_count);
    

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

    write_idx = s.size()-1;
    

    s 是一个char *,一个普通的字符指针。它不是实现称为size() 的方法的类。

    所示代码的总体问题是它假定s 是某种类,如std::string,它实现了各种方法。不是,是普通的char *

    这就是编译错误的原因。

    【讨论】:

      猜你喜欢
      • 2015-11-24
      • 1970-01-01
      • 1970-01-01
      • 2021-08-19
      • 1970-01-01
      • 1970-01-01
      • 2011-04-14
      • 2017-03-24
      • 2011-06-21
      相关资源
      最近更新 更多