好的,所以我现在正在尝试编译一些东西,而且我是C ++的新手,所以也许代码本身会导致错误,但Eclipse中的代码本身没有出现红色标记。
错误说的是
C:\ MinGW的\ BIN ../ LIB / GCC /的mingw32 / 4.6.2 /包括/ C ++ /比特/ move.h:128:7: 错误:分配只读引用'__a'
C:\ MinGW的\ BIN ../ LIB / GCC /的mingw32 / 4.6.2 /包括/ C ++ /比特/ move.h:129:7: 错误:分配只读参考'__b'
关于我需要做什么的任何想法?在Win7上,使用Eclipse Juno for C ++和MingwCC
以下是我正在编写的内容,我添加的唯一新内容是有人告诉我用于我的排列程序的这个“交换”内容。
已更新 Permutation.cc
#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(string str) {
int low = 0;
int high = str.length();
int j;
if (low == high) {
cout << str << endl;
} else {
for (j = low; j <= high; j++) {
std::swap(str[low], str[j]);
permute(str, low + 1, high);
std::swap(str[low], str[j]);
}
}
}
void Permutation::permute(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]);
// }
// }
}
Permutation.h
#pragma once
#include <string>
using namespace std;
class Permutation {
public:
Permutation();
void permute (string);
void permute (string, int, int);
private:
/* attemp to solve this problem without adding
* any instance variables/data members, but
* you may add private helper function members
* as many as you need */
};
main.cc
#include "Permutation.h"
int main()
{
Permutation p;
p.permute ("Permute");
p.permute ("--*--", 2, 3);
}
答案 0 :(得分:0)
弄清楚如何正确交换它。
int low = 0;
int high = str.length() - 1;
// make sure the string is a permutation and not a partial mix.
if (low == high) {
cout << str << endl;
} else {
//Takes each initial letter, then permutes the remaining string. Then moves to next character.
for (int i = low; i <= high; i++) {
std::swap(str[low], str[i]);
permute(str, low + 1, high);
std::swap(str[low], str[i]);
}
}
答案 1 :(得分:0)
我重写了你在C ++中链接的C代码:
// this method should be private or protected because
// str is passed by reference and will be modified !
// if you prefer a free standing function, don't add the
// declaration to the header, this for internal use only
void do_permute(std::string& str, unsigned i, unsigned n) {
// you COULD pass str by value here, which
// would remove the need to backtrack.
// however, it would create a new copy for every
// iteration which is terrible for performance,
// especially with long strings.
if(i==n)
std::cout << str << '\n';
else
for(unsigned j=i; j<=n; ++j) {
std::swap(str[i],str[j]);
do_permute(str,i+1,n);
std::swap(str[i],str[j]); // backtrack (undo swap)
}
}
// this is the public method;
// pass string by value (copy), to allow do_permute()
// to modify the string.
void permute(std::string str, unsigned i=0, unsigned n=0) {
if( n >= str.length() )
return; // prevent out of bounds access
// if n is 0 (default value) use the string length instead
do_permute(str, i, n ? n : (str.length()-1) );
}
int main() {
permute("BAR");
permute("FO0BAR", 3); // FOO***
permute("FO0BAR", 0, 2); // ***BAR
}