さて、弦を交換するには助けが必要です。
これが私がやろうとしていることの全体的なコードですが、文字列を移動することはできません。私はそれを文字に変換しようと始めましたが、返信の大部分は std::swap 関数を使用するだけであると言われましたが、これを使用することに本当に迷っています...
私の全体的な目標は、文字列の特定のセクションに指定できる文字列を並べ替えることです。これを達成するために 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]);
// }
// }
}