操作字符串的函数 ( "abcdef" -> "faebdc" )
Function to manipulate a string ("abcdef" -> "faebdc")
大家好,我正在开发一个函数,以以下方式操作任何字符串。
"abc"->"cab"
"abcd"->"dacb"
"abcdef"->"faebdc"
"diversinta"->"adtinveeg"
等等
这是我迄今为止想出的代码。我认为它能起到作用,但我认为代码和解决方案有点难看,我不确定它是否能防故障,是否能在每个给定的情况下正常工作。我将非常感谢对这段代码的任何输入,或关于如何编写此函数的任何示例。我恳求你记住,我是一个非常优秀的人,所以不要对我太苛刻
string transformer(string input) {
string temp;
int n = 0;
int m = (input.length() -1);
for( int i = 0; i < input.length(); i++) {
temp += input[m];
if (input[m] == input[n]) {
break;
}
else {
temp += input[n];
}
n += 1;
m -= 1;
if ( temp.length() == input.length() ) {
break;
}
}
return temp; }
您有三个问题。
试试"abbba"。如果结果不是你想要的,那么这个条件是:
if (input[m] == input[n]) {
break;
}
这完全是错误的。
看看另一个条件:
if ( temp.length() == input.length() ) {
break;
}
您一次向temp
添加两个字符。如果input
的长度为奇数怎么办?
假设这是正确的。考虑循环:
for( int i = 0; i < input.length(); i++) {
...
if ( temp.length() == input.length() ) {
break;
}
}
该循环在for
语句中永远不会终止。你也可以这样做:
while( temp.length() < input.length() ) {
...
}
一旦这些都正常工作,就应该研究迭代器。
此函数只将两个索引移向中心,直到它们相遇或经过。最后一个if
块处理奇数长度输入字符串的情况。它适用于ideone.com 上的所有测试用例
std::string transformer(const std::string& input)
{
std::string temp;
int i = 0;
int j = input.length() - 1;
while (i < j) {
temp += input[j--];
temp += input[i++];
}
if (i == j) {
temp += input[i];
}
return temp;
}
std::string transformer(const std::string& input) {
std::string res(input.length(), '0');
for (int i = 0; i < input.length(); ++i) {
res[i] = input[ i % 2 == 0 ? input.length() - (i/2) - 1 : (i/2) ];
}
return res;
}
不幸的是,if (input[m] == input[n])
将确保如果第一个和最后一个字符相同,则在处理完第一个字符后立即退出。
我会用std::string::iterator
和std::string::reverse_iterator
:
auto it = input.begin();
auto rit = input.rbegin();
std::string temp;
for (size_t i = 0; i < input.length()/2; ++i) {
temp += *rit++;
temp += *it++;
}
处理空和奇数长度输入的逻辑由您自己来做,不应该太难。(长度为1的输入也是一种特殊情况)
我会使用指针而不是索引来实现这一点。
因此,你有一个指针来读取边,并在每次迭代时不断交换它们。
它也会使它更快。
我认为这应该有效,但我不记得如何制作一个const-char指针数组。有人能帮我走那一步吗?
string transformer(string input) {
std::string temp;
const char *front, *back;
for (*front = input.c_str(), *back = front + input.length() - 1; front < back ; front++, back--) {
temp += *back;
temp += *front;
}
if (front == back)
temp += *front;
return temp;
}
(使用与@Blastfurt相同的方法,但跳过不必要的索引。)
- EASTL矢量<向量<int>>连续的
- C - 创建矢量&lt; vector&lt; double&gt;&gt;矩阵具有分配而不是inizializ
- C 字符串比较“祝您好运”&gt;“再见”
- 为什么将此对向量&lt; map&lt; int,int&gt;&gt;中的地图进行更新.失败
- C :对矢量进行排序&lt; struct&gt;(结构有2个整数)基于结构的整数之一
- C 操作员&gt;&gt;与突变器过载
- 明确的专业化“ CheckIntmap&lt;&gt;”实例化
- 是否需要使用 - &gt;运算符在C 中调用成员函数时
- 什么是模板&lt;&gt;inline bla bla
- 编辑C Qlist&lt; object*&gt; gt;QML代码和一些QML警告中的模型
- eigen :: llt&lt;eigen :: matrixxd&gt;具有不完整的类型
- 错误,包括&lt; ctype&gt;在原子上使用C 11
- 错误c++visual studio c2227左侧'->;Init';必须指向类/结构/联合/泛型类型
- std::vector<;uint8_t>;当C++11/14启用时,手动复制而不是调用memcpy
- ``这个''不能用this-&gt;指针变量
- 如何加入向量&lt; int&gt;到C 中的单个INT
- 是std :: set&lt; std :: future&gt;不可能存在
- 使用shared_ptr<字符串>转换为一个无序集合<字符串>
- 是numeric_limits&lt; int&gt; :: is_modulo从逻辑上矛盾
- opencv 2.4.7在iOS错误背景_segm.hpp #include&lt; list&gt;未找到