好的,所以我需要一些帮助来交换我的字符串。
这是我正在尝试做的整体代码,但我不能只是移动字符串。我开始尝试将其转换为字符,但大多数回复说只使用 std::swap 函数,但是我真的迷失在使用这个......
我的总体目标是置换一个字符串,可以将其指定到字符串的某个部分。我是 C++ 新手,我只是不确定如何使用 C++ 方法/函数来实现这一点。
(还有一个 main.cc 和 Permutation h。但它只用于定义变量,基本上是骨架代码)
感谢所有帮助,我将在大约 2 小时后回来查看。
更新代码)
#include <iostream> // for cout
#include <cstdio> // for printf()
#include <sstream> // for stringstream
#include <stdio.h>
#include <string.h>
#include "Permutation.h"
using namespace std;
Permutation::Permutation() {
/* nothing needed in the constructor */
}
void Permutation::permute(const string& str) {
string stringnew = str;
int j;
int low = 0;
int high = str.length();
cout << stringnew << endl;
for (j = 0; j <= high; j++) {
string strtemp = stringnew[j];
std::swap((strtemp + low), (strtemp + j));
permute(str, low + 1, high);
std::swap(str[j + low], str[j + j]);
}
}
void Permutation::permute(const string& str, int low, int high) {
// int j;
// if (low == high) {
// cout << str << endl;
// } else {
// for (j = low; j <= high; j++) {
// std::swap(str[j + low], str[j + j]);
// permute(str, low + 1, high);
// std::swap(str[j + low], str[j + j]);
// }
// }
}