0
在上Hackerrank平臺進行了測試,我被要求建立,取消後返回字符串的函數所有的' b」和更換所有的‘a’通過兩個‘d’。例如:I/P:abacdb O/P ddddcd通過各種文獻搜索我能想出這個程序:(請注意我不是擅自改變功能型)C++字符串,建立函數刪除字符「b」,並替換字符「a」兩個「d」
後#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*'
P租賃幫我找出什麼錯誤,我正在做