ハッカーランクプラットフォームで行われたテストでは、は、 b 'を置き換え、すべての' a 'を2つの' d 'で置換する。例:I/P:私はこのプログラム:(を思い付くことができた様々な文献を検索した後 O/P はをddddcd abacdb私は関数型を変更することはでき波平ますのでご注意ください)C++文字列、文字 'b'を削除し、文字 'a'を2つの '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リースは通常のcharポインタ、char *
である私は