C++ std::replace的逻辑错误
今天我在做一个学校项目时遇到了一个逻辑错误。对于本项目,需要切换C++ std::replace的逻辑错误,c++,string,algorithm,stl,C++,String,Algorithm,Stl,今天我在做一个学校项目时遇到了一个逻辑错误。对于本项目,需要切换chars 例如,用户输入字母A。它切换到U,一切正常。但是,当用户输入字母U时会出现问题。这个角色保持不变。我实现了两个规则,从A切换到U,并将U切换到A。字符串通过这两个规则,因此保持不变。我怎样才能防止这种情况 #include <iostream> #include <algorithm> #include <string> using namespace std; int main(
chars
例如,用户输入字母A
。它切换到U
,一切正常。但是,当用户输入字母U
时会出现问题。这个角色保持不变。我实现了两个规则,从A
切换到U
,并将U
切换到A
。字符串通过这两个规则,因此保持不变。我怎样才能防止这种情况
#include <iostream>
#include <algorithm>
#include <string>
using namespace std;
int main()
{
string seq;
getline(cin, seq);
transform ( seq.begin(), seq.end(), seq.begin(), ::toupper );
replace ( seq.begin(), seq.end(), 'A', 'U' );
replace ( seq.begin(), seq.end(), 'U', 'A' );
cout << seq;
return 0;
}
#包括
#包括
#包括
使用名称空间std;
int main()
{
字符串序列;
getline(cin,seq);
转换(seq.begin(),seq.end(),seq.begin(),::toupper);
替换(顺序开始(),顺序结束(),'A','U');
替换(顺序开始(),顺序结束(),'U','A');
在您的实现中,您可以将A
替换为U
,然后U
返回到A
:
replace ( seq.begin(), seq.end(), 'A', 'U' );
replace ( seq.begin(), seq.end(), 'U', 'A' );
您需要一次完成替换,例如使用std::transform
:
std::transform(begin(seq), end(seq), begin(seq), some_function_object);
您只需编写这个神秘的函数\u对象
。它可以是一个自由函数:
char some_function_object(char input) { /* ... */ }
std::transform(begin(seq), end(seq), begin(seq), some_function_object);
std::transform(begin(seq), end(seq), begin(seq), [](char input) { /* ... */ } );
或lambda函数:
std::transform(begin(seq), end(seq), begin(seq), [](char input) { /* ... */ } );
此函数对象必须采用char
并返回:
A
如果输入
为U
U
如果输入
是A
输入
否则
如果它是一个自由函数,你应该如何调用它呢?好吧,为什么不使用一个自描述性的名称,比如swapAandU
?基于范围的for循环似乎比使用标准算法(例如std::transform)更合适、更有效
比如说
#include <iostream>
#include <string>
int main()
{
std::string s( "AUAUA" );
std::cout << s << std::endl;
for ( char &c : s )
{
if ( c == 'A' ) c = 'U';
else if ( c == 'U' ) c = 'A';
}
std::cout << s << std::endl;
return 0;
}
#include <iostream>
#include <string>
std::string & convert( std::string &s1, const std::string &s2, const std::string &s3 )
{
for ( char &c : s1 )
{
auto i = s2.find( c );
if ( i != std::string::npos ) c = s3[i];
}
return s1;
}
int main()
{
std::string s( "AUAUA" );
std::cout << s << std::endl;
std::cout << convert( s, "AU", "UA" ) << std::endl;
return 0;
}
例如,您可以编写一个单独的函数
#include <iostream>
#include <string>
int main()
{
std::string s( "AUAUA" );
std::cout << s << std::endl;
for ( char &c : s )
{
if ( c == 'A' ) c = 'U';
else if ( c == 'U' ) c = 'A';
}
std::cout << s << std::endl;
return 0;
}
#include <iostream>
#include <string>
std::string & convert( std::string &s1, const std::string &s2, const std::string &s3 )
{
for ( char &c : s1 )
{
auto i = s2.find( c );
if ( i != std::string::npos ) c = s3[i];
}
return s1;
}
int main()
{
std::string s( "AUAUA" );
std::cout << s << std::endl;
std::cout << convert( s, "AU", "UA" ) << std::endl;
return 0;
}
字符串没有变化。为什么
而不是
?既不高效也不友好:你可以先用字符串中没有出现的字符x替换A
,然后用第二个字符y替换U
,然后用U和y分别替换x与一起工作你在问如何将所有的“A”转换为“U”和所有的“U”转换为“A”'不撤消任何以前的转换?我认为答案在于您使用的转换
。思考如何修改此代码。此代码按照我希望的方式工作。非常感谢!“比使用标准算法更有效,例如std::transform”[引文需要];@YSC冗余分配不存在。